CodeForces 712D Memory and Scores
来源:互联网 发布:厄运葬礼知乎 编辑:程序博客网 时间:2024/06/01 21:27
题目链接:http://codeforces.com/contest/712/problem/D
dp
因为每轮Memory和Lexa能取的都在[-k,k],也就是说每轮两人分数的变化量在[-2k,2k];
故可以定义状态:dp[times][diff]为第times次Memory和Lexa的分数差为diff的方案数.
而dp[times][diff]可以从dp[times-1][diff-2k]到dp[times-1][diff+2k]转移而来;
又因为变化量为-2k时的方案数为1(-k,k),
变化量为-2k+1时的方案数为2(-k,k-1;-k+1,k),
变化量为-2k+2时的方案数为3(-k,k-2;-k+1,k-1;-k+2,k),
...,
变化量为-2k+m时的方案数为m+1,
...,
变化量为0时的方案数为2k+1,
...,
变化量为2k-m时的方案数为m+1,
...,
变化量为2k-1时的方案数为2,
变化量为2k时的方案数为1.
所以状态转移方程为:dp[times][diff]=dp[times-1][diff-2k]+2*dp[times-1][diff-2k+1]+3*dp[times-1][diff-2k+2]+...+(m+1)*dp[times-1][diff-2k+m]+...+2*dp[times-1][diff+2k-1]+dp[times-1][diff+2k];
这样的话,时间复杂度为O(k2t2),代码如下:
#include<iostream>#include<cmath>#define M 1000000007LL#define TIME 105#define DIFF 300000#define BASE 150000using namespace std;typedef long long LL;LL a,b,k,t,ans;LL dp[TIME][DIFF];int main(void){ cin>>a>>b>>k>>t; dp[0][a-b+BASE]=1; LL upper=a-b+BASE+2*k*t; LL lower=a-b+BASE-2*k*t; for(LL times=1;times<=t;++times){ for(LL diff=lower;diff<=upper;diff++){ for(LL m=0;m<=2*k;m++){ LL add=-2*k+m; if(diff+add>=lower){ if(add)dp[times][diff]+=(dp[times-1][diff+add]+dp[times-1][diff-add])*(m+1); else dp[times][diff]+=dp[times-1][diff]*(m+1); dp[times][diff]%=M; } } } } for(int i=BASE+1;i<=upper;++i) ans=(ans+dp[t][i])%M; cout<<ans<<endl;}
很显然,这会T,所以必须做出优化。
注意到:
dp[times][diff]是在dp[times][diff-1]的基础上前半段各个项减一,后半段各个项加一得到的,所以可以维护一个前缀和数组pre[i],那么
dp[times][diff]=dp[times][diff-1]+(pre[diff+2k]-pre[diff-1])-(pre[diff-1]-pre[(diff-1)-2k-1])
可以在O(1)的时间内完成,优化后的代码时间复杂度为O(kt2),代码如下:
- CodeForces 712D Memory and Scores
- 【26.87%】【codeforces 712D】Memory and Scores
- codeforces 712D Memory and Scores (dp)
- 【DP or 生成函数】[CodeForces - 712D]Memory and Scores
- codeforces 712D. Memory and Scores(dp)
- Codeforces 712D Memory and Scores【dp+前缀和】
- Codeforces 712D Memory And Scores 前缀和+DP
- Memory and Scores CodeForces
- [CF 712D] Memory and Scores
- Codeforces Round #370 (Div. 2) D. Memory and Scores
- CF370 D Memory and Scores
- Codeforces 712D Memory and Scores(前缀和优化dp)
- 动态规划(Memory and Scores,cf 712D)
- Codeforces Round #370 (Div. 2) D. Memory and Scores(DP)
- Codeforces Round #370 (Div. 2) D. Memory and Scores 动态规划
- Codeforces Round #370 (Div. 2) D. Memory and Scores —— DP
- [CF712D]Memory and Scores
- 【Codeforces Round 370 (Div 2) D】【前缀和打标记】Memory and Scores 双人随机数值增减K A比B多的方案数
- 疑难问题记录
- SVN服务器搭建和使用(二)
- Android Bitmap和Drawable互转及使用BitmapFactory解析图片流
- java定时器的使用(Timer)
- C# winform中一个类中如何调用另一个窗体的控件或方法
- CodeForces 712D Memory and Scores
- PDF 在线预览类库:Aspose.Pdf.dll
- 最优化理论之牛顿法
- html 中align和 valign
- bzoj3156 防御准备
- Android Studio 升级 2.2 Maven 插件不能使用的问题
- WebView·开车指南
- Faster RCNN学习笔记
- oracle 中 rollup、cube、grouping 使用详解 -- 使用oracle 样例表演示