DP.思路:
1.求递增子序列的长度,递减子序列的长度;
2.然后找一个中间点,把两个序列连在一起,形成一个先递增后递减的子序列,也就是留下来的序列,
3.目标是要留下来的个数最大。
gaiAscendingHeight[I]表示从0~i的最长递增子序列的长度;
gaiDescendingHeight[I]表示从i~len-1的最长递减子序列的长度;
/* Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5495 Accepted: 1710 Description In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the soldiers are aligned in order by their code number: 1 , 2 , 3 , . . . , n , but they are not aligned by their height. The captain asks some soldiers to get out of the line, as the soldiers that remain in the line, without changing their places, but getting closer, to form a new line, where each soldier can see by looking lengthwise the line at least one of the line's extremity (left or right). A soldier see an extremity if there isn't any soldiers with a higher or equal height than his height between him and that extremity. Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line. Input On the first line of the input is written the number of the soldiers n. On the second line is written a series of n floating numbers with at most 5 digits precision and separated by a space character. The k-th number from this line represents the height of the soldier who has the code k (1 <= k <= n). There are some restrictions: ? 2 <= n <= 1000 ? the height are floating numbers from the interval [0.5, 2.5] Output The only line of output will contain the number of the soldiers who have to get out of the line. Sample Input 8 1.86 1.86 1.30621 2 1.4 1 1.97 2.2 Sample Output 4 */ #include <stdio.h> #include "stdlib.h" #include "string.h" #define MAX_SOLDIER_NUM 1024 #define MAX(a,b) (((a) > (b)) ? (a) : (b)) float gafHeight[MAX_SOLDIER_NUM]; int gaiAscendingHeight[MAX_SOLDIER_NUM]; int gaiDescendingHeight[MAX_SOLDIER_NUM]; int Alignmentmain(void) { int iTempNum; int iSoldierNum; int iSoldierRemainNum; int iLoop; int iLoopAscending ; int iLoopDescending; scanf("%d",&iSoldierNum); for (iLoop = 0; iLoop < iSoldierNum; iLoop++) { scanf("%f",&gafHeight[iLoop]); } memset(gaiAscendingHeight,0,MAX_SOLDIER_NUM*sizeof(int)); memset(gaiDescendingHeight,0,MAX_SOLDIER_NUM*sizeof(int)); /*Ascending LCS*/ for (iLoop = 0; iLoop < iSoldierNum; iLoop++) { iTempNum = 1; for (iLoopAscending = 0; iLoopAscending < iLoop; iLoopAscending++) { if (gafHeight[iLoop] > gafHeight[iLoopAscending] ) { iTempNum = MAX(iTempNum,gaiAscendingHeight[iLoopAscending]+1); } } gaiAscendingHeight[iLoop] = iTempNum; } /*Descending LCS*/ for (iLoop = iSoldierNum-1; iLoop >= 0; iLoop--) { iTempNum = 1; for (iLoopDescending = iSoldierNum-1; iLoopDescending > iLoop; iLoopDescending--) { if (gafHeight[iLoop] > gafHeight[iLoopDescending] ) { iTempNum = MAX(iTempNum,gaiDescendingHeight[iLoopDescending]+1); } } gaiDescendingHeight[iLoop] = iTempNum; } /*get mid point*/ iSoldierRemainNum = 0; for (iLoopAscending = 0; iLoopAscending < iSoldierNum; iLoopAscending++) { for (iLoopDescending = iSoldierNum-1; iLoopDescending >= iLoopAscending; iLoopDescending--) { if (iLoopAscending != iLoopDescending) { iSoldierRemainNum = MAX(iSoldierRemainNum,gaiAscendingHeight[iLoopAscending]+gaiDescendingHeight[iLoopDescending]); } else { iSoldierRemainNum = MAX(iSoldierRemainNum,gaiAscendingHeight[iLoopAscending]+gaiDescendingHeight[iLoopDescending]-1); } } } printf("%d/n",iSoldierNum - iSoldierRemainNum); return 0; }