动态规划题 HDU-1024
http://acm.hdu.edu.cn/showproblem.php?pid=1024
Now I think you have got an AC in Ignatius.L's "Max 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 S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (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(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j x 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(i x, j x)(1 ≤ x ≤ m) instead. ^_^
题意就是求你n个数字m段和的最大值
用动态规划求dp[n][m] 表示用前m个数划分为n块的最大和
转移方程为 dp[n][m]=max(dp[n][m-1]+a[m],max(dp[n-1][t])+a[m]),其中dp[n][m-1]+a[m]表示第m个在前i块中 后面表示的是在自己独立变成一个块(1<=t<=m-1)
那么问题来了如果 用这种方法 复杂度为(n^3)因此我们要化简一下,求max(dp[n-1][t])的时候重复求了一些过程,因此只需要改成 设一个变量M 表示前1到m-1的最大值 M=max(M,dp[i-1][j-1]);
但是空间上不够,因此用一个滚动数组 dp[2][MAN];在求M的用到了i-1;
完整代码:
#include <stdlib.h>
#include<iostream>
#include<algorithm>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long
const ll INF=0x3f3f3f3f;
#define mod 1000000007
#define mem(a,b) memset(a,b,sizeof(a))
//__builtin_popcount
using namespace std;
//priority_queue
const ll MAX=1000000; int Max[MAX+10];
int dp[2][MAX+10];
int a[MAX+10];
int main()
{
int n,m;
while(cin>>m>>n)
{
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
mem(Max,0);
mem(dp,0);
int M;
for(int i=1; i<=m; i++)
{
M=-0x3f3f3f3f;
for(int j=i; j<=n; j++)
{
M=max(M,dp[(i-1)%2][j-1]);
if(i!=j)
dp[i%2][j]=max(dp[i%2][j-1]+a[j],M+a[j]);
else dp[i%2][j]=M+a[j];
} }
M=-0x3f3f3f3f;
for(int i=m; i<=n; i++)
M=max(M,dp[m%2][i]);
cout<<M<<endl;
} }
动态规划题 HDU-1024的更多相关文章
- 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" ...
- 快速上手leetcode动态规划题
快速上手leetcode动态规划题 我现在是初学的状态,在此来记录我的刷题过程,便于以后复习巩固. 我leetcode从动态规划开始刷,语言用的java. 一.了解动态规划 我上网查了一下动态规划,了 ...
- 转载:hdu 动态规划题集
1.Robberies 连接 :http://acm.hdu.edu.cn/showproblem.php?pid=2955 背包;第一次做的时候把概率当做背包(放大100000倍化为整数): ...
- 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 ...
- 动态规划之HDU水题
做水题的感觉真好系列 HDU 2084 数塔 1: 12: 1 23: 1 2 34: 1 2 3 45: 1 2 3 4 5 dp[i][j]第i行第j个数取得的最大值dp[i][j] = max( ...
随机推荐
- map和forEach的区别
总结 forEach()可以做到的东西,map()也同样可以.反过来也是如此. map()会分配内存空间存储新数组并返回,forEach()不会返回数据. forEach()允许callback更改原 ...
- 转 proguard 混淆工具的用法 (适用于初学者参考)
转自:https://www.cnblogs.com/lmq3321/p/10320671.html 一. ProGuard简介 附:proGuard官网 因为Java代码是非常容易反编码的,况且An ...
- jenkins的sonarqube之代码检测的两种方法
#:sonarqube下载地址,我们安装6.7 高版本已经不支持MySQL和Mariadb(最小3G内存) https://www.sonarqube.org/downloads/ #:安装文档 h ...
- oracle 外部表查alter日志
--创建文件夹,路径是alter日志的路径 create or replace directory data_dir as '/u01/app/oracle/diag/rdbms/orcl/orcl/ ...
- spring注解-自动装配
Spring利用依赖注入(DI)完成对IOC容器中中各个组件的依赖关系赋值 一.@Autowired 默认优先按照类型去容器中找对应的组件(applicationContext.getBean(Boo ...
- Flask + Nginx + uwsgi 部署过程
一.安装Flask 1.itsdangerous tar xvf itsdangerous-0.23.tar.gz cd itsdangerous-0.23/ python setup.py inst ...
- When does compiler create default and copy constructors in C++?
In C++, compiler creates a default constructor if we don't define our own constructor (See this). Co ...
- Reactor之发射器(Flux、Mono)转换操作函数
数据合并函数 由于业务需求有的时候需要将多个数据源进行合并,Reactor提供了concat方法和merge方法: concat public static <T> Flux<T&g ...
- docker容器使用loki收集日志
docker-compose安装loki套件(loki+promtail+grafana) loki进行日志聚合处理 类似elk中的es promtail是日志收集,类似elk中的logstash ...
- 沉淀vue相关知识(主要还是个人积累用)
路由懒加载的配置: const Home= () =>import('../components/Home') //使用ES6中的路由懒加载的方式 const About= () =>im ...