codeforces gym100801 Problem J. Journey to the “The World’s Start”
传送门:https://codeforces.com/gym/100801
题意:
小明坐地铁,现在有n-1种类型的地铁卡卖,现在小明需要买一种地铁票,使得他可以在t的时间内到达终点站,地铁票的属性为r,表明这个地铁票一次最多只能做r站,可以坐到当前位置的pos-r到pos+r范围的任意一站,到站后他需要花费di的时间重新上地铁。
现在问你最小的花费是多少。
题解:
已知我们可以在pos-r到pos+r内的范围的任意一站的位置下车,那么如果我范围为r的票可以在规定时间内到达终点站的话,我的r+1的票也可以在规定的时间到达终点站,r+2的票也可以在规定的时间到达终点站,以此类推的话,我们就需要找到一个最小的r,从最小的r枚举到n就可以得到最小花费了。
于是我们需要得到最小的r。
二分答案
我们应该怎么check呢,我们定义dp状态为到达第i个站需要花费的最小时间是多少
\]
这个min可以用单调队列优化掉,于是check 的复杂度就降到了O(n)
最终复杂度为O(nlogn)
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
int p[maxn], n, q[maxn];
LL d[maxn], t;
LL dp[maxn];
bool check(LL L) {
int head, tail;
head = tail = 1;
q[head] = 1; dp[1] = 0;
for(int i = 2; i <= n; i++) {
while(tail < head && i - q[tail] > L) tail++;
dp[i] = dp[q[tail]] + d[i];
q[++head] = i;
while(tail < head && dp[q[head]] <= dp[q[head - 1]]) q[head - 1] = q[head], head--;
}
return dp[n] <= t;
}
int main() {
// freopen("journey.in", "r", stdin);
// freopen("journey.out", "w+", stdout);
scanf("%d%lld", &n, &t);
t -= n - 1;
for(int i = 1; i <= n - 1; ++i) {
scanf("%d", &p[i]);
}
for(int i = 2; i <= n - 1; ++i) {
scanf("%lld", &d[i]);
}
int L = 1;
int l = 1, r = n;
while(l <= r) {
int mid = (l + r) >> 1;
if(check(mid)) {
r = mid - 1;
L = mid;
} else {
l = mid + 1;
}
}
int ans = p[L];
for(int i = L + 1; i < n; ++i) {
ans = min(p[i], ans);
}
printf("%d\n", ans);
return 0;
}
codeforces gym100801 Problem J. Journey to the “The World’s Start”的更多相关文章
- Problem J. Journey with Pigs
Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...
- codeforces gym100801 Problem G. Graph
传送门:https://codeforces.com/gym/100801 题意: 给你一个DAG图,你最多可以进行k次操作,每次操作可以连一条有向边,问你经过连边操作后最小拓扑序的最大值是多少 题解 ...
- Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset
Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
- Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量
Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- codeforces Gym 100500 J. Bye Bye Russia
Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem J. Joke 水题
Problem J. Joke 题目连接: http://codeforces.com/gym/100714 Description The problem is to cut the largest ...
- 实验12:Problem J: 动物爱好者
#define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web B ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem J
Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...
随机推荐
- HTML/CSS学习之 三列布局,其中左侧和右侧的部分宽度固定,中间部分宽度随浏览器宽度的变化而自适应变化
第一种方法:绝对定位 <!DOCTYPE html> <html> <head> <title>三列布局</title> <link ...
- nodeJs学习-15 mysql中间件下载与使用、基本用法
下载mysql中间件(客户端):cnpm install mysql 链接数据库.查询示例: const mysql=require('mysql'); //1.连接 //createConnecti ...
- python 自主控制异常:用户自定义异常
- axios细节之绑定到原型和axios的defaults的配置属性
把axios绑定到原型 vue开发者一套很好用的实践,一般来说,实践如果能够让大部分人都接受,会逐渐成为一个默认的标准. // 把axios配置到原型上 Vue.prototype.$axios = ...
- 如何在WPF控件上应用简单的褪色透明效果?
原文 https://dailydotnettips.com/how-to-create-simple-faded-transparent-controls-in-wpf/ 使用OpacityMask ...
- 2019-11-20-dotnet-Blazor-用-C#-控制界面行为
title author date CreateTime categories dotnet Blazor 用 C# 控制界面行为 lindexi 2019-11-20 18:26:25 +0800 ...
- 快速完成智能数据构建,Dataphin公共云版本全面解读
公测两个月,Dataphin公共云版本已经受到了阿里云上众多轻量级用户的关注.事实上,Dataphin作为一款大数据智能构建与管理的产品,其核心功能是面向各行各业大数据建设.管理及应用诉求,一站式提供 ...
- Win10家庭版如何启用本地组策略
组策略对于优化和维护Windows系统来说十分重要.众所周知,Windows 10家庭版中并不包含组策略,对于使用家庭版Windows的朋友来说,十分不方便.小编将以Windows10家庭版为例,带大 ...
- Jmeter监控
https://www.cnblogs.com/saryli/p/6596647.html JMeter是一款压力测试工具,我们也可以用它来监控服务器资源使用情况. JMeter正常自带可以通过Tom ...
- 在springmvc中 @RequestMapping(value={"", "/"})是什么意思
这个意思是说请求路径 可以为空或者/ 我给你举个例子:比如百度知道的个人中心 访问路径是 http://zhidao.baidu.com/ihome,当然你也可以通过 http://zhidao.ba ...