首页 技术 正文
技术 2022年11月16日
0 收藏 571 点赞 2,948 浏览 2553 个字

Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42478    Accepted Submission(s): 15335

Problem DescriptionNow 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 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. ^_^ InputEach test case will begin with two integers m and n, followed by n integers S1, S2, S3 … Sn.
Process to the end of file. OutputOutput the maximal summation described above in one line. Sample Input1 3 1 2 32 6 -1 4 -2 3 -2 3 Sample Output6
8

Hint

Huge input, scanf and dynamic programming is recommended.

  题意:给出m和n,代表着有n个数,然后要你在这个n个数中找出m个连续子序列的最大和 思路:这道题难点不在状态方程,关键在于时间和空间的优化      首先分析下状态,影响我们决策的有两个因素   “数值”  “序列数”,所以我们先把状态设置为二维  dp[ i ][ j ],代表着前 j 个数中取  i 段 的最大值    然后分析下状态转移方程,按照思考,我们需要决定的就是  “要不要这个数”  和  “这个数放在哪个子序列比较好”,所以我们可以推出    dp[ i ][ j ] = max ( dp[ i ][ j -1 ], dp [ i-1 ][ x ]  )+num[ j ]     num[] 就是存放原始序列的数,x 就是 i-1~j-1的某一个数 ( 因为 dp[ i-1] 代表前面已经有i-1段了,所以一定大于 i-1),dp[ i -1][ x] 就代表着前 j 个数取 i-1 段的最大值.    这个方程代表 决定 第 j 个数,是放在第 i 段上,还是 自己单独成为 一段。      我们从方程中可以知道,决定“当前状态”的只与“前一状态有关”,   所以可以用滚动数组( 第一次听说的话,可以先去看看01背包的滚动数组优化 )来优化,    而 dp[ i -1][ x]可以用一个一维数组来优化,   我们知道 它代表着的是 “ 前 j 个数取 i-1 段的最大值 ”  所以 新设置一个一维数组  head[ j ] 代表着”  前 j 个数取 i-1 段的最大值 “    所以我们的方程变成了这样        dp[ j ]= max( dp[ j -1], head[ j – 1])+num[ j ]    还要记得实时更新head[]数组,因为dp[]是滚动的,而head[]也需要滚动       

//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#pragma GCC optimize(2)
//#include<bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include<sstream>
#include<iterator>
#include<cstring>
#include<string>
#include<cstdio>
#include<cctype>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;typedef double dou;
typedef long long ll;
#define pai acos(-1.0)
#define M 1000005
#define inf 0x3f3f3f3f
#define mod 1e18
#define left k<<1
#define right k<<1|1
#define W(a) while(a)
#define ms(a,b) memset(a,b,sizeof(a))int n, m, ans;
int num[M], head[M], dp[M];int main() {
ios::sync_with_stdio(false);
while (cin >> m >> n) {
ms(head, ), ms(dp, );
for (int i = ; i <= n; i++) {
cin >> num[i];
}
for (int i = ; i <= m; i++) {
ans = -inf;
for (int j = i; j <= n; j++) {
dp[j] = max(dp[j - ], head[j - ]) + num[j];
head[j - ] = ans;//此时的ans是i-1段时候的最大值
ans = max(dp[j], ans);//此时的ans是i段的时候的最大值
}
}
cout << ans << endl;
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,118
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,590
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,435
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,206
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,842
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,927