http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html
这里难点只有求起始位置,把握状态变化就行。一般这种子序列问题,都可以用dp简化
#include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <cctype> #include <vector> #include <iterator> #include <set> #include <map> #include <sstream> using namespace std; #define mem(a,b) memset(a,b,sizeof(a)) #define pf printf #define sf scanf #define spf sprintf #define pb push_back #define debug printf("!\n") #define MAXN 205 #define MAX(a,b) a>b?a:b #define blank pf("\n") #define LL long long #define ALL(x) x.begin(),x.end() #define INS(x) inserter(x,x.begin()) #define pqueue priority_queue #define INF 0x3f3f3f3f int n,m; int dp[100005]; int main() { int i,j,k,a,b,kase=0; int t; sf("%d",&t); while(t--) { mem(dp,0); int ans = 0,s=1,y=1,p=1; sf("%d",&n); for(i=1;i<=n;i++) { sf("%d",&dp[i]); if(i==1) { ans = dp[i]; } else { if(dp[i-1]>=0) { dp[i] += dp[i-1]; } else p = i; } if(dp[i]>ans) { ans = dp[i]; y = i; s = p; } } pf("Case %d:\n%d %d %d\n",++kase,ans,s,y); if(t) blank; } return 0; }
时间: 2024-10-14 23:26:37