kuangbin专题十二 基础DP1【从入门到熟练】【9+1题】

HDU1024 Max Sum Plus Plus

感觉这题是整个系列里难度最高的题之一?

#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#define inf 2e9
#define maxnode 200000
#define ll long long
#define lowbit(x) (x&(-x))
const int mod = 998244353;
const int maxn = 1e6 + 10;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
using namespace std;

int a[maxn];
int dp[maxn][2],pre[maxn][2];//pre[i]是从合法位置到i的dp最大值

int main(){
    //ios::sync_with_stdio(false);
    int m,n;
    while( scanf("%d%d",&m,&n)!=EOF ){
        memset(dp,0,sizeof(dp));
        memset(pre,0,sizeof(pre));
        for(int i=1;i<=n;i++) scanf("%d",a+i);
        //前i个数找j个区间的最大和

        int last=0,now=1;//滚动数组
        //初始状态就是i=0的时候,都是dp[0][j]=0,那pre自然也是0

        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                if(j<i) continue;
                if(j==i) { dp[j-1][now]=-inf;  pre[j-1][now]=-inf; }//该子问题非法

                dp[j][now] = max(dp[j-1][now],pre[j-1][last])+a[j];
                pre[j][now] = max( pre[j-1][now],dp[j][now] );
            }
            last = !last;
            now = !now;
        }
        cout<<pre[n][last]<<endl;

    }

    return 0;
}

原文地址:https://www.cnblogs.com/ZhenghangHu/p/10222676.html

时间: 2024-08-09 04:26:52

kuangbin专题十二 基础DP1【从入门到熟练】【9+1题】的相关文章

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 G - 免费馅饼

https://vjudge.net/contest/68966#problem/G 正解一: http://www.clanfei.com/2012/04/646.html 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath> 7 #define IN

【算法系列学习】DP和滚动数组 [kuangbin带你飞]专题十二 基础DP1 A - Max Sum Plus Plus

A - Max Sum Plus Plus 1 https://vjudge.net/contest/68966#problem/A 2 3 http://www.cnblogs.com/kuangbin/archive/2011/08/04/2127085.html 4 5 /* 6 状态dp[i][j]有前j个数,组成i组的和的最大值.决策: 7 第j个数,是在第包含在第i组里面,还是自己独立成组. 8 方程 dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-

【算法系列学习】状压dp [kuangbin带你飞]专题十二 基础DP1 D - Doing Homework

https://vjudge.net/contest/68966#problem/D http://blog.csdn.net/u010489389/article/details/19218795 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath>

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 E - Super Jumping! Jumping! Jumping!

https://vjudge.net/contest/68966#problem/E http://blog.csdn.net/to_be_better/article/details/50563344 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath>

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 C - Monkey and Banana

https://vjudge.net/contest/68966#problem/C [参考]http://blog.csdn.net/qinmusiyan/article/details/7986263 [题意]:多组测试数据        每组测试数据给出n个砖块的长.宽.高,每种砖块有无穷多个,可以有三种不同的放置方法(xy;xz;yz),下面的砖要比上面的砖的长和宽都大,问最大的高度是多少. [思路]:[贪心+dp]每块砖有三种放置方法,把所有砖的所有状态都压入vector,先贪心,按面

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 B - Ignatius and the Princess IV

http://www.cnblogs.com/joeylee97/p/6616039.html 引入一个cnt,输入元素与上一个元素相同,cnt增加,否则cnt减少,当cnt为零时记录输入元素,因为所求数字至少出现(N+1)/2次,所以最后记录元素就是所求元素 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<cmath&g

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 F - Piggy-Bank 【完全背包问题】

https://vjudge.net/contest/68966#problem/F http://blog.csdn.net/libin56842/article/details/9048173 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath> 7

[kuangbin带你飞]专题十二 基础DP1 A - Max Sum Plus Plus HDU - 1024

A - Max Sum Plus Plus HDU - 1024 题目链接:https://vjudge.net/contest/68966#problem/A 题目: 现在我觉得你在Ignatius.L的“Max Sum”问题上得到了一个AC.要成为一个勇敢的ACMer,我们总是挑战自己更难的问题.现在你面临着一个更加困难的问题. 给定连续的数字序列S 1,S 2,S 3,S 4 ... S x,... S n(1≤x≤n≤1,000,000,-32768≤Sx≤32767).我们定义函数su

[kuangbin带你飞]专题十二 基础DP1 B - Ignatius and the Princess IV

B - Ignatius and the Princess IV 题目链接:https://vjudge.net/contest/68966#problem/B 题目: "OK, you are not too bad, em... But you can never pass the next test." feng5166 says. "I will tell you an odd number N, and then N integers. There will be