1. 程式人生 > >CodeForces 729C Road to Cinema

CodeForces 729C Road to Cinema

!= clas 小時 ostream getchar() ble fin ref def

傳送門: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;
}

CodeForces 729C Road to Cinema