dp经典题,这题一年前就做过了,主要在确定begin和end有技巧,题目要求输出第一个sub,所以begin要尽量左移,end尽量右移
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <string> const int MAXN = 1E5+10; using namespace std; int dp[MAXN]; int main() { int t,myc=1; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&dp[i]); } int l,r,temp,maxt=dp[0]; l = r = temp = 0; for(int i=1;i<n;i++){ if(dp[i-1]<0){temp = i;} //重新设置起始节点 dp[i] += max(0,dp[i-1]); if(maxt<dp[i]){ maxt = dp[i]; l = temp; r = i; } } cout<<"Case "<<myc++<<":"<<endl; cout<<maxt<<" "<<l+1<<" "<<r+1<<endl; if(t)cout<<endl; } return 0; }
时间: 2024-11-03 01:35:34