hdu 4302 Holedox Eating
http://acm.hdu.edu.cn/showproblem.php?pid=4302
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <vector> 5 #include <algorithm> 6 #define maxn 100010 7 using namespace std; 8 9 int n,m,a,b; 10 int main() 11 { 12 int t; 13 scanf("%d",&t); 14 int cas=1; 15 while(t--) 16 { 17 priority_queue<int,vector<int>,greater<int> >q; 18 priority_queue<int,vector<int>,less<int> >p; 19 scanf("%d%d",&n,&m); 20 int pos=0; 21 int dir=1; 22 int ans=0; 23 for(int i=1; i<=m; i++) 24 { 25 scanf("%d",&a); 26 if(a==0) 27 { 28 scanf("%d",&b); 29 if(b>=pos) q.push(b); 30 else p.push(b); 31 } 32 else 33 { 34 if(!q.empty()&&!p.empty()) 35 { 36 int m1=q.top(); 37 int m2=p.top(); 38 if(m1-pos<pos-m2) 39 { 40 dir=1; 41 ans+=m1-pos; 42 pos=m1; 43 q.pop(); 44 } 45 else if(m1-pos>pos-m2) 46 { 47 dir=-1; 48 ans+=pos-m2; 49 pos=m2; 50 p.pop(); 51 } 52 else if(dir==1) 53 { 54 ans+=m1-pos; 55 pos=m1; 56 q.pop(); 57 } 58 else 59 { 60 ans+=pos-m2; 61 pos=m2; 62 p.pop(); 63 } 64 } 65 else if(!q.empty()) 66 { 67 dir=1; 68 ans+=q.top()-pos; 69 pos=q.top(); 70 q.pop(); 71 } 72 else if(!p.empty()) 73 { 74 dir=-1; 75 ans+=pos-p.top(); 76 pos=p.top(); 77 p.pop(); 78 } 79 } 80 } 81 printf("Case %d: %d\n",cas,ans); 82 cas++; 83 } 84 return 0; 85 }