J - Max Sum

来源:互联网 发布:怎么破解红蜘蛛软件 编辑:程序博客网 时间:2024/05/16 07:22
J - Max Sum
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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

25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5

Sample Output

Case 1:14 1 4Case 2:7 1 6
#include <iostream>#include <cstdio>#define  MAX 100005using namespace std;int a[MAX];int main(){int T,N,t=0,sum[MAX],s[MAX],ans,tt;cin>>T;tt=T;while(tt--){cin>>N;for(int i=0;i<N;i++)cin>>a[i];sum[0]=a[0];s[0]=0;ans=0;for(int i=1;i<N;i++){if(sum[i-1]>=0){sum[i]=sum[i-1]+a[i];s[i]=s[i-1];}else{sum[i]=a[i];s[i]=i;} if(sum[ans]<sum[i]) ans=i;}printf("Case %d:\n%d %d %d\n",++t,sum[ans],s[ans]+1,ans+1);if(t!=T)cout<<endl;}return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 子账号登录不上怎么办 淘宝宝贝曝光量少怎么办 淘宝买东西退款卖家拒绝怎么办 华为手机淘宝不能收藏宝贝怎么办 包裹发出不想要了怎么办 淘宝改登录密码忘记了怎么办 三星s8应用闪退怎么办 淘宝购物商家不发货怎么办 淘宝商家迟迟不发货怎么办 买家地址写错了怎么办 买家写错快递地址怎么办 淘宝退款选择服务类型出不来怎么办 网购东西发错了怎么办 拒签商家不退款怎么办 红米手机无响应怎么办 淘宝网登录密码忘记了怎么办 淘宝网密码忘记了怎么办 京东抢购不发货怎么办 微商不想做了怎么办 减肥过程中饿了怎么办 滴滴车龄超过6年怎么办 网络公选课没过怎么办 大学网络课挂了怎么办 淘宝虚拟订单买家恶意退款怎么办 淘宝卖家虚拟发货怎么办 淘宝买虚拟产品被骗了怎么办 哈尔滨暖气低于十八度怎么办 淘客店铺没人买怎么办 淘宝商家不给退货怎么办 淘宝卖家拒绝退款申请怎么办 运费险赔付少了怎么办 买了运费险退货怎么办 卖家运费险退货怎么办 京东生鲜有坏的怎么办 与上级意见不一致时你将怎么办 物金所倒闭投资怎么办 电商平台欺骗客户怎么办 pdf电脑打开是乱码怎么办 excel表格打开是乱码怎么办 win10安装软件出现乱码怎么办 华为手机速度越来越慢怎么办