题目链接: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的更多相关文章

  1. Codeforces 479E Riding in a Lift(dp)

    题目链接:Codeforces 479E Riding in a Lift 题目大意:有一栋高N层的楼,有个无聊的人在A层,他喜欢玩电梯,每次会做电梯到另外一层.可是这栋楼里有个秘 密实验室在B层,所 ...

  2. Codeforces 479E. Riding in a Lift (dp + 前缀和优化)

    题目链接:http://codeforces.com/contest/479/problem/E 题意:         给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...

  3. Codeforces 479E Riding in a Lift

    http://codeforces.com/problemset/problem/432/D 题目大意: 给出一栋n层的楼,初始在a层,b层不能去,每次走的距离必须小于当前位置到b的距离,问用电梯来回 ...

  4. 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 ...

  5. Codeforces 480C Riding in a Lift dp

    主题链接:点击打开链接 意甲冠军: 特定 n a b k 构造一个长度k该序列. 使得序列中 对于随意两个相邻的数 | w[i-1] - w[i] | < | w[i] - b | 且第一个数 ...

  6. Codeforces 946G Almost Increasing Array (树状数组优化DP)

    题目链接   Educational Codeforces Round 39 Problem G 题意  给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...

  7. Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...

  8. Codeforces 856D - Masha and Cactus(树链剖分优化 dp)

    题面传送门 题意: 给你一棵 \(n\) 个顶点的树和 \(m\) 条带权值的附加边 你要选择一些附加边加入原树中使其成为一个仙人掌(每个点最多属于 \(1\) 个简单环) 求你选择的附加边权值之和的 ...

  9. 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 ...

随机推荐

  1. 李洪强经典面试题39-iOS 程序员 6 级考试(答案和解释)

    iOS 程序员 6 级考试(答案和解释)   我是前言 1. 下面的代码分别输出什么? @implementation Son : Father- (id)init { self = [super i ...

  2. [译]GLUT教程 - 笔划字体

    Lighthouse3d.com >> GLUT Tutorial >> Fonts >> Stroke Fonts 笔划字体是用线条生成的.跟位图字体相反,笔划字 ...

  3. 使用第三方工具Cornerstone搭建本地SVNserver

    一.加入版本号资源库 点击Cornerstone左下角REPOSITORIES栏右边的加号button.在弹出的视图中选择File Repository,然后选择Create a New Reposi ...

  4. UVa 10651 Pebble Solitaire(DP 记忆化搜索)

    Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...

  5. 有关于__align(n) ,内存对齐

    __align __align 关键字指示编译器在 n 字节边界上对齐变量. __align 是一个存储类修饰符.它不影响函数的类型. 语法 __align(n) 其中: n 是对齐边界. 对于局部变 ...

  6. 网络流合集:bzoj1433,1934,1854 题解

    转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23992205 网络流/二分图大合集 [NO.1*原题] 1433: [ZJOI200 ...

  7. windows中wamp环境composer使用中openssl问题解决

    今天在windows下学习lavaral,使用composer update命令报如下错误: [Composer\Exception\NoSslException] The openssl exten ...

  8. Java泛型的应用

    一.泛型类 package generics; /** * 泛型类,格式:public class 类名<泛型类型1, ...> * @author zhongfg * @date 201 ...

  9. 统计输入的单词中有几个长度大于n的,n是自己指定的,用函数对象实现

    #ifndef COUNT_WORD_H #define COUNT_WORD_H #include <string.h> #include <iostream> #inclu ...

  10. DELL inspiron1420 linux下的wifi驱动安装

    首先确定无线网卡类型: lspci -vnn -d 14e4: 比如我的网卡类型为 04:00.0 Network controller [0280]:Broadcom Corporation BCM ...