题目链接: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。最后处理的时候每一个位置的准确值即为数组的前缀和。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. using namespace std;
  6. const int maxn = 5005;
  7. const int mod = 1e9 + 7;
  8. int N, A, B, K, dp[maxn][maxn];
  9. void add (int idx, int l, int r, int d) {
  10. dp[idx][l] = (dp[idx][l] + d) % mod;
  11. dp[idx][r] = (dp[idx][r] - d + mod) % mod;
  12. }
  13. int main () {
  14. scanf("%d%d%d%d", &N, &A, &B, &K);
  15. dp[0][A] = 1;
  16. for (int i = 0; i < K; i++) {
  17. for (int j = 1; j <= N; j++) {
  18. if (j == B) continue;
  19. int x = abs(j - B) - 1;
  20. add(i+1, max(j-x, 1), j, dp[i][j]);
  21. add(i+1, j+1, min(j+x+1, N + 1), dp[i][j]);
  22. }
  23. int mv = 0;
  24. for (int j = 1; j <= N; j++) {
  25. mv = (mv + dp[i+1][j]) % mod;
  26. dp[i+1][j] = mv;
  27. }
  28. }
  29. int ans = 0;
  30. for (int i = 1; i <= N; i++)
  31. ans = (ans + dp[K][i]) % mod;
  32. printf("%d\n", ans);
  33. return 0;
  34. }

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

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. 【原创】leetCodeOj ---Partition List 解题报告

    原题地址: https://oj.leetcode.com/problems/partition-list/ 题目内容: Given a linked list and a value x, part ...

  2. 在borland c++ builder 中使用 google test (gtest)

    google test version: 1.6 c++ builder version: xe6 1 download google test 1.6 2 unzip the zip file. T ...

  3. atitit..主流 浏览器 js 发动机 内核 市场份额 attialx总结vOa9

    atitit..主流 浏览器 js 发动机  内核 市场份额 attialx总结vOa9 1. 浏览器内核 1 2. 浏览器的主要组件包含: 2 2.1. 主要组件体系结构 2 2.2. WebCor ...

  4. 为什么MVC不是一种设计模式(转)

    MVC(Model-View-Controller)是处理界面应用程序时常用的解决方案,构成了表示层. MVC通过分离模型.视图.控制器在应用程序中的角色,实现界面和业务逻辑的解耦.Model(是OO ...

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

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

  6. QrcodeWithLogo

    package com.qrcode; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import ...

  7. HDU - 5036 Operation the Sequence

    Problem Description You have an array consisting of n integers: a1=1,a2=2,a3=3,-,an=n. Then give you ...

  8. [Python 学习] 两、在Linux使用平台Python

    在本节,它介绍了Linux如何使用平台Python 1. Python安装. 今天,大多数把自己的版本号Python的,它不能被安装.假设你要安装它,可以使用相应的安装指令. Fedora:先以roo ...

  9. nodeJs基础

    Node.js 是一个基于Chrome JavaScript 执行时建立的一个平台, 用来方便地搭建高速的 易于扩展的网络应用· Node.js 借助事件驱动, 非堵塞I/O 模型变得轻量和高效, 很 ...

  10. apache本地多域配置(wampserver本地多域配置)

    当我们在当地发展.通常在浏览器中输入 http://localhost/项目目录名 测试Web文件,你有没有想过在本地浏览器中,输入自己设定的名字进入项目目录,名相关的问题. 比方我想配置一个主域名w ...