Hduoj4302【优先队列】

来源:互联网 发布:ipad cellular 知乎 编辑:程序博客网 时间:2024/06/13 06:39
/*Holedox Eating Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)Total Submission(s) : 7   Accepted Submission(s) : 4Problem DescriptionHoledox is a small animal which can be considered as one point. It lives in a straight pipe whose length is L. Holedox can only move along the pipe. Cakes may appear anywhere in the pipe, from time to time. When Holedox wants to eat cakes, it always goes to the nearest one and eats it. If there are many pieces of cake in different directions Holedox can choose, Holedox will choose one in the direction which is the direction of its last movement. If there are no cakes present, Holedox just stays where it is. InputThe input consists of several test cases. The first line of the input contains a single integer T (1 <= T <= 10), the number of test cases, followed by the input data for each test case.The first line of each case contains two integers L,n(1<=L,n<=100000), representing the length of the pipe, and the number of events. The next n lines, each line describes an event. 0 x(0<=x<=L, x is a integer) represents a piece of cake appears in the x position; 1 represent Holedox wants to eat a cake. In each case, Holedox always starts off at the position 0.  OutputOutput the total distance Holedox will move. Holedox don’t need to return to the position 0. Sample Input310 80 10 510 20 0111 10 70 10 510 20 01110 80 10 10 510 20 011 Sample OutputCase 1: 9Case 2: 4Case 3: 2AuthorBUPT Source2012 Multi-University Training Contest 1*//*HDU 4302G++ 281ms 532K*/#include<stdio.h>#include<queue>#include<iostream>#include<algorithm>using namespace std;struct cmp{    bool operator()(int x,int y)    {        return x>y;//从小到大     }};priority_queue<int,vector<int>,cmp>q;//从大到小队列 priority_queue<int>q2;//从小到大队列 int main(){    int T, L, n;    int A, B;     int iCase = 0;    scanf("%d",&T);    while(T--)    {        iCase++;        scanf("%d%d", &L, &n);        while(!q.empty())   q.pop();//清空         while(!q2.empty())  q2.pop();//清空         int x=0, ans=0, t=1;        while(n--)        {            scanf("%d", &A);            if(A == 0)            {                scanf("%d", &B);                if(B >= x ) q.push(B);//如果cake在当前位置的右边放入优先队列                 else q2.push(B);//否则直接放入队列             }            else            {                if( !q.empty() && !q2.empty() )                {                    int temp1 = q.top();                    int temp2 = q2.top();                    if(temp1-x < x-temp2)//如果右边的蛋糕近                     {                        t = 1;//方向向右                         ans += q.top() - x;//记录距离                         x = q.top();//更改当前位置                         q.pop();//删除                     }                    else if(temp1-x > x-temp2)//如果左边的蛋糕近                     {                        t = -1;//方向向左                         ans += x - q2.top();                        x = q2.top();//更改当前位置                         q2.pop();//删除                     }                    else if(t == 1)//如果一样近并且上次方向向右                     {                        ans += q.top() - x;//右移                         x = q.top();//更改当前位置                         q.pop();//删除                     }                    else                    {                        ans += x - q2.top();//左移                         x = q2.top();//更改当前位置                         q2.pop();//删除                     }                }                else if( !q.empty() )//如果左边没有蛋糕                 {                    t = 1;//方向向右                     ans += q.top() - x;//右移                     x = q.top();//更改当前位置                     q.pop();//删除                 }                else if( !q2.empty() )//如果右边没有 蛋糕                 {                    t = -1;//方向向左                     ans += x-q2.top();//左移                     x = q2.top();//更改当前位置                     q2.pop();//删除                 }            }        }        printf("Case %d: %d\n", iCase, ans);    }    return 0;}


题意:给出一根管子,从0开始编号到L,给出2种命令,一种是在某个编号处放蛋糕,一种是老鼠吃蛋糕,老鼠的初始位置为0,并且老鼠会选择最近的蛋糕吃,当左右的蛋糕一样近时,则继续以原来的方向前进,吃完所有蛋糕后不需要回到0处,问移动距离。

思路:用2个有限队列分别表示老鼠当前位置左右的蛋糕位置,并且始终可以挑选出距离当前位置最近的蛋糕,记录吃完所有蛋糕所花的时间。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 吃了发霉的蚝油怎么办 蛋皮干燥起皮怎么办 wps卡顿资料没保存怎么办 手指受伤肉掉了怎么办 手机被wifi禁了怎么办 母乳一边是咸的怎么办 tcl电视蓝频了怎么办 长虹电视蓝频了怎么办 电视突然蓝频了怎么办 海信电视蓝频了怎么办 连网电视蓝频了怎么办 英雄联盟画面卡顿怎么办 长残了怎么办原来很帅 被吓到了怎么办没精神 宝宝吓着怎么办最有效 4个月婴儿易惊吓怎么办 心里有问题的人怎么办 减肥的时候想吃东西怎么办 大联盟ping很高怎么办 酷派手机弹广告怎么办 孕早期肚子紧绷怎么办 怀孕2个月同房了怎么办 怀孕前三月同房了怎么办 人流前三天同房了怎么办 人流后三天同房了怎么办 吃避孕药后月经量少怎么办 排卵日同房不想怀孕怎么办 排卵日同房没有怀孕怎么办 想怀孕想生儿子怎么办 到期大姨妈不来怎么办 怕怀孕月经不来怎么办 宫颈环扎后宫缩怎么办 来例假刺激外阴高潮了怎么办 客人消费完说没钱怎么办? 孕13周血糖偏高怎么办 餐后血糖高14点怎么办 餐后血糖20多怎么办 空腹血糖6.6饭后9.2怎么办 键盘shift键失效了怎么办 灵异附身夫妻不合怎么办 鞋子买大了半码怎么办