题目描述:输入任一序列,每一序列以-1结束,求最长下降子序列;在做DP的时候,一定要记得赋初值!!状态转移方程:D[i] = max{1,D[j] + 1}(j = 1,2,3...i-1,A[i] < A[j]) #include <iostream> #define SIZE 5001 using namespace std; int a[SIZE]; int d[SIZE]; int main() { int i, j, m, n, max, temp; m = 0; while(1) { cin >> temp; if (temp == -1) break; /* 读入数据 */ n = 1; a[n] = temp; while(1) { cin >> temp; if (temp == -1) break; a[++n] = temp; } /* DP */ max = 0; for (i = 1; i <= n; i++) { d[i] = 1; for (j = 1; j <= i - 1; j++) { if (a[i] < a[j] && d[i] < d[j] + 1) { d[i] = d[j] + 1; } } if (d[i] > max) max = d[i]; } /* 输出结果 */ cout << "Test #" << ++m << ":" << endl; cout << " maximum possible interceptions: " << max << endl; cout << endl; } return 0; }