难度:3
动态规划
状态转移方程:f[x] = max{f[x] , f[x] + a[x-1]},起始位置用s[x]标记一下就行了
#include <cstdio>
#include <cstring>
const int maxn = 100100;
int f[maxn] , s[maxn], a[maxn] , T , n , cas = 1;
int main() {
scanf("%d" , &T);
while(T--) {
printf("Case %d:\n" , cas++);
scanf("%d" , &n);
for(int i=1;i<=n;i++) scanf("%d" , &a[i]);
f[1] = a[1];
s[1] = 1;
for(int i=2;i<=n;i++) {
if(f[i-1] < 0) {
f[i] = a[i];
s[i] = i;
}
else {
f[i] = f[i-1] + a[i];
s[i] = s[i-1];
}
}
int F = f[1] , S = 1 , t = 1;
for(int i=2;i<=n;i++) {
if(f[i] > F) {
F = f[i];
S = s[i];
t = i;
}
}
printf("%d %d %d\n" , F , S , t);
if(T) puts("");
}
return 0;
}