Codeforces Round #274 Riding in a Lift (DP)
2016-07-12 20:12
351 查看
C. Riding in a Lift
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 kconsecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floora).
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).
Input
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).
Output
Print a single integer — the remainder after dividing the sought number of sequences by1000000007 (109 + 7).
Examples
input
output
input
output
input
output
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct,
if there is such integer j (1 ≤ j ≤ k),
that pj ≠ qj.
Notes to the samples:
In the first sample after the first trip you are either on floor 1, or on floor 3,
because|1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
In the second sample there are two possible sequences: (1, 2); (1, 3).
You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
一开始在a层,对于每次坐电梯,x为当前所在层,y为目标层,除了b层以外其他均可以去,去时要满足|x - y| < |x - b|。需要移动k次,一共有多少不同的路径
对于每一个状态dp[i][j],可以发现他的值就是上一次坐电梯时能到这一层的方案的总和,例如现在在4,b=6,则现在能去5和3,那么下一次考虑的时候,dp[i+1][5]和dp[i+1][3]就要累加上dp[i][4]具有的情况,因为下一层的5和3是可以由这一次在4层的状态转移过来的。
清楚这一点,可以很容易写出下面的代码:
即每次都把当前状态更新到下一行去,但是这样的时间为O(n^3),超时
经过观察我们发现每次的状态其实对应的是上一层的一段连续区间,所以可以用前缀和来优化掉一维的DP
先举个例子吧
b=6,n=12对于第i+1次的1-n层,可能由第i次坐电梯到达的情况如下(很明显不能坐到同一层):
1:23
2:1
3
3:1 2 4
4:1 2 3
5:1 2 3 4
6:
7:8 9 10 11 12
8:9 10 11 12
9:8 10 11 12
10:9 11 12
11:9 10 12
12:10 11
可以发现,
对于小于b的楼层j,其区间是 1到(j+b-1)/2,
[b]对于小于b的楼层j,其区间是(j+b)/2+1到n[/b]
[b]给代码加入前缀和如下[/b]
时间已经被压到O(n^2)了,空间还可以用滚动数组进行优化
[b]嗯,就是这样~~[/b]
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 kconsecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floora).
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).
Input
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).
Output
Print a single integer — the remainder after dividing the sought number of sequences by1000000007 (109 + 7).
Examples
input
5 2 4 1
output
2
input
5 2 4 2
output
2
input
5 3 4 1
output
0
Note
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct,
if there is such integer j (1 ≤ j ≤ k),
that pj ≠ qj.
Notes to the samples:
In the first sample after the first trip you are either on floor 1, or on floor 3,
because|1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
In the second sample there are two possible sequences: (1, 2); (1, 3).
You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
题意:
一开始在a层,对于每次坐电梯,x为当前所在层,y为目标层,除了b层以外其他均可以去,去时要满足|x - y| < |x - b|。需要移动k次,一共有多少不同的路径思路1:
用dp[i][j]表示坐了i次电梯,现在在j层的方案数目,则初始状态为dp[0][a],终止状态为dp[k][v] (v表示不为b的所有数),答案就是sum(dp[i][v])对于每一个状态dp[i][j],可以发现他的值就是上一次坐电梯时能到这一层的方案的总和,例如现在在4,b=6,则现在能去5和3,那么下一次考虑的时候,dp[i+1][5]和dp[i+1][3]就要累加上dp[i][4]具有的情况,因为下一层的5和3是可以由这一次在4层的状态转移过来的。
清楚这一点,可以很容易写出下面的代码:
#include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define rep(i,a,b) for(int i=a;i<b;i++) #define debug(a) printf("a =: %d\n",a); const int INF=0x3f3f3f3f; const int maxn=5e3+50; const int Mod=1e9+7; typedef long long ll; using namespace std; int n,a,b,k; int dp[maxn][maxn]; int sum[maxn][maxn]; int solve(){ mem(dp,0); mem(sum,0); dp[0][a]=1; for (int i=a;i<=n;i++) sum[0][i]=1; for (int i=0;i<=k;i++){ for(int j=1;j<=n;j++){ int l=max(1,j-abs(j-b)+1),r=min(j+abs(j-b)-1,n); for (int p=l;p<=r;p++) if (p==j) continue; else dp[i+1][p]=(dp[i+1][p]+dp[i][j])%Mod; } } int ret=0; for (int i=1;i<=n;i++){ if (i==b) continue; ret=(ret+dp[k][i]); if (ret>Mod) ret-=Mod; } return ret; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); #endif while(scanf("%d %d %d %d",&n,&a,&b,&k)!=EOF){ printf("%d\n",solve()); } return 0; }
即每次都把当前状态更新到下一行去,但是这样的时间为O(n^3),超时
经过观察我们发现每次的状态其实对应的是上一层的一段连续区间,所以可以用前缀和来优化掉一维的DP
先举个例子吧
b=6,n=12对于第i+1次的1-n层,可能由第i次坐电梯到达的情况如下(很明显不能坐到同一层):
1:23
2:1
3
3:1 2 4
4:1 2 3
5:1 2 3 4
6:
7:8 9 10 11 12
8:9 10 11 12
9:8 10 11 12
10:9 11 12
11:9 10 12
12:10 11
可以发现,
对于小于b的楼层j,其区间是 1到(j+b-1)/2,
[b]对于小于b的楼层j,其区间是(j+b)/2+1到n[/b]
[b]给代码加入前缀和如下[/b]
#include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define rep(i,a,b) for(int i=a;i<b;i++) #define debug(a) printf("a =: %d\n",a); const int INF=0x3f3f3f3f; const int maxn=5e3+50; const int Mod=1e9+7; typedef long long ll; using namespace std; int n,a,b,k; int dp[maxn][maxn]; int sum[maxn][maxn]; inline int getSum(int id,int l,int r,int mid){ int ret=0; ret=(sum[id][r]-sum[id][l-1]+Mod)%Mod; ret=(ret+Mod-dp[id][mid])%Mod; return ret; } int solve(){ mem(dp,0); mem(sum,0); dp[0][a]=1; for (int i=a;i<=n;i++) sum[0][i]=1; for (int i=1;i<=k;i++){ for(int j=1;j<=n;j++){ if (j<b) dp[i][j]=getSum(i-1,1,(j+b-1)/2,j); else if (j>b) dp[i][j]=getSum(i-1,(j+b)/2+1,n,j); sum[i][j]=(sum[i][j-1]+dp[i][j])%Mod; // printf("i %d j %d dp %d \n",i,j,dp[i][j]); } // puts(""); } return sum[k] ; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); #endif while(scanf("%d %d %d %d",&n,&a,&b,&k)!=EOF){ printf("%d\n",solve()); } return 0; }
时间已经被压到O(n^2)了,空间还可以用滚动数组进行优化
[b]嗯,就是这样~~[/b]
相关文章推荐
- 位移上下反复动画
- POJ 3616 Milking Time(基础DP)
- 不同应用程序之间的跳转(内容提供者)
- 教你移植Zxing二维码,兼容Android6.0
- 51 NOD 1685 第K大区间2 二分+BIT
- POI处理PPT的表格table,XSLFTable样式设置
- FrameBuffer详解
- Android四大组件之一:Activity的启动模式
- Unity位置动画
- 线段树 单点增减,单点替换,区间最值,区间求和(模板)
- c++ 开发JNI
- POI实现生成excel文件
- 我的网易博客
- nginx专题
- 控制Storyboard播放zz
- NOIP2013 day2
- iso oc登录引导页
- EditText的基本用法
- opencv2.4.9+vs2010+win64下的配置
- Git8--添加远程库