题目链接:Codeforces 479E Riding in a Lift

题目大意:有一栋高N层的楼,有个无聊的人在A层,他喜欢玩电梯,每次会做电梯到另外一层。可是这栋楼里有个秘

密实验室在B层,所以每次他移动的时候就有了一个限制,x为当前所在层,y为目标层,|x - y| < |x - b|。问说移动K次

后,有多少不同的路径。

解题思路:dp[i][j]表示在第i步到达j层有多少种不同的路径,dis = abs(j-B) - 1,那么在[j-dis,j+dis]这个范围都能被转

移,除了j。那么转移方程就非常好写了。

可是直接转移的话复杂度有点高,由于转移的范围是成段的,所以我们能够利用数组维护区间和的方式取优化。每次对

一个区间l,r加上某个值v的时候,等于在l处+v。r+1处-v。最后处理的时候每一个位置的准确值即为数组的前缀和。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int maxn = 5005;
const int mod = 1e9 + 7; int N, A, B, K, dp[maxn][maxn]; void add (int idx, int l, int r, int d) {
dp[idx][l] = (dp[idx][l] + d) % mod;
dp[idx][r] = (dp[idx][r] - d + mod) % mod;
} int main () {
scanf("%d%d%d%d", &N, &A, &B, &K); dp[0][A] = 1;
for (int i = 0; i < K; i++) {
for (int j = 1; j <= N; j++) {
if (j == B) continue;
int x = abs(j - B) - 1;
add(i+1, max(j-x, 1), j, dp[i][j]);
add(i+1, j+1, min(j+x+1, N + 1), dp[i][j]);
} int mv = 0;
for (int j = 1; j <= N; j++) {
mv = (mv + dp[i+1][j]) % mod;
dp[i+1][j] = mv;
}
} int ans = 0;
for (int i = 1; i <= N; i++)
ans = (ans + dp[K][i]) % mod;
printf("%d\n", ans);
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

Codeforces 479E Riding in a Lift(dp)的更多相关文章

  1. Codeforces 479E. Riding in a Lift (dp + 前缀和优化)

    题目链接:http://codeforces.com/contest/479/problem/E 题意:         给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...

  2. Codeforces 479E Riding in a Lift:前缀和/差分优化dp

    题目链接:http://codeforces.com/problemset/problem/479/E 题意: 有一栋n层的房子. 还有一个无聊的人在玩电梯,每次玩电梯都会从某一层坐到另外一层. 他初 ...

  3. Codeforces 479E Riding in a Lift

    http://codeforces.com/problemset/problem/432/D 题目大意: 给出一栋n层的楼,初始在a层,b层不能去,每次走的距离必须小于当前位置到b的距离,问用电梯来回 ...

  4. Codeforces 480C Riding in a Lift dp

    主题链接:点击打开链接 意甲冠军: 特定 n a b k 构造一个长度k该序列. 使得序列中 对于随意两个相邻的数 | w[i-1] - w[i] | < | w[i] - b | 且第一个数 ...

  5. codeforces 480C C. Riding in a Lift(dp)

    题目链接: C. Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. Codeforces Round #274 Div.1 C Riding in a Lift --DP

    题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种 ...

  7. Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp

    C. Riding in a Lift Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/pr ...

  8. Codeforces Round #274 (Div. 2) Riding in a Lift(DP 前缀和)

    Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. E. Riding in a Lift(Codeforces Round #274)

    E. Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. Quick Tip: How to Add Syntax Highlighting to Any Project

    Quick Tip: How to Add Syntax Highlighting to Any Projectpublic String showAllArticleForPage() throws ...

  2. GUIForDebug

    package gui; import org.luaj.vm2.Globals; import org.luaj.vm2.LuaValue; import org.luaj.vm2.ast.Chun ...

  3. Java Swing 绝对布局管理方法,null布局(转)

    首先把相关容器的布局方式设为 setLayout(null); 然后调用组件的  setBounds() 方法 设置button的位置为(100,100) 长宽分别为 60,25 jButton.se ...

  4. 将EBS设为首页worklist删除误报

    参考:How To Remove Error Notifications From The Worklist (Doc ID 357904.1) 1.1. Use one of the error n ...

  5. UVA 538 - Balancing Bank Accounts(贪心)

    UVA 538 - Balancing Bank Accounts 题目链接 题意:给定一些人的欠钱关系,要求在n-1次内还清钱,问方案 思路:贪心,处理出每一个人最后钱的状态,然后直接每一个人都和最 ...

  6. docker-gitlab(转)

    Issues Docker is a relatively new project and is active being developed and tested by a thriving com ...

  7. HDU 4513 哥几个系列故事——形成完善II manacher求最长回文

    标题来源:哥几个系列故事--形成完善II 意甲冠军:中国 思维:在manacher断 保证非严格递减即可了 #include <cstdio> #include <cstring&g ...

  8. java.io.NotSerializableException

    结果发现序列不成功非静态内部类时的序列中,出现以下异常: java.io.NotSerializableException: com.tang.sharedpreferencesdemo.MainAc ...

  9. DWR入门实例(二)

    DWR(Direct Web Remoting) DWR is a Java library that enables Java on the server and JavaScript in a b ...

  10. 房费制 它 结账BUG

    声明:以下内容仅仅是对在桌子上的卡与卡表的后面,适合学生的表!     最近,我们已经开始做VB.NET系统重构版,在这里跟大家聊聊我在机房收费系统中发现的漏洞. 在机房收费系统中有这样一个窗口--结 ...