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/problem/C
Description
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).
Input
Output
Sample Input
5 2 4 1
Sample Output
2
HINT
题意
有n层楼,你一开始在a层楼,实验室在b层楼,然后你可以瞎走k次
每次只要满足|now-next|<|now-b|就可以从now走到next去
然后问你一共有多少种走法
题解:
最简单就是dp[i][j]表示我现在在第i层,瞎走了j次的方案数,但是这个转移是n^3的
我们得优化一下
每次我们可以看到,从上一个状态转移过来的是一个区间,所以大概用一个线段树优化成n^2logn或者用前缀和优化成n^2的就行了
代码:
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
#define maxn 5005
#define mod 1000000007
int dp[maxn][maxn];
int sum[maxn];
int main()
{
int n,a,b,k;
scanf("%d%d%d%d",&n,&a,&b,&k);
dp[a][]=;
for(int i=;i<=n;i++)
sum[i]=dp[i][]+sum[i-];
for(int i=;i<=k;i++)
{
for(int j=;j<=n;j++)
{
if(j==b)continue;
int L,R;
if(j<b)
{
L = ,R = (j+b)/;
if(R-j==b-R)R--;
}
else
{
L = (j+b)/+(j+b)%;R=n;
if(j-L==L-b)L++;
}
L=max(,L);R=min(n,R);
dp[j][i]=((sum[R]-sum[L-]+mod)%mod-dp[j][i-]+mod)%mod;
}
for(int j=;j<=n;j++)
{
sum[j]=dp[j][i]+sum[j-];
while(sum[j]>=mod)sum[j]-=mod;
}
}
printf("%d\n",sum[n]);
}
Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp的更多相关文章
- 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. 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 l ...
- 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 ...
- Codeforces Round #274 (Div. 2)
A http://codeforces.com/contest/479/problem/A 枚举情况 #include<cstdio> #include<algorithm> ...
- Codeforces Round #274 (Div. 1) B. Long Jumps 数学
B. Long Jumps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/ ...
- Codeforces Round #274 (Div. 1) A. Exams 贪心
A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...
- codeforces水题100道 第八题 Codeforces Round #274 (Div. 2) A. Expression (math)
题目链接:http://www.codeforces.com/problemset/problem/479/A题意:给你三个数a,b,c,使用+,*,()使得表达式的值最大.C++代码: #inclu ...
- Codeforces Round #274 (Div. 2)-C. Exams
http://codeforces.com/contest/479/problem/C C. Exams time limit per test 1 second memory limit per t ...
- Codeforces Round #274 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/479 这次自己又仅仅能做出4道题来. A题:Expression 水题. 枚举六种情况求最大值就可以. 代码例如以下: #inc ...
随机推荐
- 【c++内存分布系列】单继承
父类包括成员函数.静态函数.静态方法,子类包括成员函数.静态函数.静态方法的情况与一个类时完全一致,这里就不做分析了.子类单独包含虚函数时继承无关,也不做分析了. 一.父类子类都为空 #include ...
- 【管理工具】Git的安装与使用
公司与公司合并,需要学习一下git的使用.从网上找了一篇资料,完全满足需求,先赞一个. http://www.cnblogs.com/Bonker/p/3441781.html 下面记录一下自己的安装 ...
- 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(5)
数据操作与REST API(Data manipulation with REST API) ElasticSearch REST API可用于各种任务.多亏了它,我们可以管理索引,更改实例参数,检查 ...
- Headmaster's Headache
题意: s门课程,现任老师有m个给出工资,和他们能教的课,现在有n个应聘的老师,给出费用和能教的课程标号,求使每门课都至少有两个老师教的最小花费 分析: n个老师选或不选有背包的特征,n很小想到用状压 ...
- Apache OFBiz 学习笔记 之 服务引擎 二
加载服务定义文件 ofbiz-component.xml:所有的服务定义文件在每个组件的ofbi-component.xml文件中 加载服务定义 例:framework/common/ofbi ...
- 博客测试:博客系统i94web beta1.0 请求测试
最近博客没怎么更新了,因为一直在撸代码,自己写了一个小小的博客系统:i94web,匆忙发布beta1.0,请求各位测试各种漏洞. 先看几张截图. 首页: 边栏: 文章页: 后台发布: 测试地址:htt ...
- MFC特定函数的应用20160720(SystemParametersInfo,GetWindowRect,WriteProfileString,GetSystemMetrics)
1.SystemParametersInfo函数可以获取和设置数量众多的windows系统参数 MFC中可以用 SystemParametersInfo(……) 函数来获取和设置系统信息,如下面例子所 ...
- 如何在安裝SELinux的环境执行Quartus II
(原創) 如何在安裝SELinux的環境執行Quartus II? (SOC) (Quartus II) (Linux) (RedHat) Abstract一般人安裝Linux時,也會同時安裝SELi ...
- FMX架构图
- WINRAR评估版本弹出框消除
网上有很多WINRAR评估版本,这些版本下载安装了之后总会有些广告弹出,让人很烦恼,现在教大家一个方法消除这些弹出框. 复制以下代码: RAR registration data SeVeN U ...