http://acm.hdu.edu.cn/showproblem.php?pid=1003
现在真是老了,还记得很久以前写过很多动态规划题的,现在都忘了,又得从新开始了~(@^_^@)~
状态转移方程:f[i] = f[i-1]>=0?f[i-1] + a[i] : a[i].
终点t就是最大的f值对应的i,起点从t顺推到不连续就行了(连续的情况是:f[i] + a[i] == f[i+1])
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> using namespace std; template <class T> void checkmin(T &t,T x) {if(x < t) t = x;} template <class T> void checkmax(T &t,T x) {if(x > t) t = x;} template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;} template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;} typedef pair <int,int> PII; typedef pair <double,double> PDD; typedef long long ll; #define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++) int T , n , cas = 1 , a[100100] , f[100100]; int main() { scanf("%d",&T); while(T--) { if(cas > 1) puts(""); printf("Case %d:\n" , cas ++); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) { if(f[i-1] >= 0) f[i] = f[i-1] + a[i]; else f[i] = a[i]; } int s = 1 , t = 1; for(int i=1;i<=n;i++) if(f[i] > f[t]) t = i; for(s=t-1;s>0 && f[s] == f[s+1] - a[s+1];s--); s ++; printf("%d %d %d\n" , f[t] , s , t); } return 0; }