ACM学习感悟——HDU1204
2015-03-13 20:01
375 查看
Problem Description
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 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. ^_^
Input
Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 ... Sn.
Process to the end of file.
Output
Output the maximal summation described above in one line.
Sample Input
Sample Output
6
8
这道题还是有点让我头疼的,主要是状态转移方程有点让人费解:dp[i][j]=MAX(dp[i][j-1] , dp[i-1][k])+data[j]
其中i<k<=j。 表示前j个数分成i段的最大值。而且由于数据规模较大,要使用滚动数组来节省内存。
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 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. ^_^
Input
Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 ... Sn.
Process to the end of file.
Output
Output the maximal summation described above in one line.
Sample Input
1 3 1 2 3 2 6 -1 4 -2 3 -2 3
Sample Output
6
8
这道题还是有点让我头疼的,主要是状态转移方程有点让人费解:dp[i][j]=MAX(dp[i][j-1] , dp[i-1][k])+data[j]
其中i<k<=j。 表示前j个数分成i段的最大值。而且由于数据规模较大,要使用滚动数组来节省内存。
<span style="font-size:14px;color:#330000;">///////////////////////////////////////////////////////// // HDU 2844 dp // // // Created by 吴尔立 // // Copyright (c) 2015年 吴尔立. All rights reserved. // ///////////////////////////////////////////////////////// #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <cctype> #include <stack> #include <queue> #include <map> #include <string> #include <set> #include <vector> #define ll long long; #define INF 1<<31 #define cir(i,a,b) for (int i=a;i<=b;i++) #define CIR(j,a,b) for (int j=a;j>=b;j--) #define CLR(x) memset(x,0,sizeof(x)) using namespace std; #define maxn 1000005 int n,m; int a[maxn],d[maxn],td[maxn]; //td表示前一个状态的最大值; d[j]表示前j个元素分成i段的最大值 int main() { while (scanf("%d%d",&m,&n)!=EOF) { CLR(d); CLR(td); cir(i,1,n) scanf("%d",&a[i]); int temp; cir(i,1,m) { temp=-INF; cir(j,i,n) { d[j]=max(d[j-1],td[j-1])+a[j]; //这里有点难以理解,若是d+a[i],则代表是将ai并上去,否则是独立成一段。</span>
<span style="font-size:14px;color:#330000;"> td[j-1]=temp; //temp一直表示前一个状态的最大值。 temp=max(temp,d[j]); } } printf("%d\n",temp); } }</span>
相关文章推荐
- ACM学习感悟
- ACM学习感悟——POJ2139(Floyd)
- ACM学习感悟——POF3255 (用SPFA求次短路)
- ACM学习感悟——FZU2192(并查集)
- ACM 学习感悟——HDU2845
- ACM学习感悟——weekly training
- ACM学习感悟——摸底赛(并查集)
- ACM学习感悟——暴力专场E 暴力dp
- ACM学习感悟——HDU2844 多重背包
- ACM学习感悟——POJ1932(图论)
- ACM学习感悟——HDU-命运
- ACM学习感悟——POJ3723(kruskal,并查集)
- ACM学习感悟——HDU257 HOW TO TYPE
- ACM学习感悟——HDU5092(dp)
- ACM学习感悟——POJ1258(kruskal)
- ACM学习感悟——POJ3169(差分约束)
- ACM学习感悟——HUD5090(我也不知道我用的啥= =)
- ACM学习感悟——HDU1505 最大0矩阵
- ACM学习感悟——暴力专场F(dp)
- ACM学习感悟——Aizu2224 (kruskal 最大生成树)