题目链接:http://codeforces.com/problemset/problem/729/C
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 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.
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 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 2liters of fuel. Finally, he drives in the accelerated mode covering the remaining 3 kilometers in 3 minutes and spending 6 liters of fuel.
题解:
1.由于题目没说明车越贵,容量越大,所以需要将价格贵但容量小的车丢弃。
做法是:先按车的容量升序排列,然后用单调队列处理,使得队列中的车的价格也递增。
2.二分车的下标。
单调队列:
int N = 0; for(int i = 1; i<=n; i++) { while(N>=1 && a[i]<=a[N]) N--; // a[i]<a[N] 还是 a[i]<=a[N]视情况而定。 a[++N] = a[i]; }
代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const double eps = 1e-6; 5 const int INF = 2e9; 6 const LL LNF = 9e18; 7 const int mod = 1e9+7; 8 const int maxn = 2e5+10; 9 10 LL n,k,s,t, g[maxn]; 11 LL N; 12 13 struct node 14 { 15 LL c,v; 16 bool operator<(const node&x)const{ 17 return v<x.v; 18 } 19 }a[maxn]; 20 21 void init() 22 { 23 cin>>n>>k>>s>>t; 24 for(int i = 1; i<=n; i++) 25 scanf("%lld%lld",&a[i].c, &a[i].v); 26 for(int i = 1; i<=k; i++) 27 scanf("%lld",&g[i]); 28 29 g[++k] = s; //将终点也放进去 30 sort(g+1,g+1+k); 31 32 N = 0; 33 sort(a+1,a+1+n); //按车的汽油容量升序排序 34 for(int j = 1; j<=n; j++) //单调队列 35 { 36 while(N>=1 && a[j].c<=a[N].c) N--; 37 a[++N] = a[j]; 38 } 39 } 40 41 int test(int pos) 42 { 43 LL x, y, T = 0; //x为以快速行驶的路程, y为以常速行驶的路程 44 LL capa = a[pos].v; 45 for(int i = 1; i<=k; i++) 46 { 47 LL dis = g[i]-g[i-1]; 48 49 if(capa<dis) //以常速都跑不完 50 return 0; 51 else if(capa<2*dis ) //快速+常速 or 常速 52 x = capa - dis, y = capa - 2*x; 53 else //可以全程以快速行驶 54 x = dis, y = 0; 55 56 T += x + 2*y; //更新时间 57 if(T>t) return 0; //超时 58 } 59 return 1; 60 } 61 62 void solve() 63 { 64 LL l = 1, r = N; 65 while(l<=r) 66 { 67 LL mid = (l+r)>>1; 68 if(test(mid)) 69 r = mid - 1; 70 else 71 l = mid + 1; 72 } 73 printf("%lld\n",l==N+1? -1 : a[l].c); 74 } 75 76 int main() 77 { 78 init(); 79 solve(); 80 }