您的位置:首页 > 其它

HDOJ 4302 - Holedox Eating 优先队列

2013-08-09 16:30 253 查看
题意:

Holedox在一个一维的坐标轴上...初始在0点...现在某些个时刻..会在某些位置放置蛋糕..Holedox在一些时刻进食..Holedox进食满足每次找距离最近的..若两头距离一样.按照原路继续走....问最终Holedox走了多长的路...

题解:

优先队列的典型应用....由于每走一步必定不会跨过蛋糕...用两个队列来维护哪些数在当前位置左侧...哪些在右侧....

Program:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<time.h>
#include<map>
#include<algorithm>
#define ll long long
#define eps 1e-5
#define oo 1000000007
#define pi acos(-1.0)
#define MAXN 100005
using namespace std;   
priority_queue<int> ql;
priority_queue<int,vector<int>,greater<int> > qr; //int型直接改变顺序
int main()
{                
       int T,cases,L,n,ans,now,face;;      
       scanf("%d",&T);
       for (cases=1;cases<=T;cases++)
       { 
                scanf("%d%d",&L,&n);
                while (!ql.empty()) ql.pop();
                while (!qr.empty()) qr.pop();
                now=ans=face=0;
                while (n--)
                {
                        int tp,x;
                        scanf("%d",&tp);
                        if (!tp) 
                        {
                               scanf("%d",&x);
                               if (x>=now) qr.push(x);
                                 else ql.push(x);
                        }else
                        {
                               if (!ql.size() && !qr.size()) continue;
                               if (!ql.size()) ans+=qr.top()-now,now=qr.top(),face=0,qr.pop();
                               else
                               if (!qr.size()) ans+=now-ql.top(),now=ql.top(),face=1,ql.pop();
                               else
                               if (now-ql.top()==qr.top()-now)
                               {
                                       if (!face) ans+=qr.top()-now,now=qr.top(),qr.pop();
                                         else ans+=now-ql.top(),now=ql.top(),ql.pop();
                               }
                               else
                               if (now-ql.top()<qr.top()-now)
                                      ans+=now-ql.top(),now=ql.top(),face=1,ql.pop();
                                 else ans+=qr.top()-now,now=qr.top(),face=0,qr.pop();
                        }
                }
                printf("Case %d: %d\n",cases,ans);
       }  
       return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: