CF2.C(二分贪心)
1 second
256 megabytes
standard input
standard output
Vasya is currently at a car rental service, and he wants to reach cinema. The film he has bought a ticket for starts in t minutes. There is a straight road of length s from the service to the cinema. Let's introduce a coordinate system so that the car rental service is at the point 0, and the cinema is at the point s.
There are k gas stations along the road, and at each of them you can fill a car with any amount of fuel for free! Consider that this operation doesn't take any time, i.e. is carried out instantly.
There are n cars in the rental service, i-th of them is characterized with two integers ci and vi — the price of this car rent and the capacity of its fuel tank in liters. It's not allowed to fuel a car with more fuel than its tank capacity vi. All cars are completely fueled at the car rental service.
Each of the cars can be driven in one of two speed modes: normal or accelerated. In the normal mode a car covers 1 kilometer in 2 minutes, and consumes 1 liter of fuel. In the accelerated mode a car covers 1 kilometer in 1 minutes, but consumes 2 liters of fuel. The driving mode can be changed at any moment and any number of times.
Your task is to choose a car with minimum price such that Vasya can reach the cinema before the show starts, i.e. not later than in t minutes. Assume that all cars are completely fueled initially.
The first line contains four positive integers n, k, s and t (1 ≤ n ≤ 2·105, 1 ≤ k ≤ 2·105, 2 ≤ s ≤ 109, 1 ≤ t ≤ 2·109) — the number of cars at the car rental service, the number of gas stations along the road, the length of the road and the time in which the film starts.
Each of the next n lines contains two positive integers ci and vi (1 ≤ ci, vi ≤ 109) — the price of the i-th car and its fuel tank capacity.
The next line contains k distinct integers g1, g2, ..., gk (1 ≤ gi ≤ s - 1) — the positions of the gas stations on the road in arbitrary order.
Print the minimum rent price of an appropriate car, i.e. such car that Vasya will be able to reach the cinema before the film starts (not later than in t minutes). If there is no appropriate car, print -1.
3 1 8 10
10 8
5 7
11 9
3
10
2 2 10 18
10 4
20 6
5 3
20
In the first sample, Vasya can reach the cinema in time using the first or the third cars, but it would be cheaper to choose the first one. Its price is equal to 10, and the capacity of its fuel tank is 8. Then Vasya can drive to the first gas station in the accelerated mode in 3 minutes, spending 6 liters of fuel. After that he can full the tank and cover 2 kilometers in the normal mode in 4 minutes, spending 2 liters of fuel. Finally, he drives in the accelerated mode covering the remaining 3 kilometers in 3 minutes and spending 6 liters of fuel.
题意:
有n辆车,每辆车有租金和邮箱的容量,路上有k个加油站,道路总长s,要求在t时间内到达终点,问选哪辆车可以以最少的租金在规定时间内到达,加油站加油不要钱,最后一行给出加油站的位置。
代码:
//直接找会在第31组样例TE,卡了好久。二分油箱,找到一个最小的油箱容量能够走完全程,汽车排序后找到第一个大于等于该容量的汽车即可
//二分啊,每次都想不清楚,写不对!
#include<bits\stdc++.h>
using namespace std;
struct Lu
{
int r,v;
}L[];
int com[];
int n,k,s,t;
bool cmp1(Lu x,Lu y)
{
return x.r<y.r;
}
bool cmp2(int x,int y)
{
return x<y;
}
int check(long long mid)
{
int m=,now=,st=t;
while(m<k)
{
if(com[m]-now>mid||com[m]-now>st)
return ;
long long tem=mid-(com[m]-now);//剩余多少油就说明可以有多少路高速行驶
if(tem>com[m]-now)
tem=com[m]-now;
st-=(tem+*(com[m]-now-tem));
now=com[m];
m++;
}
if(s-now>mid||s-now>st)
return ;
long long tem=mid-(s-now);
if(tem>s-now)
tem=s-now;
st-=(tem+*(s-now-tem));
if(st<) return ;
return ;
}
int main()
{
scanf("%d%d%d%d",&n,&k,&s,&t);
for(int i=;i<n;i++)
scanf("%d%d",&L[i].r,&L[i].v);
for(int i=;i<k;i++)
scanf("%d",&com[i]);
sort(L,L+n,cmp1);
sort(com,com+k,cmp2);
long long lef=,rig=,mid;
while(lef<=rig)
{
mid=(lef+rig)/;
if(check(mid))
rig=mid-;
else lef=mid+;
}
// cout<<rig+1<<endl;
int ans=-;
for(int i=;i<n;i++)
if(L[i].v>=rig+)
{
ans=L[i].r;
break;
}
printf("%d\n",ans);
return ;
}
CF2.C(二分贪心)的更多相关文章
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
/** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...
- 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心
题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径. ...
- Codeforces_732D_(二分贪心)
D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- CF732D Exams 二分 贪心
思路:二分+贪心 提交次数:10次以上 错因:刚开始以为二分(边界,$+1or-1$)写错了,调了半天,后来才发现是$ck()$写错了.开始只判了最后是否小于零,而应该中间一旦小于零就$return\ ...
- $CF949D\ Curfew$ 二分/贪心
正解:二分/贪心 解题报告: 传送门$QwQ$ 首先这里是二分还是蛮显然的?考虑二分那个最大值,然后先保证一个老师是合法的再看另一个老师那里是否合法就成$QwQ$. 发现不太会搞这个合不合法的所以咕了 ...
- $bzoj2067\ szn$ 二分+贪心
正解:二分+贪心 解题报告: 传送门$QwQ$ 题目大意就说有一棵树,然后要用若干条线覆盖所有边且不能重叠.问最少要用几条线,在用线最少的前提下最长的线最短是多长. 昂首先最少用多少条线这个还是蛮$e ...
- leetcode1552题解【二分+贪心】
leetcode1552.两球之间的磁力 题目链接 算法 二分+贪心 时间复杂度O(nlogn + nlogm) 1.根据题意描述,我们需要将m个球放入到n个篮子中,根据题目中数据范围描述发现m &l ...
- Codeforces 732D [二分 ][贪心]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...
随机推荐
- c#程序如何从海康ipserver查询获取DVR设备ip地址
海康威视提供的ipserver可以记录DVR设备的IP地址,并提供一个7071的端口给客户端查询用,我们在c#程序中可以用海康SDK包中的NET_DVR_GetDVRIPByResolveSvr函数来 ...
- Ionic 常用插件
ionic扩展插件 1.ionic-timepicker 时间选择 https://github.com/rajeshwarpatlolla/ionic-timepicker 2.ionic-da ...
- 如何在Linux中搭建禅道8.4.1(httpd+php+mysql)
1.安装httpd 命令:yum install httpd 然后一路y即可 2.安装php 命令:yum install php 3.安装php-mysql 命令:yum install php ...
- 添加OSG各种事件处理器
// add the state manipulator viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera ...
- React学习笔记-7-销毁阶段
销毁阶段可以使用的函数:componentWillUnmount:在删除组件之前进行清理操作,比如计时器和事件监听器.因为这些函数都是开发者手动加上去的,react不知道,必须进行手动清理. 实例第一 ...
- REPL环境
一.Node的REPL基本操作 REPL(Read-eval-print-loop):交互式解析器 在REPL环境下,可以定义和运行变量.函数.对象. REPL的常用命令: 进入node,即进入了RE ...
- Adobe illustrator & Photoshop 处理图片
Adobe illustrator 置入,可以将多张图片放在一张纸 (一个sheet)里面. Adobe photoshop: 新建一个图层,然后置入,将图片导入,然后裁剪,最后另存为就可以得到最后的 ...
- iOS常用系统信息获取方法
一.手机电量获取,方法二需要导入头文件#import<objc/runtime.h> 方法一.获取电池电量(一般用百分数表示,大家自行处理就好) -(CGFloat)getBatteryQ ...
- OE学习笔记流水
Terrain.cpp中的getWorldCoordsUnderMouse函数,进行标记.
- HTML中块级元素与行内元素
一.行内元素与块级元素 块级元素列表 <address> 定义地址 <caption> 定义表格标题 <dd> 定义列表中定义条目 <div> 定义文档 ...