HDU1003MAX SUM
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 206582 Accepted Submission(s):
48294
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.
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).
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 <bits/stdc++.h>
- using namespace std;
- const int maxn=1e5+;
- int a[maxn],n;
- int main()
- {
- int t,cas=;
- scanf("%d",&t);
- while(t--)
- {
- scanf("%d",&n);
- int sum=,ans=-;
- int s=,e=,k=;
- for(int i=;i<=n;i++)
- {
- scanf("%d",&a[i]);
- sum+=a[i];
- if(sum>ans)
- {
- s=k;
- e=i;
- ans=sum;
- }
- if(sum<) //0的意义就是这段数做的是负功
- {
- sum=;
- k=i+;
- }
- }
- printf("Case %d:\n",cas++);
- printf("%d %d %d\n",ans,s,e);
- if(t>) puts("");
- }
- return ;
- }
- #include <bits/stdc++.h>
- using namespace std;
- const int N=1e5+;
- int a[N],n;
- int main()
- {
- int t,cas=;
- cin>>t;
- while(t--)
- {
- cin>>n;
- int sum=,mx=-,s=,e=,ts=;
- for(int i=;i<=n;i++)
- {
- cin>>a[i];
- sum+=a[i];
- if(sum>mx)
- {
- mx=sum;
- s=ts;
- e=i;
- }
- if(sum<)
- {
- sum=;
- ts=i+;
- }
- }
- printf("Case %d:\n",cas++);
- printf("%d %d %d\n",mx,s,e);
- if(t) printf("\n");
- }
- return ;
- }
- /*
- 100
- 2 1 2
- 1 1
- 3 -1 1 2
- 2 -7 3
- */
牢记顺序是 加 大于 小于!!
HDU1003MAX SUM的更多相关文章
- HDU1003MAX SUM (动态规划求最大子序列的和)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 动态规划:HDU1003-Max Sum(最大子序列和)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- 动态规划: HDU1003Max Sum
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- C++-HDU1003-Max Sum
时间复杂度O(n) 空间复杂度O(1) #include <cstdio> int main() { int T;scanf("%d",&T); ,n,a,l, ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
随机推荐
- SpringMVC数据库链接池,以及其他相关配置
1.applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
- C++中使用array报错 requires compiler and library surpport for the ISO c++ 2011 standard
#error This file requires compiler and library support for the \ISO C++ 2011 standard. This support ...
- 浅谈php对api开发的作用
最近正在做一个手机APP的服务端API开发,虽然是基于Ruby on Rails的,做的也不太专业,不过大致相通,希望能够给你一些启发. 首先,如果是比较简单的手机APP,例如新闻客户端这样的不会涉及 ...
- 利用Arraylist输入学生的成绩,求出平均分和总分。
Console.WriteLine("请输入学生人数:"); int n=int.Parse(Console.ReadLine()); ArrayList arr= new Arr ...
- stl 迭代器(了解)
STL 主要是由 containers(容器),iterators(迭代器)和 algorithms(算法)的 templates(模板)构成的. 对应于它们所支持的操作,共有五种 iterators ...
- 使用PPA在Ubuntu上安装php5.4~5.6,7
使用PPA在Ubuntu上安装php5.4~5.6,7 sudo apt-get install software-properties-common sudo add-apt-repository ...
- 使用I/O 系统调用--copy.c
作为Linux/Unix 系统编程入门,小生按照自己可以理解的方式,改写了源copy.c源代码来自:Linux/UNIX 系统编程手册 上册 P57 #include <stdio.h>/ ...
- CSS transition 过渡 详解
transition 过渡 IE10.Firefox.Chrome.Opera 支持 transition 属性. Safari 需要前缀 -webkit-. Chrome 25 以及更早版本需要前缀 ...
- PHP中is_numeric函数十六进制绕过0day
0×00 简介国内一部分CMS程序里面有用到过is_numberic函数,我们先看看这个函数的结构bool is_numeric ( mixed $var )如果 var 是数字和数字字符串则返回 T ...
- .NET设计模式(2):单件模式(Singleton Pattern)(转载)
概述 Singleton模 式要求一个类有且仅有一个实例,并且提供了一个全局的访问点.这就提出了一个问题:如何绕过常规的构造器,提供一种机制来保证一个类只有一个实例?客户程 序在调用某一个类时,它是不 ...