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 ...
随机推荐
- IE浏览器下,输入框最后不显示X
IE浏览器下,输入框最后不显示X.设置: .login-input::-ms-clear { display: none; } .login-input::-ms-reveal { display: ...
- 《TomCat与Java Web开发技术详解》(第二版) 第四章节的学习总结--常用Servlet API
要开发Servlet,自然要掌握常用的servlet的相关API.通过此章节的学习,了解到如下常用API 1.Servlet接口--->GenericServlet抽象类(实现Servlet接口 ...
- UserScan的处理流程分析
UserScan的处理流程分析 前置说明 Userscan是通过client或cp中发起的scanner操作. 在Scan中通过caching属性来返回能够返回多少条数据.每次进行next时. 通过b ...
- 使用Percona监控插件监控MySQL
1.使用Percona监控插件监控MySQL yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/per ...
- PYTHON测试邮件系统弱密码
#-*- coding:utf-8 -*- #测试公司邮件系统弱密码, from email.mime.text import MIMEText import smtplib #弱密码字典 passL ...
- Selenium 应用 WebDriverWait 和 expected_conditions(待验证)
收藏在我的收藏看不到,只能copy了,转载至http://www.cnblogs.com/yicaifeitian/p/4749149.html 哈哈,我始终相信贴出来总会有人看.WebDriverW ...
- dubbo开发中使用到的一些服务配置方式
通过之前的学习了解了dubbo的常规的使用,下面我们看看特殊情况或者说真实环境下使用dubbo的一些配置实例. 一.一个接口有多个实现时可以使用group来区分 1.服务提供者配置 <?xml ...
- iOS GPUImage 滤镜介绍
这里直接引用官方描述: The GPUImage framework is a BSD-licensed iOS library that lets you apply GPU-accelerated ...
- 【Robot Framework】---- Robot Framework简介、特点、RIDE
Robot Framework简介.特点.RIDE 一.简介.特点. Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型 ...
- cocos2dx使用cocostudio导出的animation
local uilocal function createLayerUI() if not ui then ui=cc.Layer:create(); createLayerUI=nil; end r ...