清华大学机试 最大序列和 Easy *DP问题连续最大和

基本思想:

无;

关键点:

无;

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

typedef long long ll;
const int maxn = 1000100;

ll dp[maxn];
ll d[maxn];
int n;

int main() {
	while (cin >> n) {
		for (int i = 0; i < n; i++) {
			cin >> d[i];
		}
		dp[0] = d[0];
		for (int i = 1; i < n; i++) {
			if (d[i] + dp[i - 1] > d[i]) {
				dp[i] = d[i] + dp[i - 1];
			}
			else {
				dp[i] = d[i];
			}
		}
		sort(dp, dp + n);
		cout << dp[n - 1] << endl;
	}
	return 0;
}

  

猜你喜欢

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