hdu 4302 Holedox Eating

来源:互联网 发布:淘宝客服一天接待人数 编辑:程序博客网 时间:2024/06/04 23:37

优先队列做法:用两个优先队列维护信息,蛋糕大于他当前所在位置的,用从小到大的优先级;蛋糕小于等于他当前所在位置,用从大到小的优先级。两个队列取出的元素再进行比较

#include <iostream>#include <queue>#include <cstdio>using namespace std;struct cmp{    bool operator () (int x,int y)    {        return x > y;    }};priority_queue<int , vector<int>, cmp>q; //小到大priority_queue<int>qq; //大到小int l,n;int tt,pos,x=0;int sum=0,index = 1;void Forword() //向前走{    int tmp = q.top();    q.pop();    sum += tmp - x;    index = 1;    x = tmp;}void Back() //向后走{    int tmp = qq.top();    qq.pop();    sum += x - tmp;    index = 0;    x = tmp;}int main(){    int t,Case=1;    cin >> t;    while(t--)    {        while(!q.empty())            q.pop();        while(!qq.empty())            qq.pop();        scanf("%d%d",&l,&n);        x=0;        sum=0;        index = 1;        for(int i=0; i<n; i++)        {            scanf("%d",&tt);            if(!tt)            {                cin >> pos;                if(pos <= x)                    qq.push(pos);                else                    q.push(pos);            }            else            {                if(q.empty() && qq.empty())                    continue;                if(q.empty() && ! qq.empty())                {                    Back();                    continue;                }                if(qq.empty() && !q.empty())                {                    Forword();                    continue;                }                if(!q.empty() && !qq.empty())                {                    int tmp1 = q.top();                    int tmp2 = qq.top();                    if(tmp1 - x < x - tmp2 )                    {                        Forword();                    }                    else if(tmp1 - x > x - tmp2)                    {                        Back();                    }                    else                    {                        if(!index)                            Back();                        else                            Forword();                    }                }            }        }        printf("Case %d: %d\n",Case++,sum);    }    return 0;}

原创粉丝点击