1

来源:互联网 发布:cdn网络节点 编辑:程序博客网 时间:2024/06/06 07:39
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<stdio.h>int main(){    int t,n,tt=1;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        //尺取法         int maxx=-1001,sum=0,l=1,left,right,x;        for(int i=1;i<=n;i++)        {            scanf("%d",&x);            sum+=x;            if(sum>maxx)            {                maxx=sum;                left=l;//更新起点                 right=i;//更新终点             }            if(sum<0)//顺序不能反,考虑如果一组数全是负数             {                sum=0;//如果当前的累加和已经小于0则抛弃这一段重新开始                 l=i+1;            }        }        printf("Case %d:\n",tt++);        printf("%d %d %d\n",maxx,left,right);        if(t) printf("\n");    }    return 0;}


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 东四型钢 东四周围医院 东四饭馆 东四型钢有限公司 东四附近租房 东四胡同 东四邮局 东四西餐 东四楼 东四速八 东四在哪个区 西四命住东四宅 东四砂锅居 东四东北菜 东四好吃的 东四妇幼保健医院 东四命配西四命 审美东四店 东四宏状元 东四工人文化宫 回龙观龙跃苑东四区 中航快捷酒店东四店 东圃客运站 东圃客运站汽车时刻表 东圃 马浩文东圃大桥事件 广州东圃 广州天河东圃 东圃租房 东圃地铁站 东士科技 东土 东土科技 东土科技股吧 封天龙帝东土西唐 300353东土科技 东土九祖 东土大茄作品 东土斗村 东土大唐 东土六祖