Codeforces 479E Riding in a Lift(dp)
题目链接: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)的更多相关文章
- Codeforces 479E. Riding in a Lift (dp + 前缀和优化)
题目链接:http://codeforces.com/contest/479/problem/E 题意: 给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...
- Codeforces 479E Riding in a Lift:前缀和/差分优化dp
题目链接:http://codeforces.com/problemset/problem/479/E 题意: 有一栋n层的房子. 还有一个无聊的人在玩电梯,每次玩电梯都会从某一层坐到另外一层. 他初 ...
- Codeforces 479E Riding in a Lift
http://codeforces.com/problemset/problem/432/D 题目大意: 给出一栋n层的楼,初始在a层,b层不能去,每次走的距离必须小于当前位置到b的距离,问用电梯来回 ...
- Codeforces 480C Riding in a Lift dp
主题链接:点击打开链接 意甲冠军: 特定 n a b k 构造一个长度k该序列. 使得序列中 对于随意两个相邻的数 | w[i-1] - w[i] | < | w[i] - b | 且第一个数 ...
- 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 ...
- Codeforces Round #274 Div.1 C Riding in a Lift --DP
题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 【Android开发经验】使用反射,得到的类的字段、方法、并实现了简单的调用
本文后推出Android的ICO框架做准备,所以,假设你想要一个最近的一项研究Android的ICO学生框架.你可以稍微看一下. 首先,简介一下Java里面的反射. JAVA反射机制是在执行状态中,对 ...
- 重新想象 Windows 8 Store Apps (18) - 绘图: Shape, Path, Stroke, Brush
原文:重新想象 Windows 8 Store Apps (18) - 绘图: Shape, Path, Stroke, Brush [源码下载] 重新想象 Windows 8 Store Apps ...
- effective c++ 条款4 make sure that objects are initialized before they are used
1 c++ 类的数据成员的初始化发生在构造函数前 class InitialData { public: int data1; int data2; InitialData(int a, int b) ...
- Linux下/proc目录简介(转)
1. /proc目录Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文 ...
- 《TCP/IP详细解释》札记(23章)-TCP该保活定时器
可能有这样的备用现实TCP连接:流通过. 也就是说.假设TCP连接的两方都没有向对方发送数据.则在两个TCP模块之间不交换不论什么信息,这意味着我们能够启动一个客户与server建立连接,然后长时间不 ...
- List、Map和Set实现类
List.Map和Set实现类 1.List实现类 (1)ArrayList (2)Vector 2.Map实现类 (1)HashMap (2)Hashtable 3.Set实现类 (1)HashSe ...
- HPUX在oracle10g安装和卸载缩写
创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/ ...
- shell script 入门 笔记
shell script 入门 在 shell script 注意必须使用完全相同写在下面: 1. 指令的运行是从上而下.从左而右的分析与运行: 2. 指令的运行就如同第五章内提到的: 指令.选项 ...
- XUtils骨架HttpUtils采用Get总是返回请求解决问题的相同信息
如需转载请注明出处:http://blog.csdn.net/itas109 版本号:Xutils 2014年11月11日 下载地址:https://github.com/wyouflf/xUtils ...
- jquery声明
$("[id^=total_item]")代表 id随着total_item开始XX..必须jquery支持 版权声明:本文博主原创文章,博客,未经同意,不得转载.