传送门: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个站需要花费的最小时间是多少

\[dp[i] = min(dp[i - j]) + cost[i],(1 <= i <= n, 1 <= j <= min(i - 1, range) )
\]

这个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”的更多相关文章

  1. Problem J. Journey with Pigs

    Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...

  2. codeforces gym100801 Problem G. Graph

    传送门:https://codeforces.com/gym/100801 题意: 给你一个DAG图,你最多可以进行k次操作,每次操作可以连一条有向边,问你经过连边操作后最小拓扑序的最大值是多少 题解 ...

  3. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  4. Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量

    Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...

  5. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  6. codeforces Gym 100500 J. Bye Bye Russia

    Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...

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

  8. 实验12:Problem J: 动物爱好者

    #define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web B ...

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

随机推荐

  1. css 的float和inline-block区别

    CSS布局创建网站,浮动绝对占据了很大的比例.大块区域如主内容及侧边栏,以及在其中的小块区域,都可以看到浮动的影子.这里浮动是唯一的解决方案吗? 浮动通常表现正常,但有时候搞起来会很纠结.特别是处理内 ...

  2. Vagrant-安装教程及常见问题

    http://ju.outofmemory.cn/entry/346215 前言: Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境. 它的主要意义是让所有开发人员都使用和线上服务 ...

  3. springboot 数据访问【转】【补】

    六.SpringBoot与数据访问 1.JDBC pom.xml配置 <dependencies> <dependency> <groupId>org.spring ...

  4. phpexcel导出数据库成excel文件

    <?php error_reporting(E_ALL); date_default_timezone_set('Europe/London'); /** PHPExcel */ require ...

  5. SDUT-2449_数据结构实验之栈与队列十:走迷宫

    数据结构实验之栈与队列十:走迷宫 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个由n * m 个格子组成的迷宫,起 ...

  6. Knative 初体验:Eventing Hello World

    作者 | 阿里云智能事业群高级开发工程师 元毅 基于事件驱动是Serveless的核心功能之一,通过事件驱动服务,满足了用户按需付费(Pay-as-you-go)的需求.在之前的文章中我们介绍过 Kn ...

  7. 1176. Two Ends

    题目链接地址:http://soj.me/1176 题目大意:两头取数.第一个人随机取,第二个人用贪婪算法(每次都取大的),求两人取数在第一个人赢的情况下的最大分差.使用贪婪算法时,如果左右两边相等, ...

  8. Android Tween和Frame 动画

    关于动画的实现,Android提供了Animation,在Android SDK介绍了2种Animation模式: 1. Tween Animation:通过对场景里的对象不断做图像变换(平移.缩放. ...

  9. Oracle使用——varchar2() 和 char()关联查询 存在空格

    背景 表dbcontinfo 字段loanid,类型为varchar2(60) 表dbloanbal 字段loanid,类型为char(60) loanid字段实际长度为24位 问题 两张表dbloa ...

  10. poj 1092 Farmland (Geometry)

    1092 -- Farmland 怎么最近做几何题都这么蛋疼,提交C++过不了交G++就过了.据我估计,原因是用了atan2这个函数,或者是其他一些函数造成了精度的影响.不管怎样,这题最后还是过了~ ...