博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1003 Max Sum 求区间最大值 (尺取法)
阅读量:5085 次
发布时间:2019-06-13

本文共 1847 字,大约阅读时间需要 6 分钟。

            Max Sum

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

            Total Submission(s): 294096    Accepted Submission(s): 69830

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
 
#include
#include
#include
using namespace std;int num[100086];int main(){ int T; scanf("%d",&T); int cases=0; while(T--){ cases++; int n; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&num[i]); } int l=1,r=1; int x,y; int maxx=-999999999,sum=0; for(int i=1;i<=n;i++){ sum+=num[i]; if(sum>maxx){ x=l;y=i; maxx=sum; } if(sum<0){sum=0;l=i+1;} } if(cases!=1){printf("\n");} printf("Case %d:\n",cases); printf("%d %d %d\n",maxx,x,y); }}

  

转载于:https://www.cnblogs.com/ZGQblogs/p/9489907.html

你可能感兴趣的文章
nginx 反向代理
查看>>
实验九
查看>>
【原创】Metro大都会扫码乘地铁技术大揭密
查看>>
iOS的相对路径和绝对路径
查看>>
WordPress的have_posts()和the_post()
查看>>
实体框架 Code First
查看>>
BZOJ4044 : [Cerc2014] Virus synthesis
查看>>
BZOJ2215 : [Poi2011]Conspiracy
查看>>
逆序打印数组元素,并显示数组长度--简单
查看>>
excel合并
查看>>
时间操作(Java版)—获取给定日期N天后的日期
查看>>
fedora 安装 pidgin-lwqq
查看>>
LOJ6268拆分数
查看>>
完美分割字符串,实现字符串的splict功能
查看>>
c++的准备知识4
查看>>
Kafka权威指南阅读笔记(第六章)
查看>>
js关键词高亮,及关键词出现的次数
查看>>
vmware8安装win2008 r2后找不到网卡驱动的解决办法
查看>>
rpc框架之HA/负载均衡构架设计
查看>>
eclipse(javaee windows)
查看>>