HDU1003 結題報告(最大子序列和)
阿新 • • 發佈:2018-11-22
題解
#include<iostream> #include<algorithm> #include<string> #include<math.h> #include<set> #include<map> #include<queue> using namespace std; typedef long long ll; const int MAXN = 1e5 + 5; int a[MAXN]; int main() { std::ios::sync_with_stdio(false); int t = 1 , n, T; int maxSum, l, r, position, thisSum; cin >> T; while (T--) { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } position = l = r = 0; thisSum = maxSum = a[0]; for (int i = 1; i < n; i++) { if (thisSum < 0) { thisSum = a[i]; position = i; } else { thisSum += a[i]; } if (thisSum > maxSum) { maxSum = thisSum; l = position; r = i; } } printf("Case %d:\n", t++); cout << maxSum << " " << l+1 << " " << r+1 << endl; if (T) { cout << endl; } } return 0; }