poj2010

</pre><pre name="code" class="html"><img src="https://img-blog.csdn.net/20151117215338104?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><pre name="code" class="html">#include"iostream"
#include"cstdio"
#include"queue"
#include"algorithm"
using namespace std;
struct Cow{int s,f;}cow[101000];
bool cmp(const Cow &a,const Cow &b)
{
    if(a.s!=b.s)
       return a.s>b.s;
    else
        return a.f<b.f;
}
int main()
{
    priority_queue<int>Q;
    int n,c,f,i,nu,dpl[101000],dpr[101000];
    while(scanf("%d%d%d",&n,&c,&f)!=EOF)
    {
        for(i=1;i<=c;i++)
            cin>>cow[i].s>>cow[i].f;
        sort(cow+1,cow+1+c,cmp);
        nu=n/2;
        int sum=0;
        while(!Q.empty())
           Q.pop();
        for(i=1;i<=nu;i++)
           Q.push(cow[i].f),sum+=cow[i].f;
        dpl[nu]=sum;
        for(i=nu+1;i<=c;i++)
        {
           if(cow[i].f>=Q.top())
              dpl[i]=sum;
           else
           {
              sum=sum-Q.top()+cow[i].f;
              Q.pop();
              Q.push(cow[i].f);
              dpl[i]=sum;
           }
        }
        sum=0;
        while(!Q.empty())
           Q.pop();
        for(i=c;i>=c-nu+1;i--)
            Q.push(cow[i].f),sum+=cow[i].f;
        dpr[c-nu+1]=sum;
        for(i=c-nu;i>=1;i--)
        {
            if(cow[i].f>=Q.top())
               dpr[i]=sum;
            else
           {
               sum=sum-Q.top()+cow[i].f;
               Q.pop();
               Q.push(cow[i].f);
               dpr[i]=sum;
           }
        }
        bool flag=false;
        for(i=nu+1;i<=c-nu;i++)
        {
            if(cow[i].f+dpl[i-1]+dpr[i+1]<=f)
            {
               cout<<cow[i].s<<endl;
               flag=true;
               break;
            }
        }
        if(!flag)
           cout<<"-1"<<endl;
    }

}



            
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lemon_boluo/article/details/49893891
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭