Codeforces 479E. Riding in a Lift (dp + 前缀和优化)
题目链接:http://codeforces.com/contest/479/problem/E
题意:
给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你当前楼层x与下一个要去的楼层y的距离小于x到b的距离。求出走k趟的方案数。
题解:
dp[i][j] 表示第i趟 在第j层楼的方案数。一般用三个for才可以,所以我们用前缀和优化一下,时间复杂度降到O(n*k)。空间复杂度有点大,所以我们可以用滚动数组。
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = ;
LL dp[][N], mod = 1e9 + ;
LL sum[][N]; int main()
{
int n, a, b, k;
cin >> n >> a >> b >> k;
dp[][a] = ;
sum[][a] = ;
for(int i = ; i <= k; ++i) {
for(int j = ; j <= n; ++j) {
sum[(i - )&][j] += sum[(i - )&][j - ];
sum[i&][j] = dp[i&][j] = ;
}
for(int j = ; j <= n; ++j) {
int l, r;
if(j > b) {
l = (b + j + ) / + (b + j + ) % , r = n;
l = l == j ? l + : l, r = r == j ? r - : r;
if(l > r)
continue;
} else if(j < b) {
l = , r = (b + j - ) / ;
r = r == j ? r - : r, l = l == j ? l + : l;
if(l > r)
continue;
} else {
continue;
}
//cout << i << " " << j << " = " << l << " " << r << endl;
if(l < j && r > j) { // 包含
dp[i&][j] = mod - dp[(i - )&][j];
}
dp[i&][j] += ((sum[(i - )&][r] - sum[(i - )&][l - ]) % mod + mod) % mod;
dp[i&][j] %= mod;
sum[i&][j] += dp[i&][j];
sum[i&][j] %= mod;
}
}
LL ans = ;
for(int i = ; i <= n; ++i) {
ans += dp[k&][i];
ans %= mod;
}
cout << ans << endl;
return ;
}
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/problemset/problem/479/E 题意: 有一栋n层的房子. 还有一个无聊的人在玩电梯,每次玩电梯都会从某一层坐到另外一层. 他初 ...
- Codeforces 479E Riding in a Lift
http://codeforces.com/problemset/problem/432/D 题目大意: 给出一栋n层的楼,初始在a层,b层不能去,每次走的距离必须小于当前位置到b的距离,问用电梯来回 ...
- Codeforces 480C Riding in a Lift dp
主题链接:点击打开链接 意甲冠军: 特定 n a b k 构造一个长度k该序列. 使得序列中 对于随意两个相邻的数 | w[i-1] - w[i] | < | w[i] - b | 且第一个数 ...
- HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化
HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...
- [Codeforces712D] Memory and Scores(DP+前缀和优化)(不用单调队列)
[Codeforces712D] Memory and Scores(DP+前缀和优化)(不用单调队列) 题面 两个人玩游戏,共进行t轮,每人每轮从[-k,k]中选出一个数字,将其加到自己的总分中.已 ...
- T2988 删除数字【状压Dp+前缀和优化】
Online Judge:从Topcoder搬过来,具体哪一题不清楚 Label:状压Dp+前缀和优化 题目描述 给定两个数A和N,形成一个长度为N+1的序列,(A,A+1,A+2,...,A+N-1 ...
- 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 ...
- 2018多校第九场 HDU 6416 (DP+前缀和优化)
转自:https://blog.csdn.net/CatDsy/article/details/81876341 #include <bits/stdc++.h> using namesp ...
随机推荐
- 51nod1188 最大公约数之和 V2
考虑每一个数对于答案的贡献.复杂度是O(nlogn)的.因为1/1+1/2+1/3+1/4......是logn级别的 //gcd(i,j)=2=>gcd(i/2,j/2)=1=>phi( ...
- springmvc+hibernate入门-揭开神秘的面纱
Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这 ...
- How to: Modify a Project System So That Projects Load in Multiple Versions of Visual Studio
http://msdn.microsoft.com/en-us/library/hh266706(v=VS.110).aspx
- notepad 行替换使用指南
notepad++是开源的文本处理软件,性能堪比ultraedit,不过在转用notepad++之后一直为他的行替换功能纠结,UE当中只需要用 [^p] 就可以表示一行,但是在notepad++当中, ...
- 使用模板时 error LNK2019: 无法解析的外部符号
类模板是c++编译器指令 说明了如何生成类和成员函数 除非编译器实现了新的关键字export关键字 否则将模板成员函数放置在一个独立的实现文件中 将无法运行 因为模板不是函数 他们不能单独编译 模板必 ...
- ArcGlobe点击IGlobeServerLayer图层读取信息
ArcGISServer将点图层发布成Globe服务,AE开发中自定义识别工具,读取点数据信息. 1) 通过Locate方法获取图层对象,图层对象中的SearchOID就是你点中的要素Objectid ...
- PhoneGap与Jquery Mobile结合开发android应用配置
由于工作需要,用到phonegap与jquery moblie搭配,开发android应用程序. 这些技术自己之前也都没接触过,可以说是压根没听说过,真是感慨,在开发领域,技术日新月异,知识真是永远学 ...
- mysql 1130 ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server
mysql -u root -p mysql;use mysql; mysql;select 'host' from user where user='root'; mysql;update u ...
- tengine+lua的安装步骤
我是在Red Hat 5.8 的虚机上安装的. Nginx的一些模块需要其他第三方库的支持,例如gzip模块需要zlib库,rewrite模块需要pcre库,ssl功能需要openssl库等.建议把这 ...
- 【LeetCode 236】Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...