HDOJ 4302 - Holedox Eating 优先队列
2013-08-09 16:30
253 查看
题意:
Holedox在一个一维的坐标轴上...初始在0点...现在某些个时刻..会在某些位置放置蛋糕..Holedox在一些时刻进食..Holedox进食满足每次找距离最近的..若两头距离一样.按照原路继续走....问最终Holedox走了多长的路...
题解:
优先队列的典型应用....由于每走一步必定不会跨过蛋糕...用两个队列来维护哪些数在当前位置左侧...哪些在右侧....
Program:
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; }
相关文章推荐
- hdu 4302 Holedox Eating(优先队列)
- HDU/HDOJ----4302 Holedox Eating
- hdu - 4302 Holedox Eating (优先队列)
- HDOJ 4302 —— 线段树 或 优先队列
- Hdoj 4302 Holedox Eating 【multiset】
- HDOJ 4302 Holedox Eating (multiset || 线段树)
- HDU 4302 Holedox Eating (两个优先队列)
- HDU - 4302 :Holedox Eating ,线段树、树状数组+二分,优先队列
- hdoj 4302 Holedox Eating 【优先队列】
- multiset || 线段树 HDOJ 4302 Holedox Eating
- HDU 4302 Holedox Eating
- HDOJ 1873 看病要排队(优先队列)
- HDOJ 1789 Doing Homework again (贪心&&优先队列)
- HDOJ-3785 寻找大富翁(优先队列)
- hdoj 2653 Waiting ten thousand years for Love(优先队列+BFS)
- hdu 4302 Holedox Eating
- HDU 4302 Holedox Eating (二分+树状数组维护)
- hdu 4302 Holedox Eating
- (转)HDOJ 4006 The kth great number(优先队列)
- HDOJ 1873 看病要排队(优先队列)