HDOJ 1003-Max Sum
来源:互联网 发布:sql express 安装失败 编辑:程序博客网 时间:2024/06/07 00:58
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
思路:套模板 用beg来记录最大和子串的开始位置 然后通过开始位置求末位置
#include"stdio.h"#include"math.h"#include"stdlib.h"const int maxn=10e5+100;int ai[maxn];int mmax(int a,int b){ return (a>b)?a:b;}int maxsum(int x,int y,int &beg){ int a,b; if(x==y) { beg=x; return ai[x]; } int mid=(x+y)/2; int asum=maxsum(x,mid,a),bsum=maxsum(mid+1,y,b); int maxs=mmax(asum,bsum); int begs=(asum>bsum)?a:b; int l=-10e8,r=-10e8,vis=0,kais; for(int i=mid;i>=x;i--) { vis+=ai[i]; if(vis>l) { l=vis; kais=i; } } vis=0; for(int i=mid+1;i<=y;i++) { vis+=ai[i]; r=mmax(r,vis); } beg=(maxs>(l+r))?begs:kais; return mmax(maxs,l+r);}int main(){ int T,n; int beg; scanf("%d",&T); int s=T; while(T--) { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&ai[i]); } int big=maxsum(0,n-1,beg); int sum=0,endd; for(int i=beg;i<n;i++) { sum+=ai[i]; if(sum==big) { endd=i; break; } } printf("Case %d:\n",s-T); printf("%d %d %d\n",big,beg+1,endd+1); if(T) printf("\n"); } return 0;}
- HDOJ 1003 Max Sum
- HDOJ 1003 Max Sum
- hdoj 1003 Max Sum
- HDOJ 1003 Max Sum
- hdoj 1003Max Sum
- HDOJ 1003 Max Sum
- HDOJ 1003 Max Sum
- HDOJ--1003Max Sum
- hdoj 1003 Max Sum
- HDOJ 1003 Max Sum
- HDOJ 1003 Max Sum
- HDOJ 1003 Max Sum
- HDOJ 1003 Max Sum
- HDOJ 1003 Max Sum
- hdoj 1003 Max Sum
- HDOJ Max Sum 1003
- 【HDOJ 1003】 Max Sum
- HDOJ 1003 ( Max Sum )
- Android OpenGL10 基本图形绘制 <3>
- react-router 按需加载
- 初识Node
- 初识PHP(1)
- 每日异常
- HDOJ 1003-Max Sum
- css代码编写空心箭头
- sql注入之必备的基础知识
- 哈希表
- 如何使用爬虫采集天猫店铺商品信息
- How to Write Go Code
- Installation failed with message:INSTALL_CANCELED_BY_USER
- 在JSP页面引入jquery的时候发现,调试的时候发现报404错误
- 网络请求框架----AsyncHttpClient的get,post和图片上传服务器