Codeforces #380 div2 C(729C) Road to Cinema

C. Road to Cinema
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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 point0, 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 2minutes, 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 tminutes. Assume that all cars are completely fueled initially.

Input

The first line contains four positive integers nks 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.

Output

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.

Examples
input
3 1 8 10
10 8
5 7
11 9
3
output
10
input
2 2 10 18
10 4
20 6
5 3
output
20
Note

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 3minutes, 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.

题目大意:一个一维的路,从距离为0到距离为s,中间有一些加油站,每次都能不耗时加满油,每辆车有c和v,分别表示租金和油量,有两种速度,一公里花1个单位时间消耗2个单位的油,一公里花2个单位的时间花1个单位的油,要求在t秒内到达终点所能租的最便宜的车花多少钱?

做法:稍微思考就能看出是二分,由于每次路过加油站都能加满油而且不耗时,所以争取在能到达下一个加油站(或终点)的前提条件下能开多快开多快,所以如果△s>v,那么v太小了不行,如果△s小于0.5v,那么耗时△t=s,其他情况,计算得△t=3s-v,将每一个△t加起来,判断是否小于等于t。

因此对每一个v都可判断t秒内能不能走到终点,对t取值进行二分,找到满足条件的最小的v,用这个v遍历汽车油量与租金,在满足油量>v的基础上找最小租金。

PS:除了几处手残写错之外,注意加油站的位置不是按顺序给的,所以要排个序。。。本来觉得这道题肯定能做出来,最后剩下2分钟过的,好险。

代码:

 #include <cstdio>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define N 200005
#define inf 1e18+5
typedef long long ll;
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
ll k,m,n,t,cc,s,ma,mi;
ll g[N];
struct st{
ll c;
ll v;
}a[N];
bool check(ll v);
int main()
{
while(scanf("%lld%lld%lld%lld",&n,&k,&s,&t)!=EOF){
ma=-;
mi=inf;
for(int i=;i<=n;i++){
scanf("%lld %lld",&a[i].c,&a[i].v);
ma=max(ma,a[i].v);
mi=min(mi,a[i].v);
}
for(int i=;i<=k;i++){
scanf("%lld",&g[i]);
}
sort(g+,g+k+);
ll l=mi,r=ma;
while(l<r-){
ll mid=(l+r)>>;
if(check(mid)==){
r=mid;
}
else {
l=mid;
}
}
ll ans=;
if(check(l)) ans=l;
else if(check(r)) ans=r;
else ans=inf;
ll aa=inf;
for(int i=;i<=n;i++){
if(a[i].v>=ans){
aa=min(aa,a[i].c);
}
}
if(aa==inf) printf("-1\n");
else printf("%lld\n",aa);
} return ;
}
bool check(ll v){ ll t1=;
ll s1=g[];
if(s1>v) {
return ;
}
else if(s1<=v&&s1>=(v/2.0)){
t1+=*s1-v;
}
else {
t1+=s1;
}
for(int i=;i<k;i++){
ll s1=g[i+]-g[i];
if(s1>v) {
return ;
}
else if(s1<=v&&s1>=(v/2.0)){
t1+=*s1-v;
}
else {
t1+=s1;
}
}
s1=s-g[k];
if(s1>v) {
return ;
}
else if(s1<=v&&s1>=(v/2.0)){
t1+=*s1-v;
}
else {
t1+=s1;
}
if(t1>t) return ;
else return ;
}
上一篇:Mysql数据库密码忘记的解决办法


下一篇:动态给drawable上色