DP好题集选

2019年到了,马上又是省赛区域赛的一年了,但是我的dp水平还是堪忧,所以在众多网站(主要codeforce)搜罗一下dp的好题刷一下,并在此发一下解题报告来督促自己....

目录

  1. MemSQL Start[c]UP 3.0 - Round 1 C.Pie Rules
  2. Avito Cool Challenge 2018 C. Colorful Bricks(组合数学+dp)
  3. Codeforces Round #538 (Div. 2) D. Flood Fill
  4. AtCoder Beginner Contest 118 D - Match Matching
  5. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost(树上dp)
  6. Codeforces Round #518 (Div. 1) [Thanks, Mail.Ru!] A. Array Without Local Maximums(计数dp)
  7. [NOIP2018模拟赛] 小P的技能(计数dp+构造二叉树)
  8. Codeforces Global Round 1 D. Jongmah(思维+巧妙DP)
  9. Hello 2019 D. Makoto and a Blackboard(概率dp+积性函数)
  10. Comparing Your Heroes ZOJ 2002(状压dp+拓扑思维)
  11. Codeforces Round #427 (Div. 2) D. Palindromic characteristics(区间dp+hash)
  12. Misunderstood … Missing 2018 Ec-final 西安(二维01背包)
  13. Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(dp+矩阵快速幂优化)
  14. Codeforces Round #548 (Div. 2) D.Steps to On(期望DP+莫比乌斯反演)
  15. 浙江师范大学校赛 G.Little Sub and Piggybank(动态规划)
  16. 山东省第十届省赛 B.Flipping Game(DP)
  17. Educational Codeforces Round 64 (Rated for Div. 2)(换根dp)
  18. Atcoder AGC 030D Inversion Sum(期望dp)(好题)

MemSQL Start[c]UP 3.0 - Round 1 C.Pie Rules

题意:有n个派,每个派有一个大小,Alice和Bob有一个令牌,谁有令牌可以选择当前派给自己或对方,同时下一回合没有得到派的人将得到令牌,已知每个人都会选择最优的策略,Alice和Bob最终得到的派的体积分别是多少?

题解:因为第一回合是Bob拥有令牌,我们设dp[i]代表第i回合选择的人的最大价值,那么dp[1]就是Bob的答案,然后逆推进行转移就可以了,很不错的dp思维题~~

#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=1e6+7;
typedef long long ll;
const int mod=1e9+7;
#define IO cin.tie(0),ios::sync_with_stdio(false);
#define pi acos(-1)
#define PII pair<ll,ll>
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();return Nig*x;}
#define read read()
/** keep hungry and keep calm! **/

int a[maxn],dp[maxn],sum[maxn];
int Sheryang(){

    int n=read;
    for(int i=1;i<=n;i++){
        a[i]=read;
    }
    for(int i=n;i>=1;i--){
        sum[i]=sum[i+1]+a[i];
        dp[i]=max(dp[i+1],sum[i+1]-dp[i+1]+a[i]);
    }

    printf("%d %d\n",sum[1]-dp[1],dp[1]);
    return 0;
}

Avito Cool Challenge 2018 C. Colorful Bricks(组合数学+dp)

题意:n个方格m种颜色,要求有k个方格与左边的方格颜色不一样的方案数。

题解:比较简单的计数dp,dp[i][j]为前i个方格有j种不同的方案数,枚举到第i位的时候只有两种状态,和左边一样dp[i][j]+=dp[i-1][j]和左边不一样dp[i][j]+=dp[i-1][j-1]*(m-1)即可~~~

#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=1e6+7;
typedef long long ll;
const int mod=998244353;
#define IO cin.tie(0),ios::sync_with_stdio(false);
#define pi acos(-1)
#define PII pair<ll,ll>
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();return Nig*x;}
#define read read()
/** keep hungry and keep calm! **/

ll dp[2005][2005];
void add(ll &x,ll y){
    x+=y;
    if(x>=mod) x-=mod;
}

int Sheryang(){
    int n=read,m=read,K=read;

    dp[1][0]=m;
    for(int i=2;i<=n;i++){
        for(int j=0;j<=min(K,i-1);j++){
            add(dp[i][j],dp[i-1][j]);
            if(j) add(dp[i][j],dp[i-1][j-1]*(m-1)%mod);
            //printf("%d %d = %lld\n",i,j,dp[i][j]);
        }
    }

    printf("%lld\n",dp[n][K]);
    return 0;
}

Codeforces Round #538 (Div. 2) D. Flood Fill

题意:相邻方块颜色颜色一样则可成为一个联通块,每次可以把一个联通块变为相邻联通块的颜色,最小变换多少次使得所有方块都具有同一颜色。

题解:刚开始的方块左右两边可能和自己是同一颜色,没法直接进行区间dp,所以首先要将相同颜色的联通块压成一块,然后直接进行经典的区间dp就可以了~~(想到压缩也想到区间dp,但是没想到压缩然后区间dp,卡了一段时间才过,蠢死~)

#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=1e6+7;
typedef long long ll;
const int mod=1e9+7;
#define pi acos(-1)
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),
  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值