传送门: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. 设置onselectstart在ie浏览器下对于input及textarea标签页会生效

    设置onselectstart在ie浏览器下对于input及textarea标签页会生效, 可以使用js来控制对于某种标签不生效,代码如下: document.onselectstart = disa ...

  2. linux下清除tomcat缓存

    进入tomcat/bin目录下,执行命令:./shutdown.sh 然后执行命令:ps -aux ,查看tomcat是否真的关闭了 如果没有关闭则执行命令:kill -9 #pid来彻底关闭tomc ...

  3. Gartner:阿里云位列全球云数据库市场份额前三,数据库未来需上云

    近日,国际权威研究机构Gartner发布 <The Future of the Database Management System (DBMS) Market Is Cloud>报告,鲜 ...

  4. 应用中弹出 WiFi 提示框的方法

    如果 iOS 程序中用到了 WiFi,想有 WiFi 提示,只需要在 .plist 文件中加入如下 Key/Value 即可: 键名:ApplicationusesWi-Fi 值:YES 键名:SBU ...

  5. nginx设置301永久重定向

    https://blog.csdn.net/wzqzhq/article/details/53376501 比如说我的域名有多个,一个主域名www.zq110.com,多个次域名:www.aaa.co ...

  6. 2018-8-10-WPF-轻量级-MVVM-框架入门-2.1.2

    title author date CreateTime categories WPF 轻量级 MVVM 框架入门 2.1.2 lindexi 2018-08-10 19:16:51 +0800 20 ...

  7. LeetCode58 Length of Last Word

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

  8. Project Euler Problem 24-Lexicographic permutations

    全排列的生成,c++的next_permutation是O(n)生成全排列的.具体的O(n)生成全排列的算法,在 布鲁迪 的那本组合数学中有讲解(课本之外,我就看过这一本组合数学),冯速老师翻译的,具 ...

  9. Spark JDBC系列--Mysql tinyInt字段特殊处理

    当spark取出表的scheme中,类型名为tinyint的字段,会被处理为Boolean型.而mysql中tinyint的sqlType都会默认处理为bit,所以如果数据库中的这类字段中,存储了0. ...

  10. Java安装完毕后的环境配置

    右键计算机=>属性=>高级系统设置=>环境变量=>系统变量=>新建系统变量 变量名:JAVA_HOME变量值:E:\Program Files\Java\jdk-9.0. ...