PS: 这道题目很不错,虽然不难但是很经典,整理好思路再编码。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 0x7ffffff;
const int maxn = 100010;
//Accepted 1003 15MS 1020K 1114 B C++ Achiberx
int d[maxn];
int x[maxn];
int T, n;
void dp(int t)
{
int idx;
int st, endx;
int maxv = -INF;
memset(x, 0, sizeof(x));
for(int i = 1; i <= n; i++) {
if(i==1) x[i] = d[i];
else {
x[i] = max(d[i], x[i-1]+d[i]);
}
if(x[i] > maxv) {
maxv = x[i];
idx = i;
endx = i;
}
}
st = endx;
for(int i = idx-1; i >= 1; i--) {
if(x[i] >= 0) {
st = i;
}
else break;
}
printf("Case %d:\n", t);
printf("%d %d %d\n", maxv, st, endx);
if(t!=T) printf("\n");
}
int main()
{
scanf("%d", &T);
for(int t = 1; t <= T; t++) {
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &d[i]);
}
dp(t);
}
return 0;
}
/*********
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
4 0 0 0 0
5 -1 -2 0 -3 -4
5 2 -1 3 4 0
7 6 -1 -7 5 4 -7 9
5 -3 -2 -1 0 -5
4 0 0 0 0
12 -1 2 5 -1 3 -10 -2 3 4 -1 3 -10
********/