Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3468 Accepted Submission(s): 1188
3 10 8 0 1 0 5 1 0 2 0 0 1 1 1 10 7 0 1 0 5 1 0 2 0 0 1 1 10 8 0 1 0 1 0 5 1 0 2 0 0 1 1
Case 1: 9 Case 2: 4 Case 3: 2
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<queue> #include<stack> #include<vector> #include<set> #include<algorithm> using namespace std; priority_queue<int,vector<int>,less<int> >left; priority_queue<int,vector<int>,greater<int> >right; int main() { int t,n,m; int i,j,k=1; int op,pos; int now;//当前所在位置 int move;//记录总移动数 int face;//记录上一次移动方向 1为右 0为左 scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); while(!left.empty()) { left.pop(); } while(!right.empty()) { right.pop(); } move=now=0;face=1;//第一步必向右 while(m--) { scanf("%d",&op); if(!op) { scanf("%d",&pos); if(pos>=now) right.push(pos); else left.push(pos); } else { if(left.empty()&&right.empty()) { continue; } if(left.empty()) { move+=right.top()-now; now=right.top(); right.pop(); face=1; continue; } if(right.empty()) { move+=now-left.top(); now=left.top(); left.pop(); face=0; continue; } if(now-left.top()<right.top()-now) { move+=now-left.top(); now=left.top(); left.pop(); face=0; } else if(now-left.top()>right.top()-now) { move+=right.top()-now; now=right.top(); right.pop(); face=1; } else { if(face) { move+=right.top()-now; now=right.top(); right.pop(); face=1; } else { move+=now-left.top(); now=left.top(); left.pop(); face=0; } } } } printf("Case %d: ",k++); printf("%d\n",move); } return 0; }