Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

hdu 4302 Holedox Eating

$
0
0

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

#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;
}

作者:paradiserparadiser 发表于2013-5-21 20:28:40 原文链接
阅读:35 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>