HDU 1024 Max Sum Plus Plus——♂简单♂DP
2017-08-17 21:24
525 查看
这题不是很好想,当时卡在这个题上主要是思维定式了,我看这个题的大体思路以及滚动数组的优化很像01背包,就按照01背包的大体思路来想了,这就导致我一直不明白为什么m要套在外层循环,为什么要对n进行压缩,后来翻了两页博客直到看到下面这位大佬的博客才明白:
http://blog.csdn.net/ninefailure/article/details/52127984
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 10;
int n, m, temp, a[maxn], dp[maxn], maxv[maxn];
int main()
{
while (scanf("%d %d", &m, &n) == 2) {
memset(dp, 0, sizeof(dp));
memset(maxv, 0, sizeof(maxv));
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= m; i++) {
temp = -INF;
for (int j = i; j <= n; j++) {
dp[j] = max(dp[j - 1], maxv[j - 1]) + a[j];
maxv[j - 1] = temp;
temp = max(temp, dp[j]);
}
}
printf("%d\n", temp);
}
return 0;
}
http://blog.csdn.net/ninefailure/article/details/52127984
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 10;
int n, m, temp, a[maxn], dp[maxn], maxv[maxn];
int main()
{
while (scanf("%d %d", &m, &n) == 2) {
memset(dp, 0, sizeof(dp));
memset(maxv, 0, sizeof(maxv));
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= m; i++) {
temp = -INF;
for (int j = i; j <= n; j++) {
dp[j] = max(dp[j - 1], maxv[j - 1]) + a[j];
maxv[j - 1] = temp;
temp = max(temp, dp[j]);
}
}
printf("%d\n", temp);
}
return 0;
}
相关文章推荐
- HDU 1024 Max Sum Plus Plus 简单DP
- Hdu 1024 Max Sum Plus Plus (dp)
- hdu 1024 Max Sum Plus Plus 一串数字中,m段连续数字最大和 滚动数组+dp
- 【HDU 1024】Max Sum Plus Plus(DP+滚动数组优化+最大m段字段之和)
- HDU - 1024 Max Sum Plus Plus(dp)
- HDU 1024 Max Sum Plus Plus (DP,水题)
- DP ( 滚动数组 )——Max Sum Plus Plus ( HDU 1024 )
- hdu 1024 Max Sum Plus Plus DP
- A - Max Sum Plus Plus HDU - 1024——基础dp
- Hdu 1024 Max Sum Plus Plus(基础DP)
- [简单DP]Max Sum Plus Plus HDU 102
- HDU 1024 Max Sum Plus Plus【DP+优化时间复杂度】
- HDU 1024 Max Sum Plus Plus[dp](最大m子段和)
- hdu 1024 Max Sum Plus Plus(经典dp) 有点小bug?
- HDU 1024 Max Sum Plus Plus 经典 DP
- hdu 1024 Max Sum Plus Plus m段连续子序列最大和 dp
- hdu 1024 Max Sum Plus Plus(dp)
- HDU 1024 Max Sum Plus Plus (dp, 降维)
- hdu 1024 Max Sum Plus Plus (dp优化)
- 【hdu 1024】Max Sum Plus Plus —— dp && 滚动数组