Holedox Eating
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2704 Accepted Submission(s): 898
Problem Description
Holedox 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.
Input
The 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.
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.
Output
Output the total distance Holedox will move. Holedox don’t need to return to the position 0.
Sample Input
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
Sample Output
Case 1: 9 Case 2: 4 Case 3: 2每次保证到达距离当前最近的位置,若两边相同则选择方向不变的那个方向由于数据较多,不可以直接选择,那样时间复杂度较高O(N*N),可以用优先级队列降低时间复杂度,当前位置左边的用从大到小排列,右边的从小到大排列,每次移动前比较两个方向的移动距离,左边的肯定与最大的比较,左边的肯定与最小的就比较,于是有了优先级队列的思想,左边大顶堆,右边的小顶堆,总的时间复杂度为O(n*log),应该可以接受。#include<iostream> #include<algorithm> #include<queue> #include<cstdio> #include<cmath> using namespace std; struct cmp { bool operator()(int a,int b) { return a>b; } }; int main() { int i,cas,len,n,cmd,pos,dis,cur,der,tag=1; int tmp1; int tmp2; cin>>cas; while(cas--) { dis=0; cur=0; der=1; priority_queue<int>bef; priority_queue<int,vector<int>,cmp>aft; scanf("%d%d",&len,&n); for(i=0;i<n;i++) { scanf("%d",&cmd); if(!cmd) { scanf("%d",&pos); if(pos<cur) bef.push(pos); else { aft.push(pos); } } else { if(!aft.empty()&&!bef.empty()) { tmp1=bef.top(); tmp2=aft.top(); if(abs(cur-tmp1)<abs(tmp2-cur)) { der=-1; dis+=abs(cur-tmp1); cur=tmp1; bef.pop(); } else if(abs(cur-tmp1)>abs(tmp2-cur)) { der=1; dis+=abs(tmp2-cur); cur=tmp2; aft.pop(); } else { dis+=abs(cur-tmp1); if(der==-1) { cur=tmp1; bef.pop(); } else { cur=tmp2; aft.pop(); } } } else { if(!bef.empty()) { tmp1=bef.top(); der=-1; dis+=abs(cur-tmp1); cur=tmp1; bef.pop(); } else if(!aft.empty()) { tmp1=aft.top(); der=1; dis+=abs(tmp1-cur); cur=tmp1; aft.pop(); } else { continue; } } } } printf("Case %d: %d\n",tag++,dis); } return 0; }