Codeforces 479E Riding in a Lift:前缀和/差分优化dp
题目链接:http://codeforces.com/problemset/problem/479/E
题意:
有一栋n层的房子。
还有一个无聊的人在玩电梯,每次玩电梯都会从某一层坐到另外一层。
他初始在a层,然后要玩k次电梯。
这栋楼里还有一个神秘实验室,在b层。
这让他每次坐电梯受到了限制:
当前在x层,然后要坐到y层,则必须满足|x-y|<|x-b|
问你共有多少种坐电梯的方案。
题解:
表示状态:
dp[i][j] = numbers
表示当前在第i层,已经坐了j次电梯,此时的方案数。
找出答案:
ans = ∑ dp[i][k]
如何转移:
若当前在第i层,则移动距离最大为r = |i-b|-1
dp[i-r to i-1][j+1] += dp[i][j]
dp[i+1 to i+r][j+1] += dp[i][j]
注意判断越界。
边界条件:
set dp = 0
dp[a][0] = 1
前缀和/差分优化:
如果直接去做的话,枚举状态要O(N^2),转移要O(N),总复杂度O(N^3)明显超了。
所以考虑将转移变成O(1)的。
因为转移是给一段区间加上同一个值,所以可以用差分去做,转移完之后在求一边前缀和就变成了原值。
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 5005
#define MAX_K 5005
#define MOD 1000000007 using namespace std; int n,a,b,k;
int dp[MAX_N][MAX_K]; inline int abs(int x)
{
return x> ? x : -x;
} inline int mod(int x)
{
return (x%MOD+MOD)%MOD;
} void sec(int x,int y,int v,int id)
{
if(x>y || y<= || x>n) return;
x=max(x,); x=min(x,n);
y=max(y,); y=min(y,n);
dp[x][id]=mod(dp[x][id]+v);
dp[y+][id]=mod(dp[y+][id]-v);
} int main()
{
cin>>n>>a>>b>>k;
memset(dp,,sizeof(dp));
dp[a][]=;
for(int j=;j<k;j++)
{
for(int i=;i<=n;i++)
{
int r=abs(i-b)-;
sec(i-r,i-,dp[i][j],j+);
sec(i+,i+r,dp[i][j],j+);
}
for(int i=;i<=n;i++)
{
dp[i][j+]=mod(dp[i][j+]+dp[i-][j+]);
}
}
int ans=;
for(int i=;i<=n;i++) ans=mod(ans+dp[i][k]);
cout<<ans<<endl;
}
Codeforces 479E Riding in a Lift:前缀和/差分优化dp的更多相关文章
- Codeforces 479E Riding in a Lift(dp)
题目链接:Codeforces 479E Riding in a Lift 题目大意:有一栋高N层的楼,有个无聊的人在A层,他喜欢玩电梯,每次会做电梯到另外一层.可是这栋楼里有个秘 密实验室在B层,所 ...
- Codeforces 479E. Riding in a Lift (dp + 前缀和优化)
题目链接:http://codeforces.com/contest/479/problem/E 题意: 给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...
- Codeforces 479E Riding in a Lift
http://codeforces.com/problemset/problem/432/D 题目大意: 给出一栋n层的楼,初始在a层,b层不能去,每次走的距离必须小于当前位置到b的距离,问用电梯来回 ...
- 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 480C Riding in a Lift dp
主题链接:点击打开链接 意甲冠军: 特定 n a b k 构造一个长度k该序列. 使得序列中 对于随意两个相邻的数 | w[i-1] - w[i] | < | w[i] - b | 且第一个数 ...
- Codeforces 946G Almost Increasing Array (树状数组优化DP)
题目链接 Educational Codeforces Round 39 Problem G 题意 给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...
- Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)
Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...
- Codeforces 856D - Masha and Cactus(树链剖分优化 dp)
题面传送门 题意: 给你一棵 \(n\) 个顶点的树和 \(m\) 条带权值的附加边 你要选择一些附加边加入原树中使其成为一个仙人掌(每个点最多属于 \(1\) 个简单环) 求你选择的附加边权值之和的 ...
- Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
随机推荐
- JavaScript跨浏览器事件处理
var EventUtil = { getEvent: function(event){ return event ? event : window.event; }, getTarget: func ...
- Photoshop中磁力套索的一种简陋实现(基于Python)
经常用Photoshop的人应该熟悉磁力套索(Magnetic Lasso)这个功能,就是人为引导下的抠图辅助工具.在研发领域一般不这么叫,通常管这种边缘提取的办法叫Intelligent Sciss ...
- 《Python基础教程》第20章学习笔记
python实现:https://github.com/captainwong/instant_markup c++实现:https://github.com/captainwong/instant_ ...
- Elasticsearch5.X IN Windows 10 系列文章(1)
系统类型:windows10 64位家庭版 ElasticSearch版本: 5.5.1 (最新稳定版为5.5.2),由于用到IK中文分词插件,最新版本没有5.5.2 ,所以使用5.5.1 日期:2 ...
- laravel学习之路4artisan
php artisan list php artisan help migrate Tinker 让你可以在命令行中与 Laravel 应用进行交互php artisan tinker 在routes ...
- 苹果mac shell 终端 命令行快捷键——行首行尾
ctrl+a //移到行首 ctrl+e //移到行尾 http://blog.csdn.net/hherima/article/details/47083739
- 使用 cacti 批量监控服务器以及其 PHP 运作环境配置
http://www.ibm.com/developerworks/cn/linux/l-cn-cacti/ http://www.360doc.com/content/12/0711/22/1465 ...
- 06 Memcached中的一些参数限制
一: Memcached中的一些参数限制 Key的长度:250字节(二进制协议支持65536个字节) value的限制:1M ,一般都是存储一些文本,如新闻标题等等这个值足够了, 内存的限制:32位下 ...
- Tomcat startup.bat启动隐藏弹出的信息窗口
to make tomcat to use javaw.exe instead of java.exe using some startup parameter or environment vari ...
- 批处理--复制,解压文件,goto,nul
rem 复制文件 copy "D:\xxxx" "C:\xxxx" rem 复制文件夹 xcopy "D:\xxxx" "C:\x ...