CodeForces 729C Road to Cinema

传送门:CF729C Road to Cinema
算法分析:到达终点的最小时间单调递增,即钱越多,时间越短,考虑二分,关键在于判断油箱剩余油量的操作


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxN=300000;
struct Node
{
    int c,v;
}a[maxN+1];
bool comp(Node x,Node y)
{
    if(x.v!=y.v) return x.v<y.v;
    return x.c<y.c;
}
int n,s,k,t,oil[maxN+1],ans=-1,final=2147483647;
bool check(int);
inline int read();
int main()
{
    n=read(); k=read(); s=read(); t=read();
    for(int i=1;i<=n;i++)
    {
        a[i].c=read();
        a[i].v=read();
    }
    sort(a+1,a+n+1,comp);
    for(int i=1;i<=k;i++)
        oil[i]=read();
    sort(oil+1,oil+k+1);
    k++; oil[k]=s;
    int l=1,r=n;
    while(l<=r)
    {
    	int mid=(l+r)/2;
    	if(check(mid)) {ans=mid; r=mid-1;}
    	else l=mid+1;
	}
	if(ans==-1) printf("-1");
	else
	{
		for(int i=ans;i<=n;i++)
			final=min(final,a[i].c);
		printf("%d",final);
	}
		
    return 0;
}
inline int read()
{
    int num=0,f=1;
    char ch=getchar();
    while((ch<'0' || ch>'9') && ch!='-') ch=getchar();
    if(ch=='-') {f=-1; ch=getchar();}
    while(ch>='0' && ch<='9')
    {
        num=num*10+ch-'0';
        ch=getchar();
    }
    return num*f;
}
bool check(int num)
{
    int ti=0,loca=0;
    for(int i=1;i<=k;i++)
    {
        int dis=oil[i]-oil[i-1];
        if(dis>a[num].v) return false;
        if(dis*2<=a[num].v) ti+=dis;
        else
		{
        	int rest=a[num].v-dis;
        	ti=ti+dis*2-rest;
		}
        loca+=dis;
    }
    if(ti>t) return false;
    return true;
}
posted @ 2019-02-10 15:49  常青藤的花语  阅读(155)  评论(0编辑  收藏  举报

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。