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天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...
随机推荐
- python 装饰器
#!/usr/bin/env python3 #-*-encoding:utf-8-*- def w3(*args, **kwargs): ') def w1(): def ww1(func): de ...
- express框架
一.express的使用 安装express 在根目录下输入 npm install express 引入express的包 const express = require("express ...
- shell脚本比较两个数大小
#/bin/bash read -p "请输入第一个数:" a read -p "请输入第二个数:" b if [ $a -gt $b ] #判断第一个数是不是 ...
- centos下建立双机信任关系
在有些情况下,我们希望在两台centos机器之间建立ssh连接的时候,可以不用输入密码.最常见的情况就是在使用脚本做数据库备份的时候.这种情况下,我们可以通过公钥/私钥来建立双机之间的信任关系. 网上 ...
- JAVA的模式对话框和非模式对话框
周末的时候,一位网友让我帮他把他的无模式对话框改成有模式对话框. 界面是由swing制作的,都是JFrame,我从来没有接触过swing编程.大致的代码还是看的懂,很多都和C#很相似. 然后就去查资料 ...
- [图像]判断图片是PNG还是JPG格式
typedef NS_ENUM(NSInteger, NSPUIImageType) { NSPUIImageType_JPEG, NSPUIImageType_PNG, NSPUIImageType ...
- 项目中 -- 设置tabBar样式 (旅游局)
- (void)addChildViewController:(UIViewController *)ViewController image:(UIImage *)image selectImg:( ...
- yii asset 初步
yii 版本是2.0 .8 我 js 目录 web->js->login.js 页面引入js方法一: $this->registerJsFile('@web/js/login.js' ...
- 海外建VPS并支持VPN
推荐 DigitalOcean http://www.digitalocean.com/?refcode=7c26aea99ed6
- 再谈缓存和Redis
自从上次分享<Redis到底该如何利用?>已经有1年多了,这1年经历了不少.从码了我们网站的第一行开始到现在,我们的缓存模块也不断在升级,这之中确实略有心得,最近也有朋友探讨缓存,觉得可以 ...