Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 174588    Accepted Submission(s): 40639

Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
 
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
 
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence.
If there are more than one result, output the first one. Output a blank line between two cases.
 
Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
 
Sample Output
Case 1:
14 1 4 Case 2:
7 1 6

题意:给定一个序列,求其子序列的最大和,还有最大和的子序列的起始位置和结束位置。

求和与end都没什么好说的。求start是亮点,一开始判断了很久,后来还是那个想法,对于序列或是字符串正着想想不出来就逆过来想,从左到右end容易求,那从右至左的话start就容易求了。

代码:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#pragma warning(disable:4996)
using namespace std; int value[100005];
int dp[100005]; int main()
{
//freopen("input.txt","r",stdin);
//freopen("out.txt","w",stdout); int Test,num,i,j,ans,start,end;
value[0]=0; cin>>Test;
for(j=1;j<=Test;j++)
{
ans = -1005;
memset(dp,0,sizeof(dp)); cin>>num;
for(i=1;i<=num;i++)
{
cin>>value[i];
dp[i]=max(dp[i-1]+value[i],value[i]);
if(dp[i]>ans)
{
ans=dp[i];
end=i;
}
}
ans=-1005;
memset(dp,0,sizeof(dp));
for(i=num;i>=1;i--)
{
dp[i]=max(dp[i+1]+value[i],value[i]);
if(dp[i]>=ans)
{
ans=dp[i];
start=i;
}
}
cout<<"Case "<<j<<":"<<endl;
cout<<ans<<" "<<start<<" "<<end<<endl; if(j!=Test)
cout<<endl;
} return 0;
}

后来看discuss里其他人的思路,觉得从end开始往回倒,对每一个值都加起来,什么时候等于求出来的最大和了,什么时候就是start了,觉得这样做也很好。

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. 一步步搭建react-native环境(苹果OS X)
  2. hadoop2.2.0伪分布式搭建3--安装Hadoop
  3. Unity3d:Unknown type &#39;System.Collections.Generic.CollectionDebuggerView&#39;1
  4. C#获得和发送网站Session
  5. ll 命令不好用了,ls 命令没有颜色了怎么办
  6. 黑马程序猿 ---------- Java网络技术之 ---正則表達式 (Day06)
  7. Spark RDD编程核心
  8. 【原创】bootstrap框架的学习 第八课 -[bootstrap表单]
  9. Spark操作HBase问题:java.io.IOException: Non-increasing Bloom keys
  10. C#中DataGridView动态添加行及添加列的方法
  11. iOS-cocoapods安装与使用以及常见错误
  12. Java开发速度神器Lombok,Eclipse端安装使用教程
  13. 微信小程序之动态获取元素宽高
  14. 使用EasyNetQ简化RabbitMQ操作
  15. Codeforces 101623E English Restaurant - 动态规划
  16. 局域网下Android与scoket通信的实现
  17. P1216 数字三角形
  18. python之item方法
  19. 面试4——java进程和线程相关知识
  20. node 相关网站

热门文章

  1. ASM ClassReader failed to parse class file
  2. 45 孩子们的游戏(圆圈中最后剩下的数) + list操作总结+ for_each多记忆容易忘记
  3. SSM-Maven配置
  4. spingboot中使用scheduled设置定时任务注意事项
  5. 071、Java面向对象之使用private封装属性
  6. 图的数据结构的实现与遍历(DFS,BFS)
  7. Shortest and Longest LIS
  8. 2017北京网络赛 F Secret Poems 蛇形回路输出
  9. CF 1198 A. MP3 模拟+滑动窗口
  10. 学习Linux系统永远都不晚