[笔记] Codeforces#274 Riding in a Lift (479E) DP
2014-10-22 17:13
519 查看
http://codeforces.com/problemset/problem/479/E
dp[i][j] 表示第i次到达j层的路径数量
转移方程
dp[i][j] = sum(dp[i-1][k]) , (k是满足要求的起始层)
由于k的集合组成一个连续的区间(剔除k=j), 故可以预先计算出区间左右边界,并维护一个部分和O(1)求出
sum(dp[i-1][k]) = sum[i-1][right] - sum[i-1][left-1] - dp[i-1][j]
使用滚动数组
dp[i][j] 表示第i次到达j层的路径数量
转移方程
dp[i][j] = sum(dp[i-1][k]) , (k是满足要求的起始层)
由于k的集合组成一个连续的区间(剔除k=j), 故可以预先计算出区间左右边界,并维护一个部分和O(1)求出
sum(dp[i-1][k]) = sum[i-1][right] - sum[i-1][left-1] - dp[i-1][j]
使用滚动数组
#include <iostream> #include <cstring> #include <cmath> #include <cstdio> using namespace std; const int MAX = 5005; const int MOD = 1000000007; int dp[2][MAX]; int sum[2][MAX]; int main() { int n,a,b,k; cin>>n>>a>>b>>k; memset(dp,0,sizeof(dp)); memset(sum,0,sizeof(sum)); int sw = 0; dp[sw][a] = 1; for (int i=1;i<=n;i++) sum[sw][i] = sum[sw][i-1]+dp[sw][i]; for (int i=1;i<=k;i++){ for (int x=1;x<=n;x++){ if (x==b) continue; int left,right; if (x<b){ left = 1; right = (x+b)/2; if ((x+b)%2==0) right--; }else { left = (x+b)/2+1; right = n; } dp[sw^1][x] = ((sum[sw][right] - sum[sw][left-1] -dp[sw][x])%MOD+MOD)%MOD; sum[sw^1][x] = (sum[sw^1][x-1] + dp[sw^1][x])%MOD; } sw^=1; } int ans = 0; for (int i=1;i<=n;i++) ans = (ans+dp[sw][i])%MOD; printf("%d\n",ans); return 0; }
相关文章推荐
- 【Codeforces Round 274 (Div 2)E】【DP 成段更新 打标记法 滚动数组】Riding in a Lift 乘坐k次电梯避免到达b层的方案数
- Codeforces Round #274 (Div. 2) E题:Riding in a Lift(DP)
- Codeforces Round #274 Riding in a Lift (DP)
- Codeforces Round #274 Div.1 C Riding in a Lift --DP
- Codeforces Round #274 Div.1 C Riding in a Lift --DP
- Codeforces Round #274 (Div. 2) E:Riding in a Lift DP + 前缀优化
- Codeforces Round #274 (Div. 2)E. Riding in a Lift
- Codeforces Round #274 (Div. 1)——C. Riding in a Lift
- codeforce 479E - Riding in a Lift
- codeforces Riding in a Lift
- CF 274 div2 E - Riding in a Lift (递推)
- Codeforces Round #274 (Div. 2) Riding in a Lift(DP 前缀和)
- (Educational Codeforces Round 9)Thief in a Shop(dp)
- dp+Codeforces Round #274 (Div. 2)E
- Codeforces Round #129 (Div. 1) (各种好dp)
- Codeforces Round #266 (Div. 2)D(DP)
- Codeforces Round #264 (Div. 2)D(图论+dp)
- codeforces Diagrams & Tableaux1 (状压DP)
- Codeforces Round #267 (Div. 2) C. George and Job(DP)
- Codeforces Round #FF (Div. 2/C)/Codeforces446A_DZY Loves Sequences(DP)