hdu1003
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 138410 Accepted Submission(s): 32144
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.
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).
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.
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
14 1 4
Case 2:
7 1 6
- #include<iostream>
- #include<cstdio>
- #include<cstdlib>
- //#define LOCAL
- using namespace std;
- int main()
- {
- #ifdef LOCAL
- freopen("d:datain.txt","r",stdin);
- freopen("d:dataout.txt","w",stdout);
- #endif
- int n;
- while(scanf("%d",&n)!=EOF)
- {
- int i,m;
- for(i = ; i< n;i++)
- {
- scanf("%d",&m);
- int dp[],a[];
- scanf("%d",&a[]);
- dp[] = a[]; //当前最大
- for(int j = ; j<m;j++) //生成了dp状态数组了
- {
- scanf("%d",&a[j]);
- if(dp[j-]+a[j]<a[j]) //状态转移方程
- dp[j]=a[j];
- else
- dp[j]=dp[j-]+a[j];
- }
- int Max,End;
- Max = dp[];
- End = ;
- for(int j = ;j<m;j++) //寻找区间
- if(Max<dp[j])
- {
- End = j;
- Max = dp[j];
- }
- int Begin = End;
- int temp = ;
- for(int j = End;j>=;j--)
- {
- temp +=a[j];
- if(temp==dp[End])
- Begin = j;
- }
- cout<<"Case "<<i+<<":"<<endl<<Max<<" "<<Begin+<<" "<<End+<<endl;
- if(i<n-)
- cout<<endl;
- }
- }
- return ;
- }
简化后不带dp数组的,因为这题在dp问题中是比较简单的。
- //hdu 1003
- #include<stdio.h>
- int main()
- {
- int n;
- while(scanf("%d",&n)!=EOF)
- {
- for(int i = ;i<n;i++)
- {
- int a;
- int Max = -;
- int sum = ,m;
- int Begin=,End=,flag=;
- scanf("%d",&m);
- scanf("%d",&a);
- Max = sum = a;
- for(int j = ;j<m ;j++)
- {
- scanf("%d",&a);
- if(sum<)
- {
- sum=a;
- flag=j;
- }
- else
- {
- sum=sum+a;
- }
- if(Max<sum)
- {
- Max = sum ;
- Begin =flag;
- End = j;
- }
- }
- printf("Case %d:\n%d %d %d\n",i+,Max,Begin+,End+);
- if(i<n-)
- printf("\n");
- }
- }
- return ;
- }
hdu1003的更多相关文章
- hdu1000,hdu1001,hdu1002,hdu1003
hdu1000 仅仅是为了纪念 #include <cstdio> int main() { int a,b; while (scanf("%d%d",&a,& ...
- hdu1003 1024 Max Sum&Max Sum Plus Plus【基础dp】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4302208.html ---by 墨染之樱花 dp是竞赛中常见的问题,也是我的弱项orz, ...
- hdu1003 Max Sum(最大子串)
https://vjudge.net/problem/HDU-1003 注意考虑如果全为负的情况,特判. 还有输出格式,最后一个输出不用再空行. #include<iostream> #i ...
- hdu1003 Max Sum【最大连续子序列之和】
题目链接:https://vjudge.net/problem/HDU-1003 题目大意:给出一段序列,求出最大连续子序列之和,以及给出这段子序列的起点和终点. 解题思路:最长连续子序列之和问题其实 ...
- 解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾
2017-09-06 21:32:22 writer:pprp 可以作为一个模板 /* @theme: hdu1003 Max Sum @writer:pprp @end:21:26 @declare ...
- HDU1003 简单DP
Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the ...
- HDu1003(maxn sum)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABBcAAAMDCAYAAAD5XP0yAAAgAElEQVR4nOy97a8c133n2X+H3xjIC4
- hdu1003 dp
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1003 #include<cstdio> #include<algorit ...
- hdu1003 dp(最大子段和)
题意:给出一列数,求其中的最大子段和以及该子段的开头和结尾位置. 因为刚学过DP没几天,所以还会这题,我开了一个 dp[100002][2],其中 dp[i][0] 记录以 i 为结尾的最大子段的和, ...
- HDU1003前导和
简单维护前导和 #include<stdio.h> int main() { ],cas,key=; scanf("%d",&cas); while(cas-- ...
随机推荐
- perl 爬取数据<1>
use LWP::UserAgent; use POSIX; use DBI; $user="root"; $passwd="11111111"; $dbh=& ...
- 大量客户反映wordpress的网站打开巨慢,经分析发现,这些网站大都使用了google的字体服务,由于最近google的服务已经被大陆屏蔽,所以wordpress的网站打开时,会卡在字体加载上。
一会你安装完wp,发现打开巨卡的话,看看这个帖子:http://bbs.myhostcn.com/thread-1026-1-1.html最近一段时间,大量客户反映wordpress的网站打开巨慢, ...
- ERROR: HHH000388: Unsuccessful: create table
做SSH整合的时候,总是出现错误信息: 类似这样: : HHH000388: Unsuccessful: create table right (right_code varchar(255) not ...
- 格而知之3:Core Data的基本使用
最近准备做一个随手笔记类的app给自己用,考虑到从未使用过Core Data,就决定用Core Data来做数据存储.在网上参考了一些Core Data的资料后,用一天的时间写了这个demo,主要测试 ...
- Can you find it?(二分 二分+STL set map)
Can you find it? Time Limit : 10000/3000ms (Java/Other) Memory Limit : 32768/10000K (Java/Other) T ...
- 一种基于Qt的可伸缩的全异步C/S架构server实现(二) 网络传输
二.网络传输模块 模块相应代码命名空间 (namespace ZPNetwork) 模块相应代码存储目录 (\ZoomPipeline_FuncSvr\network) 2.1 模块结构 ...
- Oracle SQL函数之聚组函数
AVG([distinct|all]x) [功能]统计数据表选中行x列的平均值. [参数]all表示对所有的值求平均值,distinct只对不同的值求平均值,默认为all 如果有参数distinct或 ...
- C#事件、委托简单示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Hibernate 查询:HQL查询(Hibernate Query Languge)
HQL是一种面向对象的查询语言,其中没有表和字段的概念,只有类,对象和属性的概念. 使用HQL查询所有学生: public static void main(String[] args) { Sess ...
- stuts1:(Struts)Action类及其相关类
org.apache.struts.action.Action类是Struts的心脏,也是客户请求和业务操作间的桥梁.每个Action类通常设计为代替客户完成某种操作.一旦正确的Action实例确定, ...