Max Sum

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 <bits/stdc++.h>
using namespace std;
const int MAXN = + ;
int T, n;
int arr[MAXN], dp[MAXN];
int S, E;
int main() {
scanf("%d", &T);
for(int t = ; t < T; ++t) {
S = E = ;
cin >> n;
for(int i = ; i != n; ++i)
cin >> arr[i];
dp[] = arr[];
for(int i = ; i != n; ++i)
if(dp[i-] >= )
dp[i] = dp[i-] + arr[i];
else
dp[i] = arr[i];
int Max = dp[];
for(int i = ; i != n; ++i)
if(dp[i] >= Max) {
Max = dp[i];
E = i;
}
int sum = ;
for(int i = E; i >= ; --i) {
sum += arr[i];
if(sum == Max)
S = i;
}
cout << "Case " << t+ << ":" << endl;
cout << Max << " " << S+ << " " << E+ << endl;
if(t < T-) puts("");
}
return ;
}

用了数组

#include <bits/stdc++.h>
using namespace std; int main() {
int T,n;
int Max, S, E, sum, a;
cin >> T;
for(int t = ; t <= T; ++t) {
cin >> n;
S = E = sum = ;
Max = -;
int k = ;
for(int i = ; i != n; ++i) {
cin >> a;
sum += a;
if(sum > Max) {
Max = sum;
S = k;
E = i;
}
if(sum < ) {
sum = ;
k = i + ;
}
}
cout << "Case " << t << ":" << endl;
cout << Max << " " << S+ << " " << E+ << endl;
if(t < T) puts("");
}
return ;
}

不用数组

HDU1003 简单DP的更多相关文章

  1. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  3. codeforces Gym 100500H A. Potion of Immortality 简单DP

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

  4. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  5. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  6. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  7. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

  8. hdu 2471 简单DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 简单dp, dp[n][m] +=(  dp[n-1][m],dp[n][m-1],d[i][k ...

  9. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

随机推荐

  1. easyUi load方法重新加载表单的数据

    1.表单回显数据的方法 <script> //方法一 function loadLocal(){ $('#ff').form('load',{ name:'myname', email:' ...

  2. 如何学好JAVA编程

    我的思路:     1.我觉得先学学JSP,用纯JSP做个日记本简单的小系统,纯粹从语言层面上了解一些基   础知识,把tomcat玩熟了,就用记事本编就行,主要是熟悉,能够理解jsp运行机制.然   ...

  3. Linux下长时间ping网络加时间戳并记录到文本

    Linux下长时间ping网络加时间戳并记录到文本   由于一些原因,比如需要检查网络之间是否存在掉包等问题,会长时间去ping一个地址,由于会输出大量的信息而且最好要有时间戳,因此我们可以使用简单的 ...

  4. 数据导入导出Oracle数据库

    临近春节,接到了一个导入数据的任务,在Linux客户端中的数据有50G,大约3亿3千万行: 刚开始很天真,把原始的txt/csv文件用sh脚本转化成了oralce 的insert into 语句,然后 ...

  5. Excel—身份证生日提取

    一.只有18位的身份证号码 如: 身份证号 330682199302264000 41120019890823729X 231081199002256839 131101198203154666 36 ...

  6. Servlet接口五种方法介绍

    Servlet接口定义了5种方法: init() service() destroy() getServletConfig() getServletInfo() init() 在Servlet实例化后 ...

  7. Windows平台下Qt中glut库的使用

    用Qt中的QGLWidget窗体类中是不包括glut工具库的,难怪在myGLWidget(在我的程序中是QGLWidget的派生类)中绘制实心球体是说“glutSolidSphere”: 找不到标识符 ...

  8. ASP.NET AJAX调用 WebService

    同事的代码,帮忙修改的,为了实现页面跳转回来后,状态的保持,Service 使用了Session. 主要的JS $.ajax({ url: "/ws/StaffInfo.asmx/Note& ...

  9. ZOJ 3703 Happy Programming Contest

    偏方记录背包里的物品.....每个背包的价值+0.01 Happy Programming Contest Time Limit: 2 Seconds      Memory Limit: 65536 ...

  10. MVC验证session是否过期,在每个action执行之前验证

            protected override void OnActionExecuting(ActionExecutingContext filterContext)         {   ...