动态规划 hdu 1024
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 41033 Accepted Submission(s):
14763
Sum" problem. To be a brave ACMer, we always challenge ourselves to more
difficult problems. Now you are faced with a more difficult
problem.
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.
^_^
followed by n integers S1, S2,
S3 ... Sn.
Process to the end of
file.
line.
2 6 -1 4 -2 3 -2 3
8
Huge input, scanf and dynamic programming is recommended;
int dp[maxn],pre[maxn],arr[maxn];
int temp,n,m;
arr[ ]储存输入的数;
for(int k=;k<=m;k++)
{
temp=-inf;
for(int j=k;j<=n;j++)
{
dp[j]=max(dp[j-],pre[j-])+arr[j];
pre[j-]=temp;
temp=max(temp,dp[j]);
}
}
用temp来找出 j 个数取 k 组所得的组的最大和;同时把它记入在pre[ ]中,用于进行下次更新;
dp[j]=max(dp[j-1],pre[j-1])+arr[j];此时dp[j]和dp[j-1]有同样的组数,
dp[j]=dp[j-1]+arr[j]; // 表示把第j个数加入dp[j-1]的其中一组,能保持组数不变;
dp[j]=pre[j-1]+arr[j]; // 表示让第j个数独成一组,再加上比dp[j]少一组的组集中的最大的;
两者中选择大的组合方式;
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn=+;
const int inf=0x3f3f3f3f; int dp[maxn],pre[maxn],arr[maxn];
int temp,n,m; int main()
{
while(~scanf("%d%d",&m,&n))
{
for(int i=;i<=n;i++)
{
scanf("%d",&arr[i]);
}
memset(dp,,sizeof(dp));
memset(pre,,sizeof(pre));
for(int k=;k<=m;k++)
{
temp=-inf;
for(int j=k;j<=n;j++)
{
dp[j]=max(dp[j-],pre[j-])+arr[j];
pre[j-]=temp;
temp=max(temp,dp[j]);
}
}
printf("%d\n",temp);
}
return ;
}
动态规划 hdu 1024的更多相关文章
- 动态规划-----hdu 1024 (区间连续和)
给定一个长度为n的区间:求m段连续子区间的和 最大值(其中m段子区间互不相交) 思路: dp[i][j]: 前j个元素i个连续区间最大值 (重要 a[j]必须在最后一个区间内) 转移方程:dp[i][ ...
- HDU 1024 Max Sum Plus Plus (动态规划)
HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...
- HDU 1024 max sum plus
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- 怒刷DP之 HDU 1024
Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- Max Sum Plus Plus HDU - 1024
Max Sum Plus Plus HDU - 1024 Now I think you have got an AC in Ignatius.L's "Max Sum" ...
- HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1024 Max Sum Plus Plus (动态规划、最大m子段和)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1024(滚动数组+动态规划)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- Java基础10-集合
作业回顾 蜜蜂和熊的生产消费关系,熊在蜂蜜满10斤吃掉.蜜蜂一次生产一斤蜂蜜,且蜜蜂生成一斤蜂蜜花费的时间是10s. 十只蜜蜂和两只熊. 蜜蜂 bag: 20 每次产1,耗时10ms 满5的时候给蜜罐 ...
- Mysql --创建用户和授权,备份
权限管理 我们知道我们的最高权限管理者是root用户,它拥有着最高的权限操作.包括select.update.delete.update.grant等操作.那么一般情况在公司之后DBA工程师会创建一个 ...
- bootstrap 解决弹出窗口(modal) 常见问题
无法使用键盘esc关闭窗口方法: 首先在modal容器的div中增加属性tabindex="-1",其次设置键盘ESC属性keyboard为true: 方法1:使用js打开窗口时 ...
- 绝对定位下margin的作用
以前一直对绝对定位下的margin作用很模糊,今天细看一下 不使用top,left,margin等 <!DOCTYPE html> <html lang="en" ...
- 1、在Centos上安装Grafana
1.如果你想将老版本的Grafana升级为新版本的Grafana,可以阅读链接中的文档指南: http://docs.grafana.org/installation/upgrading/ 2.使用y ...
- spring boot slf4j日记记录配置详解
https://blog.csdn.net/liuweixiao520/article/details/78900779
- Pyqt walk 在Windows查找文件
在任意目录下查找需要的文件如何操作呢? 其实很简单, WIN+E [桌面计算机]- 右上角“搜索 计算机” 这个就是Windows自带的文件搜索功能.自己做一个文件搜索的应该应该也挺好玩的. 知识要点 ...
- symfony-安装,使用与创建应用程序以及创建第一个hello world界面
说明:由于学校里面要用到symfony3进行开发,并且之前对php和mysql有了一定的学习,所以这里进行对symfony2.3版本的学习,目前的版本已经到了symfony4了,但是本人之后要用到的是 ...
- 虚拟机Oracle VM VirtualBox linux系统如何访问windows共享文件夹
1. 在本机系统设置一个共享文件夹,用于与Ubuntu交互的区域空间. 2.右击状态栏上共享文件夹图标或菜单栏“设备-共享文件夹”,打开共享文件夹设置,如图示 3.点击共享文件夹设置框,右 ...
- Hashmap误区
HashMap简介 HashMap 是一个散列表,它存储的内容是键值对(key-value)映射.HashMap 继承于AbstractMap,实现了Map.Cloneable.java.io.Ser ...