博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1024(最大M子段和)
阅读量:5290 次
发布时间:2019-06-14

本文共 2637 字,大约阅读时间需要 8 分钟。

Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 21540    Accepted Submission(s): 7215

 

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 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. ^_^
 
Input
Each test case will begin with two integers m and n, followed by n integers S
1, S
2, S
3 ... S
n.
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
 
Hint
Huge input, scanf and dynamic programming is recommended.
 
Author
JGShining(极光炫影)
 
dp[i][j]表示前j个数组成的序列最大i子段和,则状态转移方程为
dp[i][j]=max{dp[i][j-1]+a[j],max{dp[i-1][t]}+a[j]} i-1=<t<j-1
优化1:本题数据范围太大, 所以可以用滚动数组优化
优化2:用last数组记录上一层循环得到的最大值,具体见代码
/*ID: LinKArftcPROG: 1024.cppLANG: C++*/#include #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define eps 1e-8#define randin srand((unsigned int)time(NULL))#define input freopen("input.txt","r",stdin)#define debug(s) cout << "s = " << s << endl;#define outstars cout << "*************" << endl;const double PI = acos(-1.0);const double e = exp(1.0);const int inf = 0x3f3f3f3f;const int INF = 0x7fffffff;typedef long long ll;const int maxn = 1000010;int num[maxn], dp[maxn], last[maxn];int main() { int n, m; while (~scanf("%d %d", &m, &n)) { memset(dp, 0, sizeof(dp)); memset(last, 0, sizeof(last)); for (int i = 1; i <= n; i ++) scanf("%d", &num[i]); int ma; for (int i = 1; i <= m; i ++) { //分成i段 ma = INT_MIN; for (int j = i; j <= n; j ++) { //前j个数 dp[j] = max(dp[j-1], last[j-1]) + num[j];//dp[j]表示前j个划分成i段最大值,last[j-1]表示前j-1个划分成i-1段最大值 last[j-1] = ma; ma = max(ma, dp[j]); } last[n] = ma; } printf("%d\n", ma); } return 0;}

 

转载于:https://www.cnblogs.com/LinKArftc/p/4963191.html

你可能感兴趣的文章
字节流与字符流的区别详解
查看>>
20141026--娱乐-箱子
查看>>
自定义分页
查看>>
Oracle事务
查看>>
任意输入10个int类型数据,把这10个数据首先按照排序输出,挑出这些数据里面的素数...
查看>>
String类中的equals方法总结(转载)
查看>>
图片问题
查看>>
bash使用规则
查看>>
AVL数
查看>>
第二章练习
查看>>
ajax2.0
查看>>
C#时间截
查看>>
C语言程序设计II—第九周教学
查看>>
C# 获取系统时间及时间格式转换
查看>>
WCF、WebAPI、WCFREST、WebService之间的区别
查看>>
2018-2019-2-20175332-实验四《Android程序设计》实验报告
查看>>
全栈12期的崛起之捡点儿有用的说说
查看>>
基础类型
查看>>
属性动画
查看>>
标识符
查看>>