2019年到了,马上又是省赛区域赛的一年了,但是我的dp水平还是堪忧,所以在众多网站(主要codeforce)搜罗一下dp的好题刷一下,并在此发一下解题报告来督促自己....
目录
- MemSQL Start[c]UP 3.0 - Round 1 C.Pie Rules
- Avito Cool Challenge 2018 C. Colorful Bricks(组合数学+dp)
- Codeforces Round #538 (Div. 2) D. Flood Fill
- AtCoder Beginner Contest 118 D - Match Matching
- Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost(树上dp)
- Codeforces Round #518 (Div. 1) [Thanks, Mail.Ru!] A. Array Without Local Maximums(计数dp)
- [NOIP2018模拟赛] 小P的技能(计数dp+构造二叉树)
- Codeforces Global Round 1 D. Jongmah(思维+巧妙DP)
- Hello 2019 D. Makoto and a Blackboard(概率dp+积性函数)
- Comparing Your Heroes ZOJ 2002(状压dp+拓扑思维)
- Codeforces Round #427 (Div. 2) D. Palindromic characteristics(区间dp+hash)
- Misunderstood … Missing 2018 Ec-final 西安(二维01背包)
- Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(dp+矩阵快速幂优化)
- Codeforces Round #548 (Div. 2) D.Steps to On(期望DP+莫比乌斯反演)
- 浙江师范大学校赛 G.Little Sub and Piggybank(动态规划)
- 山东省第十届省赛 B.Flipping Game(DP)
- Educational Codeforces Round 64 (Rated for Div. 2)(换根dp)
- 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,所以