Max Sum (hdu 1003 简单DP水过)
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 161294 Accepted Submission(s): 37775
If there are more than one result, output the first one. Output a blank line between two cases.
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Case 1:
14 1 4 Case 2:
7 1 6
pid=1058" target="_blank" style="color:rgb(26,92,200); text-decoration:none">1058
1203 1257 1421 1024题意:给出n个数的序列,求出最大的子串和。并输出起点和终点。
思路:dp[i]表示以i为结尾的最大子串和。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std; int dp[maxn]; //dp[i]表示以i为结尾的最大子串和
int a[maxn];
int n; int main()
{
int i,j,t,cas=1;;
sf(t);
bool flag=false;
while (t--)
{
if (flag) pf("\n");
flag=true;
sf(n);
FRL(i,1,n+1)
sf(a[i]);
mem(dp,0);
dp[1]=a[1];
int S=1,T=1,s=1,t=1,maxx=a[1];//s,t记录当前首尾指针。S。T记录当前最大值的首尾指针
FRL(i,2,n+1)
{
if (a[i]>dp[i-1]+a[i]) //假设a[i]对dp[i-1]没有贡献反而会使dp[i-1]减小,那么就以i又一次作为起点
{
s=i;
t=i;
dp[i]=a[i];
}
else if (a[i]<=dp[i-1]+a[i]) //a[i]比dp[i-1]+a[i]更大,就将尾指针t向后移赋为i
{
t=i;
dp[i]=dp[i-1]+a[i]; //更dp[i]
}
if (dp[i]>maxx) //假设当前dp[i]比之前的最大值要大,更新最大值,并记录首尾指针
{
S=s;
T=t;
maxx=dp[i];
}
}
pf("Case %d:\n",cas++);
pf("%d %d %d\n",maxx,S,T);
}
return 0;
}
Max Sum (hdu 1003 简单DP水过)的更多相关文章
- Max Sum -- hdu -- 1003
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- Max Sum—hdu1003(简单DP) 标签: dp 2016-05-05 20:51 92人阅读 评论(0)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1024 Max Sum Plus Plus 简单DP
这题的意思就是取m个连续的区间,使它们的和最大,下面就是建立状态转移方程 dp[i][j]表示已经有 i 个区间,最后一个区间的末尾是a[j] 那么dp[i][j]=max(dp[i][j-1]+a[ ...
- HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1024 Max Sum Plus Plus(DP的简单优化)
Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...
- HDU 1024:Max Sum Plus Plus(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...
- HDU 1024 Max Sum Plus Plus【DP】
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
- HDU 1024:Max Sum Plus Plus(DP,最大m子段和)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU OJ Max sum 题目1003
#include <iostream> #include<stdio.h> #include<stdlib.h> using namespace std; i ...
随机推荐
- Unity3D NGUI,uGUI总结
跪求官方UI系统(2014年11月底已出,用原生的比用NGUI放心) uGUI注意点 1.要防止多个canvas叠加点击穿透,canvas里面的graphics raycaster调整到恰当选项 2. ...
- android launcher开发之图标背景以及默认配置
1:然后我自己看了一下桌面图标的载入过程: 桌面第一次载入时是默认读取一个xml配置文件,完毕配置工作.这个配置文件在Launcher文件夹下, 路径是:\Launcher\res\xml\defau ...
- UVA它11292 - Dragon of Loowater
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- shell telnet 路由器
#!/usr/bin/expect -f spawn telnet 172.16.1.80 expect "login" { send "admin\n" ex ...
- springboot 开发入门,及问题汇总
1 . springboot简单介绍(http://projects.spring.io/spring-boot/) 现在的web项目几乎都会用到spring框架,而要使用spring难免需要配置大量 ...
- list view Item 里面有ImageButton
list view Item 里面有ImageButton listview 的item中有ImageButton, 或者 Button. 目的: ImageButton 和 Button的click ...
- 采购订单me22n 或者me21n增强 (点击保存和回车)
IF_EX_ME_PROCESS_PO_CUST DATA:l_header TYPE mepoheader, l_item TYPE mepoitem. DATA:lt_items TYPE pur ...
- open sql 更新数据
insert语句: 1.插入单行数据有两种方法:wa是一个工作区 insert into dbtab values wa. insert into dbtab from wa. 该语句也可以将数据行插 ...
- RPC 的概念模型与实现解析(转)
今天分布式应用.云计算.微服务大行其道,作为其技术基石之一的 RPC 你了解多少?一篇 RPC 的技术总结文章,数了下 5k+ 字,略长,可能也不适合休闲的碎片化时间阅读,可以先收藏抽空再细读:) 全 ...
- 使用Curl进行抓取远程内容时url中文编码问题
PHP中对于URL进行编码,可以使用 urlencode() 或者 rawurlencode(),二者的区别是前者把空格编码为 '+',而后者把空格编码为 '%20',不过应该注意的是,在编码时应该只 ...