Codeforces Round #274 (Div. 2) E. Riding in a Lift(DP)
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom
to top with integers from 1 to n.
Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has
a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a).
For another trip between floors you choose some floor with number y (y ≠ x)
and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to
the chosen y must be strictly less than the distance from the current floor x to
floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|.
After the lift successfully transports you to floor y, you write down number y in
your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift.
As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
The first line of the input contains four space-separated integers n, a, b, k (2 ≤ n ≤ 5000, 1 ≤ k ≤ 5000, 1 ≤ a, b ≤ n, a ≠ b).
Print a single integer — the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
5 2 4 1
2
5 2 4 2
2
5 3 4 1
0
题意:做电梯。刚開始的时候你在a层,不能到b层,每次你到新的地方的y,必须满足|x-y|<|x-b|,求坐k次有多少种可能
思路:比較easy想到的是dp[i][j]表示第i次到了j层的可能。分情况讨论。比如:当a<b的时候。下一次的层数i是不能超过j+(b-j-1)/2的,然后每次预先处理出前j层的可能。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int mod = 1000000007;
const int maxn = 5005; int n, a, b, k, dp[maxn][maxn];
int sum[maxn]; int main() {
scanf("%d%d%d%d", &n, &a, &b, &k);
memset(dp, 0, sizeof(dp));
if (a < b) {
dp[0][a] = 1;
for (int j = 1; j < b; j++)
sum[j] = sum[j-1] + dp[0][j];
for (int i = 1; i <= k; i++) {
for (int j = 1; j < b; j++)
dp[i][j] = (sum[(b-j-1)/2+j] - dp[i-1][j] + mod) % mod;
sum[0] = 0;
for (int j = 1; j < b; j++)
sum[j] = (sum[j-1] + dp[i][j]) % mod;
}
printf("%d\n", sum[b-1]);
}
else {
dp[0][a] = 1;
for (int j = n; j >= b+1; j--)
sum[j] = sum[j+1] + dp[0][j];
for (int i = 1; i <= k; i++) {
for (int j = b+1; j <= n; j++)
dp[i][j] = (sum[j-(j-b-1)/2] - dp[i-1][j] + mod) % mod;
sum[0] = 0;
for (int j = n; j >= b+1; j--)
sum[j] = (sum[j+1] + dp[i][j]) % mod;
}
printf("%d\n", sum[b+1]);
}
return 0;
}
Codeforces Round #274 (Div. 2) E. Riding in a Lift(DP)的更多相关文章
- 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.1 C Riding in a Lift --DP
题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种 ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
- Codeforces Round #356 (Div. 2) C. Bear and Prime 100(转)
C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)
题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)
http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...
- Codeforces Round #603 (Div. 2) C. Everyone is a Winner! (数学)
链接: https://codeforces.com/contest/1263/problem/C 题意: On the well-known testing system MathForces, a ...
- Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)
题目链接:https://codeforces.com/contest/1105/problem/D 题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 ...
- Codeforces Round #509 (Div. 2) F. Ray in the tube(思维)
题目链接:http://codeforces.com/contest/1041/problem/F 题意:给出一根无限长的管子,在二维坐标上表示为y1 <= y <= y2,其中 y1 上 ...
随机推荐
- C++关联容器<map>简单总结
C++关联容器<map>简单总结 map提供大小可变的关联容器,基于关联键值高效检索元素值.当你处理键值对的数据是,都可以考虑使用map关联容器. 特点: 大小可变的关联容器,基于关联键值 ...
- ajax调用webservice(二) 跨域。
所需工具与项目结构同(一). service.asmx中代码如下: using System; using System.Collections.Generic; using System.Web; ...
- (Spring加载xml时)org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");报错 在启动Spring时,报以下错 ...
- php获取mac用于网站绑定服务器
php获取mac用于网站绑定服务器 <?php class GetMacAddr{ var $return_array = array(); // 返回带有MAC地址的字串数组 var $mac ...
- String类扩展
String s1=new String("fsdfsd"); String s2=new String("fsdfsd"); String a1=" ...
- 详解boost库中的Message Queue .
Message Queue(后文简写成MQ或消息队列)是boost库中用来封装进程间通信的一种实现,同一台机器上的进程或线程可以通过消息队列来进行通迅.消息队列中的消息由优先级.消息长度.消息数据三部 ...
- epoll 中EPOLLIN 和 EPOLLOUT
epoll主要是事件回调运行的,我们使用socket的时候主要使用两个事件 EPOLLOUT事件:EPOLLOUT事件只有在连接时触发一次,表示可写,其他时候想要触发,那你要先准备好下面条件:1.某次 ...
- 【学习笔记】【oc】类的包装类 协议 category
1.类的两种包装类: 将基本数据包装成对象:NSValue:NSNumber; NSValue是NSNumber的父类, NSValue用来封装一些基本数据, NSValue是一个通用的包装类,用来包 ...
- 64位系统ADB
应该把ADB文件放在C:\Windows\SysWOW64目录下面,而不是System32下.
- MVC + LigerUI 做后台管理还真是清爽
LigerUI是基于Jquery,轻量级UI框架.具体可以看官方演示 http://www.ligerui.com/ 我的简单后台 模拟Winodw桌面效果,挺不错呢.最喜欢的还是他的,下拉列表绑定G ...