题目链接:Codeforces 479E Riding in a Lift

题目大意:有一栋高N层的楼,有个无聊的人在A层,他喜欢玩电梯,每次会做电梯到另外一层。可是这栋楼里有个秘

密实验室在B层,所以每次他移动的时候就有了一个限制,x为当前所在层,y为目标层,|x - y| < |x - b|。问说移动K次

后,有多少不同的路径。

解题思路:dp[i][j]表示在第i步到达j层有多少种不同的路径,dis = abs(j-B) - 1,那么在[j-dis,j+dis]这个范围都能被转

移,除了j。那么转移方程就非常好写了。

可是直接转移的话复杂度有点高,由于转移的范围是成段的,所以我们能够利用数组维护区间和的方式取优化。每次对

一个区间l,r加上某个值v的时候,等于在l处+v。r+1处-v。最后处理的时候每一个位置的准确值即为数组的前缀和。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int maxn = 5005;
const int mod = 1e9 + 7; int N, A, B, K, dp[maxn][maxn]; void add (int idx, int l, int r, int d) {
dp[idx][l] = (dp[idx][l] + d) % mod;
dp[idx][r] = (dp[idx][r] - d + mod) % mod;
} int main () {
scanf("%d%d%d%d", &N, &A, &B, &K); dp[0][A] = 1;
for (int i = 0; i < K; i++) {
for (int j = 1; j <= N; j++) {
if (j == B) continue;
int x = abs(j - B) - 1;
add(i+1, max(j-x, 1), j, dp[i][j]);
add(i+1, j+1, min(j+x+1, N + 1), dp[i][j]);
} int mv = 0;
for (int j = 1; j <= N; j++) {
mv = (mv + dp[i+1][j]) % mod;
dp[i+1][j] = mv;
}
} int ans = 0;
for (int i = 1; i <= N; i++)
ans = (ans + dp[K][i]) % mod;
printf("%d\n", ans);
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

Codeforces 479E Riding in a Lift(dp)的更多相关文章

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

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

  2. Codeforces 479E Riding in a Lift:前缀和/差分优化dp

    题目链接:http://codeforces.com/problemset/problem/479/E 题意: 有一栋n层的房子. 还有一个无聊的人在玩电梯,每次玩电梯都会从某一层坐到另外一层. 他初 ...

  3. Codeforces 479E Riding in a Lift

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

  4. Codeforces 480C Riding in a Lift dp

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

  5. codeforces 480C C. Riding in a Lift(dp)

    题目链接: C. Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. Codeforces Round #274 Div.1 C Riding in a Lift --DP

    题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种 ...

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

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

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

随机推荐

  1. C# Out和Ref区别

    rel是有进有出,out是只出不进 ref 要求参数在传递给函数前要初始化,out则不需要

  2. [转载] 创建为ClickOnce清单签名的.pfx格式数字证书

    使用vs2013自动创建的.pfx数字证书默认有效期只有一年,并且“颁发者”.“颁发给”均为当前机器名和当前登陆用户名的组合,其实我们完全可以创建更友好的.pfx数字证书. 打开Microsoft . ...

  3. 如何处理 Windows Phone 8 动态砖变成黑白砖

    原文:如何处理 Windows Phone 8 动态砖变成黑白砖 ? 问题的来龙去脉 我的 Windows Phone 8 动态砖变成黑白砖,所有图示和文字变成黑白,该如何处理? ? 问题的发生原因 ...

  4. 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

    题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...

  5. Linux在简短而经常使用的命令

    Linux组成: 内核:的心脏.是执行程序和管理像磁盘和打印机等硬件设备的核心程序. shell:是系统的用户界面,提供了用户和内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行.是 ...

  6. win32加载图片获得像素值

    在写光栅渲染器时,需要加载图片获得像素以便进行纹理插值,试了几种方法发现下面这种比价简单,效率也可以接受 Texture2D是我自己定义的类,其中m_pixelBuffer是一个动态二维数组,每个元素 ...

  7. 自定义ViewGroup实现垂直滚动

    转载请表明出处:http://write.blog.csdn.net/postedit/23692439 一般进入APP都有欢迎界面,基本都是水平滚动的,今天和大家分享一个垂直滚动的例子. 先来看看效 ...

  8. Android - match_parent 和 fill_parent差异

    Android - match_parent 和 fill_parent差异 本文地址: http://blog.csdn.net/caroline_wendy match_parent 和 fill ...

  9. Best Time to Buy and Sell Stock I,II,III [leetcode]

    Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...

  10. HDU 1061 Rightmost Digit解决问题的方法

    求大量N^N的值最右边的数字,即最低位. 它将能够解决一个简单二分法. 只是要注意溢出,只要把N % 10之后.我不会溢出,代替使用的long long. #include <stdio.h&g ...