利用动态规划来计算最长递减子序列。 dp[i]为以h[i]结尾的最长递减子序列的长度 #include <iostream> using namespace std; int h[10000]; int dp[10000]; #define max(a,b) ((a)>(b)?(a):(b)) int main() { int len, next, ans, i, j, count=0; while (cin>>h[0] && h[0]!=-1) { count++; i = 0; ans = 1; while (cin>>h[++i] && h[i]!=-1); len = i; memset(dp, 0, sizeof(dp)); dp[0] = 1; for (i=1; i<len; i++) { for (j=0; j<i; j++) if (h[i]<=h[j] && dp[i]<dp[j]+1) dp[i] = dp[j] + 1; dp[i] = max(dp[i], 1); ans = max(ans, dp[i]); } if (count != 1) cout << endl; cout << "Test #" << count << ":/n" << " maximum possible interceptions: " << ans << endl; } }