codeforces 480C C. Riding in a Lift(dp)
题目链接:
2 seconds
256 megabytes
standard input
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 numberb 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 题意: 有n层楼,不能去b层,一开始在a层,每次与要选的楼层的距离只能比去b层的小,现在问走k步的方案数是多少; 思路: dp[i][j]表示走了i次,第i次在j层的方案数,这样转移很简单,但是复杂度太高,是O(k*n*n);
可以发现在枚举下一次的层数的时候更新是更新一段的,用线段树啥的还有个log的复杂度,我们可以把dp[i][j]分解成s[i][j]-s[i][j-1];
那么dp[i][j]就是s的前缀和了;好神奇啊;学习到了; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const int mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e6+20;
const int maxn=5e3+5;
const double eps=1e-12; int n,a,b,k;
int dp[maxn][maxn]; inline void solve(int d,int l,int r,int val)
{
if(l>r)return ;
dp[d][l]+=val;
if(dp[d][l]>=mod)dp[d][l]-=mod;
dp[d][r+1]-=val;
if(dp[d][r+1]<0)dp[d][r+1]+=mod;
}
int main()
{
read(n);read(a);read(b);read(k);
for(int i=0;i<=n;i++)for(int j=0;j<=k;j++)dp[i][j]=0;
int len=abs(b-a)-1;
int l=max(1,a-len),r=min(n,a+len);
for(int i=l;i<=r;i++)if(i!=b&&i!=a)dp[1][i]=1;
for(int i=n;i>0;i--)
{
dp[1][i]=dp[1][i]-dp[1][i-1];
if(dp[1][i]<0)dp[1][i]+=mod;
}
for(int i=1;i<k;i++)
{
int sum=0;
for(int j=1;j<=n;j++)
{
sum=sum+dp[i][j];
if(sum>=mod)sum-=mod;
if(j==b)continue;
len=abs(j-b)-1;
l=max(1,j-len);r=min(n,j+len);
solve(i+1,l,j-1,sum);
solve(i+1,j+1,r,sum);
}
}
int ans=0,sum=0;
for(int i=1;i<=n;i++)
{
sum+=dp[k][i];
if(sum>=mod)sum-=mod;
ans+=sum;
if(ans>=mod)ans-=mod;
}
cout<<ans<<"\n";
return 0;
}
codeforces 480C C. Riding in a Lift(dp)的更多相关文章
- Codeforces 479E Riding in a Lift(dp)
题目链接:Codeforces 479E Riding in a Lift 题目大意:有一栋高N层的楼,有个无聊的人在A层,他喜欢玩电梯,每次会做电梯到另外一层.可是这栋楼里有个秘 密实验室在B层,所 ...
- Codeforces 480C Riding in a Lift dp
主题链接:点击打开链接 意甲冠军: 特定 n a b k 构造一个长度k该序列. 使得序列中 对于随意两个相邻的数 | w[i-1] - w[i] | < | w[i] - b | 且第一个数 ...
- Codeforces 479E. Riding in a Lift (dp + 前缀和优化)
题目链接:http://codeforces.com/contest/479/problem/E 题意: 给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...
- 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 ...
- cf479E Riding in a Lift
E. Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
随机推荐
- windows临界区
临界区: 临界区是一种轻量级机制,在某一时间内只允许一个线程执行某个给定代码段.通常在多线程修改全局数据时会使用临界区.事件.信号量也用于多线程同步,但临界区与它们不同,并不总是执行向内核模式的切换, ...
- 函数改变全局变量-JS
切记,一定按三步走: 1. 全局变量声明 2. 函数声明 3. 函数调用 正确做法: var dataStr = null; function remoteCallback(data) { dataS ...
- .NET破解之太乐地图下载器【非暴破】
不知不觉,接触破解逆向已经三个月了,从当初的门外汉到现在的小白,这个过程只有经历过才知道其中的苦与乐: 有无知.困惑.痛苦.惊喜.彻悟.欣慰…… 有无助的软件脱壳,茫然的代码分析,有无趣的反复测试, ...
- 高清SDI编码器|上海视涛科技
SDI编码器(E500)简介 SDI编码器(E500)是上海视涛科技出品的高性能SDI编码产品.该SDI编码器是上海视涛电子完全自主研发,并适用于各种SDI信号的编码采集及网络传输的专用硬件设备.可兼 ...
- EL表达式概述
E L(Expression Language) 目的:为了使JSP写起来更加简单.表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方法. ...
- C++类模板
在上篇文章(C++函数模板)中,主要介绍了C++中函数模板,与函数相似,类也可以被一种或多种类型参数化.容器类就是一个具有这种特性的典型的例子, 本文地址:http://www.cnblogs.com ...
- Android中使用抖动动画吸引来用户注意
原文:http://www.androidcn.org/topic/552e65bc61d460226ab27a5c 在应用中,有时候我们要吸引用户去点击某些按钮,比如应用市场的推荐按钮,为了能够吸引 ...
- CAS实现单点登入(sso)经典教程
本教程我已按照步骤实现,不过要深入了解单点登入还需要进一步的学习,掌握其中的精髓. 一.简介 1.cas是有耶鲁大学研发的单点登录服务器 2.本教材所用环境 Tomcat7.2 JDK6 CAS Se ...
- Android bitmap高效显示和优化
第一部分:Bitmap高效显示 应用场景:有时候我们想在界面上显示一个网络图片或者显示一张本地的图片,但是图片本身是很大的有几兆,但是显示的位置很小或者说我们可以用更小的图片来满足这样的需求,如果把整 ...
- 彻底退出所有的Acticity
有时候点击回退键退出应用,会出现有些Activity不能完全退出的情况,那么可以使用前面这个方法: 在需要退出的Activity的onCreate()方法中加入 ExitApplication.get ...