hdu---1024Max Sum Plus Plus(动态规划)
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15898 Accepted Submission(s): 5171
Given a consecutive number sequence S1, S2, S3, S4 ... Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ Sx ≤ 32767). We define a function sum(i, j) = Si + ... + Sj (1 ≤ i ≤ j ≤ n).
Now given an integer m (m > 0), your task is to find m pairs of i and j which make sum(i1, j1) + sum(i2, j2) + sum(i3, j3) + ... + sum(im, jm) maximal (ix ≤ iy ≤ jx or ix ≤ jy ≤ jx is not allowed).
But I`m lazy, I don't want to write a special-judge module, so you don't have to output m pairs of i and j, just output the maximal summation of sum(ix, jx)(1 ≤ x ≤ m) instead. ^_^
Process to the end of file.
Huge input, scanf and dynamic programming is recommended.
aa | -1 | 4 | -2 | 3 | -2 | 3 | ||||
第一遍 maxc | -1 | 4 | 4 | 4 | 5 | \ | ||||
dp | -1 | 4 | 2 | 5 | 3 | 6 | ||||
第二遍 maxc | -1 | 3 | 3 | 7 | 7 | \ | ||||
dp | -1 | 3 | 2 | 7 | 5 | 8 | ||||
代码
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int a[1000001],dp[1000001],max1[1000001];
int max(int x,int y){
return x>y?x:y;
}
int main(){
int i,j,n,m,temp;
while(scanf("%d%d",&m,&n)!=EOF)
{
dp[0]=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
dp[i]=0;
max1[i]=0;
}
max1[0]=0;
for(i=1;i<=m;i++){
temp=-0x3f3f3f3f;
for(j=i;j<=n;j++){
dp[j]=max(dp[j-1]+a[j],max1[j-1]+a[j]);
max1[j-1]=temp;
temp=max(temp,dp[j]);
}
}
printf("%d\n",temp);
}
return 0;
}
优化后的代码:
/*hdu 1024 @coder Gxjun*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=;
int aa[maxn],dp[maxn],maxc[maxn];
int max(int a,int b){
return a>b?a:b;
}
int main()
{
int n,m,i,j,temp;
while(scanf("%d%d",&m,&n)!=EOF){
memset(maxc,,sizeof(int)*(n+));
memset(dp,,sizeof(int)*(n+));
for(i=;i<=n;i++)
scanf("%d",&aa[i]);
for(i=;i<=m;i++){
temp=-0x3f3f3f3f;
for(j=i;j<=n;j++){
dp[j]=max(dp[j-],maxc[j-])+aa[j];
maxc[j-]=temp;
temp=max(temp,dp[j]);
}
}
printf("%d\n",temp);
}
}
hdu---1024Max Sum Plus Plus(动态规划)的更多相关文章
- HDU 1024Max Sum Plus Plus(最大m字段和)
/* 动态转移方程:dp[i][j]=max(dp[i-1]+a[i], max(dp[t][j-1])+a[i]) (j-1<=t<i) 表示的是前i个数j个字段和的最大值是多少! */ ...
- HDU 1176 免费馅饼 (动态规划)
HDU 1176 免费馅饼 (动态规划) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼 ...
- HDU 1074 Doing Homework (动态规划,位运算)
HDU 1074 Doing Homework (动态规划,位运算) Description Ignatius has just come back school from the 30th ACM/ ...
- HDOJ(HDU).1258 Sum It Up (DFS)
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
- HDU 1024 Max Sum Plus Plus [动态规划+m子段和的最大值]
Max Sum Plus Plus Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- hdu 1024 Max Sum Plus Plus (动态规划)
Max Sum Plus PlusTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1024 Max Sum Plus Plus (动态规划 最大M字段和)
Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum
Sum Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...
- HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)
C - 最大连续子序列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
随机推荐
- wamp出现could not execute run action问题
wamp出现could not execute run action问题 原文地址:http://blog.sina.com.cn/s/blog_4a60ba9c0100zzlr.html上午 ...
- Linux命令工具基础02 文件及目录管理
文件及目录管理 文件管理不外乎文件或目录的创建.删除.查询.移动,有mkdir/rm/mv 文件查询是重点,用find来进行查询:find的参数丰富,也非常强大: 查看文件内容是个大的话题,文本的处理 ...
- 在Yii用createUrl中明明白白生成网址
在Yii中经常要生成URL,不管是为了自动跳转还是仅仅是一个链接.下面对Yii中的URL生成做了一个总结.提示:以下controllerX代表控制器X,actionX代表方法X.在Controller ...
- C#开发Activex控件(1)
项目结构 创建Activex步骤: 1.选择创建类别(Windows 控件库或类库) 2.设置对应的com属性 AssemblyInfo.cs中须做以下设置:a.引入命名空间:using System ...
- iOS 设置button中图文位置
方法一. -(CGRect)imageRectForContentRect:(CGRect)contentRect { return CGRectMake(self.width - kImage ...
- Git的优势
分布式,强调个体 公共服务器压力和数据量都不会太大 速度快.灵活 任意两个开发者之间可以很容易的解决冲突 离线工作
- h5 input file ajax实现文件上传
<input type="file" accept="image/*" height="0" class="file_inp ...
- IE浏览器GET传参后台乱码
ie里面 get传递的字符串 为 gb2312 ,后台用的是utf-8类型 所以用 POST传递字符串到后端 否则进行js参数转码 encodeURI(""); 后端解码
- python语法笔记(六)
1.序列的方法 python中序列包含列表list.元组tuple.字符串str. 可以用于序列(表.元组.字符串)的内建函数: len(s) 返回: 序列中包含元素的个数 min(s) 返回 ...
- 深入理解JVM虚拟机-7虚拟机类加载机制
虚拟机把描述类的数据从Class文件夹加载到内存,并对数据进行小燕.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型,这就是虚拟机的类加载机制. 下面所说的Class文件不是具体的某个文件 ...