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 ...
随机推荐
- 3Dmax+blend+WPF综合运用
原文:3Dmax+blend+WPF综合运用 赛后总结 本人小菜,WPF刚入门,只是写一下最近的项目心得.欢迎各位前辈们前来拍砖指正,感激不敬!先申明,小弟我入门仓促,很多东西也是一知半解,所以很多问 ...
- Python 清理HTML标签相似PHP的strip_tags函数功能(二)
没有发现Python 有现成的类似功能模块,所以昨天写了个简单的 strip_tags 但还有些问题,今天应用到採集上时进行了部分功能的完好, 1. 对自闭和标签处理 2. 以及对标签參数的过滤 fr ...
- TRIZ系列-创新原理-22-变害为利原理
变害为利原理的详细表述例如以下:1)利用有害的因素(特别是环境中的)获得积极的效果: 有害无害不过相对的(时间,空间,人),将有害的因素通过一定的处理和转化,能够变有害为实用,比方废品回收, ...
- (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)
称号: You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...
- 使用WPF创建无边框窗体
一.无边框窗口添加窗口阴影 实际上在WPF中添加无边框窗口的窗口阴影十分简单. 首先,设置WindowStyle="None"以及AllowsTransparency=" ...
- UVA580-Critical Mass
题目链接 题意:一个栈中仅仅能放入U和L,问存在连续3个以上U(危急组合)的个数为几个 思路:用总组合数-安全组合=危急组合.d[i]表示第i个位置以L结束的序列,所以就有d[i] = d[i - 1 ...
- 学习pthreads,创建和终止多线程
更CPU多线程编程,通过笔者的研究发现,,pthreads使用日趋广泛.它是螺纹POSIX标准,它定义了一组线程的创建和操作API. 配置环境见上博客文章.配置环境后,只需要加入#include &l ...
- 分布式服务框架 dubbo/dubbox 入门示例(转)
dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架. 官网首页:http://dubbo.io/ ,官方用户指南 http://dubbo.io/User+Guide-zh.htm ...
- 从苹果系统InstallESD.dmg里提取IOS
右键下载的Mac OS X Mountain Lion镜像:InstallESD.dmg,选择7-zip------打开压缩包 2.双击InstallMacOSX.pkg 3.选中InstallESD ...
- UVA No Tipping
Problem A - No Tipping As Archimedes famously observed, if you put an object on a lever arm,it will ...