AC代码
同样的代码一开始提交第三个测试点答案错误,过了一会又提交了一次就AC了。。
#include <iostream>
#include <vector>
using namespace std;
struct DP{
int maxSum,L;
};
int main(){
int k;
vector<int> str;
scanf("%d",&k);
bool allN = true;
for(int i=0;i<k;i++){
int temp;
scanf("%d",&temp);
str.push_back(temp);
if(temp >= 0) allN = false;
}
if(allN == true){
printf("0 %d %d",str[0],str[k-1]);
system("pause");
return 0;
}
DP dp[10010];
dp[0].maxSum=str[0];
dp[0].L=0;
int max=str[0],L=0,R=0;
for(int i=1;i<k;i++){
dp[i].maxSum = dp[i-1].maxSum+str[i] >= str[i] ? dp[i-1].maxSum+str[i] : str[i];
dp[i].L = dp[i-1].maxSum+str[i] >= str[i] ? dp[i-1].L : i;
if(dp[i].maxSum > max){
max = dp[i].maxSum;
L = dp[i].L;
R = i;
}
}
printf("%d %d %d",max,str[L],str[R]);
system("pause");
return 0;
}