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 ...
随机推荐
- C#日志写入
/// <summary> /// 写日志,指定日志文件 /// </summary> /// <param name="File"></ ...
- 关于cookie的清除
关于cookie的清除 设置cookie时若指定了目录,那么在删除cookie时也必须指定相同的目录,如此才可以删除之前设置的cookie的值!!!!!!!!!
- 长链非编码RNA(lncRNA)
长链非编码RNA(lncRNA) 转自:http://blog.sina.com.cn/s/blog_909da11301010bkz.html 长链非编码RNA(lncRNA)是一类转录本长 ...
- [HRBUSTOJ1476]Pairs(FFT)
题目链接:http://acm-software.hrbust.edu.cn/problem.php?id=1476 题意:给n个数,m次询问,每次询问一个k.问n个数里两数之和严格小于k的数对. 根 ...
- 移动端 移动web屏幕适配方案 随不同宽度的屏幕而改变
链接地址1:http://www.cnblogs.com/zjzhome/p/4802157.html 链接地址2:http://www.html-js.com/article/Mobile-term ...
- html5 drap & drop
小知识点记录一下:onselectstart,onselect 1.onselectstart 该js方法是用来控制盒中内容是否被允许选中 <head> <style> #tm ...
- tracert命令详解
一.windows.Linux系统下 tracert ip/网站域名 二.mac traceroute IP/域名 ---------2016-10-10 15:29:07-- source:[1]t ...
- js 的小效果---->选项卡
js选项卡 <!doctype html> <html> <head> <meta charset="utf-8"> <t ...
- 【Todo】【转载】深度学习&神经网络 科普及八卦 学习笔记 & GPU & SIMD
上一篇文章提到了数据挖掘.机器学习.深度学习的区别:http://www.cnblogs.com/charlesblc/p/6159355.html 深度学习具体的内容可以看这里: 参考了这篇文章:h ...
- 【Linux日志】系统日志及分析
Linux系统拥有非常灵活和强大的日志功能,可以保存几乎所有的操作记录,并可以从中检索出我们需要的信息. 大部分Linux发行版默认的日志守护进程为 syslog,位于 /etc/syslog 或 / ...