20152016-acmicpc-neerc-northern-subregional-contest J:Journey to the "The World's Start"(单调队列+DP+二分)
http://codeforces.com/gym/100801/attachments
题意:给出n-1张不同的票,票价分别为 pi,每张票每次最多可以坐 r 个站(1<=r<n),并且票是可以无限用并且只能买一张,如果坐到限定的距离了,要出站再重新进站,这里要花费 di 的时间(2<=i<=n-1),并且每坐一个站花费 1 min,一个人坐地铁要从第一个站坐到最后一个站,问在规定时间 t 里面可以买到的最低票价是多少。
思路:首先可以确定的是,我们对票价都要进行一次DP,我自己推出的方程是dp[i] = min(dp[i], (dp[i-j] + j) + time[i]),(1<=j<=r),接着发现无论怎么样,那个人肯定要坐 n-1 个站,所以直接让 t 减去 n - 1,然后方程变成dp[i] = min(dp[i], dp[i-j]+time[i]),这样的话DP时间复杂度还是O(n^2)。
由于是 f[k] + g[i] 型的 dp(只有形如 dp[i]=max/min (f[k]) + g[i] (k<i && g[i]是与k无关的变量)才能用到单调队列进行优化。),我们可以使用单调队列来优化这个DP,化成dp[i] = min(dp[i], dp[que[head]] + time[i])这样的形式。这样优化之后时间复杂度就直接变成了O(n)了。
只有这样还是不够的,因为我们要枚举每种票,通过题意可以发现每种票的距离 r 是递增的,因此我们可以通过二分搜索来枚举,这样复杂度就变成了O(nlogn)了,还有一点就是可能一种票的路程比搜索出来的临界值大,但是价格较小,我们应该枚举一下后面的取较小者。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
#include <cstdlib>
typedef long long LL;
using namespace std;
#define N 50005
#define INF 0x3f3f3f3f3f
//单调队列优化DP + 二分搜索
LL price[N];
LL time[N];
LL dp[N];
int que[N];
int n, t; bool solve(int r)
{
if(r == ) return false;
dp[] = ;
int head = , tail = ;
que[++tail] = ;
for(int i = ; i <= n; i++) {
dp[i] = dp[que[head]] + time[i];
// 如果队尾的时间大于当前时间,就删除,然后把当前的站插入
while(head <= tail && dp[que[tail]] >= dp[i])
tail--;
que[++tail] = i;
// 当前距离和之前的距离不能超过 r
while(head <= tail && i - que[head] >= r)
head++;
}
if(dp[n] > t) return false;
return true;
} int main()
{
// freopen("journey.in", "r", stdin);
// freopen("journey.out", "w", stdout);
scanf("%d%d", &n, &t);
for(int i = ; i < n; i++)
scanf("%I64d", &price[i]);
for(int i = ; i <= n-; i++)
scanf("%I64d", &time[i]);
//总共坐n-1个站要n-1 min
t -= n-;
int l = , r = n - ;
while(l <= r) {
int mid = (l+r) / ;
if(solve(mid)) {
r = mid - ;
} else {
l = mid + ;
}
} int ans = price[l];
for(int i = l + ; i < n; i++)
if(price[i] < ans) ans = price[i]; printf("%d\n", ans); return ;
}
20152016-acmicpc-neerc-northern-subregional-contest J:Journey to the "The World's Start"(单调队列+DP+二分)的更多相关文章
- 模拟赛小结:2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest
2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest 2019年10月11日 15:35-20:35(Solved 8,Penalty 675 ...
- 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest (9/12)
$$2015-2016\ ACM-ICPC,\ NEERC,\ Northern\ Subregional\ Contest$$ \(A.Alex\ Origami\ Squares\) 签到 //# ...
- 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D】---暑假三校训练
2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D Problem D. Distribution in Metagonia Input ...
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest J Cleaner Robot
Cleaner RobotCrawling in process... Crawling failed Time Limit:2000MS Memory Limit:524288KB ...
- 2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest J - Jealousy
题意:有n张照片,每张照片上有一些妹子,要按照片顺序给妹纸安排男朋友,如果妹纸i安排的男朋友之前有女朋友,那么费用+wi,求总费用最小,和输出路径 题解:费用流,先把照片天数建点i连i+1,流量k(最 ...
- 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D:Distribution in Metagonia(构造)
http://codeforces.com/gym/100801/attachments 题意:给出一个数n(1 <= n <= 1e18),将 n 拆成 m 个整数,其中 m 必须是 2 ...
- 【题解】G.Graph(2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest)
题目链接G题 题意 序列 \(a_1,a_2,⋯,a_n\) 是一个排列, 当且仅当它含有 1 到 n 的所有整数. 排列 \(a_1,a_2,⋯,a_n\) 是一个有向图的拓扑排序,当且仅当对于每条 ...
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest A Email Aliases(模拟STL vector+map)
Email AliasesCrawling in process... Crawling failed Time Limit:2000MS Memory Limit:524288KB ...
- 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest
A. Anniversary Cake 随便挑两个点切掉就好了. #include<bits/stdc++.h> using namespace std; const int Maxn=2 ...
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest, B. Layer Cake
Description Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping ...
随机推荐
- 【代码备忘录】VC设置您的计算机环境变量、注册表操作
欢迎增加C/C++ QQ组,无论你的工作.学生,只有具备c / vc / c++ 编程经验.就来吧!158427611 [设置电脑环境变量] 设置电脑环境变量非常easy,由于window而言.环境变 ...
- ef core 数据类型 && 表字段名设置
HasColumnType HasColumnType是指定字段类型 [Column(TypeName = "decimal(18, 2)")] public decimal Mo ...
- Bootstrap 媒体对象 列表组
@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport&q ...
- jquery 可以给事件传参数
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 在 Laravel 中通过 Artisan View 扩展包创建及删除应用视图文件
1.简介 本扩展包添加了两个视图相关的Artisan命令到Laravel应用,以便我们通过Artisan命令即可创建和管理视图文件,可谓是进一步解放了生产力. 2.安装 还是通过Composer安装: ...
- 前端开发常用PhotoShop快捷键整理(更新中)
图片来源 UI提供的psd图 印屏幕:PrScrn SysRq(键盘按键) 浏览器(插件)获取 常用的快捷键: 新建 Ctrl + N 取消选框 Ctrl + D 反选 Ctrl + shift + ...
- __declspec的15种用法
__cdecl和__stdcall都是函数调用规范(还有一个__fastcall),规定了参数出入栈的顺序和方法,如果只用VC编程的话可以不用关心,但是要在C++和Pascal等其他语言通信的时候就要 ...
- html send mail
<html> <body> <script> var formattedBody = "FirstLine \n Second Line \n Third ...
- Silverlight DataGrid自适应数据
silverlight的DataGrid如果改成fill模式 那么当数据超长也不会出现横向滚动条 如果改成按单元格模式 如果数据较短又会出现空白的地方 所以我自己写了个算法 目前实现了以list为数据 ...
- ASP如何实现注册后发送激活邮件?
<% Sub Sendemail(title,content,email) Set jmail = Server.CreateObject("JMAIL.Message") ...