hdu -1231最大連續子序列
阿新 • • 發佈:2018-12-28
題目:http://acm.hdu.edu.cn/showproblem.php?pid=1231
思路:區間最大子序列的想法,從sum=0,(左邊界)l=0開始,sum+=a[i],如果得到sum值小於0,就將sum清零,並將左邊界更新為l=i+1,dp的思維方式。
#include <iostream> #include <cstdio> #include <cstdlib> #include <vector> #include <cstring> #include <set> #include <algorithm> #include <queue> using namespace std; typedef long long ll; int a[100010]; int main() { int n; while (cin >> n&&n) { for (int i = 1; i <= n; i++) cin >> a[i]; int wei = 2; int l, r; int sum = 0; int MAX = -11111; for (int i = 1; i <= n; i++) { sum += a[i]; if (sum > MAX) { MAX = sum; r = wei; l = i + 1; } if (sum < 0) { sum = 0; wei = i + 2; } } if (MAX < 0) cout << 0 << " " << a[1] << " " << a[n] << endl; else cout << MAX << " " << a[r - 1] << " " << a[l - 1] << endl; } //system("pause"); }