首页 技术 正文
技术 2022年11月21日
0 收藏 412 点赞 2,942 浏览 1894 个字

Max Sum Plus Plus

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

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.

dp[i][j][0] … 表示前i个数分成j个组,不选第i个数的最大得分

dp[i][j][1] … 表示前i个数分成j个组,选第i个数的最大得分

因为状态i只跟状态i-1, 所以可以用滚动数组来减空间

取最要自己写 。 否则卡常数会超时

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>using namespace std ;
const int N = ;
const int inf = 1e9+;int dp[][N][] , n , m , x[N] ;
inline int MAX( int a , int b ) {
if( a > b ) return a ;
else return b ;
}
int main() {
// freopen("in.txt","r",stdin);
while( ~scanf("%d%d",&m,&n) ) {
for( int i = ; i <= n ; ++i ) {
scanf("%d",&x[i]);
}
int v = ;
dp[v][][] = ;
dp[v][][] = x[] ;
for( int i = ; i < n ; ++i ) {
for( int j = ; j <= i + && j <= m ; j++ ) {
dp[v^][j][] = dp[v^][j][] = -inf ;
}
for( int j = min( m , i ) ; j >= ; --j ) {
if( j != i ) {
dp[v^][j+][] = MAX( dp[v][j][] + x[i+] , dp[v^][j+][] );
dp[v^][j][] = MAX( dp[v][j][] , dp[v^][j][]);
}
if( j != ) {
dp[v^][j][] = MAX ( dp[v^][j][] , dp[v][j][] + x[i+] ) ;
dp[v^][j+][] = MAX ( dp[v^][j+][] , dp[v][j][] + x[i+] ) ;
dp[v^][j][] = MAX ( dp[v^][j][] , dp[v][j][] ) ;
}
}
v ^= ;
}
int ans = -inf ;
if( m < n ) ans = MAX( ans , dp[v][m][] );
if( m > ) ans = MAX( ans , dp[v][m][] );
printf("%d\n",ans);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,030
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,147
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,859