E. Printer

http://codeforces.com/contest/253/problem/E

对优先级进行二分  每次给一个优先级就模拟一下看是否合格 不合格就二分调整

需要注意的地方 所给T的范围会超 int (就是这点敲代码时忽略了 wa 了很久 细节呀)  还有就是优先级没有重的情况

代码:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<map>

#include<vector>

#include<stack>

#include<set>

#include<map>

#include<queue>

#include<algorithm>

#define LL long long

using namespace std;

const int N=50005;

struct node

{

    LL t,s,p;

    int I;

    bool operator <(node x)const

    {

        return p<x.p;

    }

}mem[N];

priority_queue<node>qt;

int K;

LL ans[N];

set<LL>st;

bool cmp(node x,node y)

{

    return x.t<y.t;

}

LL run(int p,int n)

{

    mem[K].p=p;

    int k=mem[K].I;

    int l=0;

    LL tnow=0;

    while(!qt.empty()||l<n)

    {

        if(l<n&&qt.empty())

        {qt.push(mem[l]);tnow=mem[l].t;++l;}

        while(l<n)

        {

            if(mem[l].t==tnow)

            {qt.push(mem[l]);++l;}

            else

            break;

        }

        node tmp=qt.top();

        qt.pop();

        while(l<n)

        {

            if(mem[l].t-tnow<tmp.s&&mem[l].p<tmp.p)

            {qt.push(mem[l]);++l;}

            else

            break;

        }

        if(l==n||mem[l].t-tnow>=tmp.s)

        {

            tnow+=tmp.s;

            tmp.s=0;

        }else

        {

            tmp.s-=(mem[l].t-tnow);

            tnow=mem[l].t;

        }

        if(tmp.s==0)

        {

            ans[tmp.I]=tnow;

        }else

        {

            qt.push(tmp);

        }

    }

    return ans[k];

}

int main()

{

    //freopen("data.in","r",stdin);

    freopen("input.txt","r",stdin);

    freopen("output.txt","w",stdout);

    int n;

    while(scanf("%d",&n)!=EOF)

    {

        st.clear();

        for(int i=0;i<n;++i)

        {

            cin>>mem[i].t>>mem[i].s>>mem[i].p;

            mem[i].I=i;

            st.insert(mem[i].p);

        }

        LL T;

        cin>>T;

        sort(mem,mem+n,cmp);

        for(int i=0;i<n;++i)

        if(mem[i].p==-1)

        K=i;

        LL l=1,r=1e9,mid;

        while(st.find(l)!=st.end())

        ++l;

        while(st.find(r)!=st.end())

        --r;

        while(l<=r)

        {

            mid=(l+r)>>1;

            while(st.find(mid)!=st.end())

            ++mid;

            LL t=run(mid,n);

            if(t==T)

            break;

            if(t<T)

            r=mid-1;

            else

            l=mid+1;

            if(st.find(l)!=st.end())

            ++l;

            while(st.find(r)!=st.end())

            --r;

        }

        cout<<mem[K].p<<endl;

        for(int i=0;i<n;++i)

        cout<<ans[i]<<" ";

        printf("\n");

    }

    return 0;

}

  

 

 

你可能感兴趣的:(print)