北京大学机试 拦截导弹 需要二刷 *典型的最长不下降子串问题

基本思想:

最长不下降字串问题,注意dp数组的思想;

关键点:

无;

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn = 25;
int n;

int d[maxn];
int dp[maxn];

int main() {
	while (cin >> n) {
		fill(d, d + maxn, 0);
		fill(dp, dp + maxn, 0);
		for (int i = 0; i < n; i++) {
			cin >> d[i];
		}
		dp[0] = 1;
		for (int i = 1; i < n; i++) {
			int Max = 0;
			for (int j = 0; j < i; j++) {
				if (d[i] <= d[j] && dp[j] > Max)
					Max = dp[j];
			}
			dp[i] = max(Max+1, 1);
		}
		sort(dp, dp + n);
		cout << dp[n - 1] << endl;
	}
}

  

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12604662.html