Hduoj4302【优先队列】

/*Holedox Eating 
Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 4
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. 
 
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

Author
BUPT
 
Source
2012 Multi-University Training Contest 1
*/
/*
HDU 4302
G++ 281ms 532K

*/

#include<stdio.h>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
struct cmp
{
    bool operator()(int x,int y)
    {
        return x>y;//从小到大 
    }
};
priority_queue<int,vector<int>,cmp>q;//从大到小队列 
priority_queue<int>q2;//从小到大队列 
int main()
{
    int T, L, n;
    int A, B; 
    int iCase = 0;
    scanf("%d",&T);
    while(T--)
    {
        iCase++;
        scanf("%d%d", &L, &n);
        while(!q.empty())   
		q.pop();//清空 
        while(!q2.empty())  
		q2.pop();//清空 
        int x=0, ans=0, t=1;
        while(n--)
        {
            scanf("%d", &A);
            if(A == 0)
            {
                scanf("%d", &B);
                if(B >= x ) 
				q.push(B);//如果cake在当前位置的右边放入优先队列 
                else 
				q2.push(B);//否则直接放入队列 
            }
            else
            {
                if( !q.empty() && !q2.empty() )
                {
                    int temp1 = q.top();
                    int temp2 = q2.top();
                    if(temp1-x < x-temp2)//如果右边的蛋糕近 
                    {
                        t = 1;//方向向右 
                        ans += q.top() - x;//记录距离 
                        x = q.top();//更改当前位置 
                        q.pop();//删除 
                    }
                    else if(temp1-x > x-temp2)//如果左边的蛋糕近 
                    {
                        t = -1;//方向向左 
                        ans += x - q2.top();
                        x = q2.top();//更改当前位置 
                        q2.pop();//删除 
                    }
                    else if(t == 1)//如果一样近并且上次方向向右 
                    {
                        ans += q.top() - x;//右移 
                        x = q.top();//更改当前位置 
                        q.pop();//删除 
                    }
                    else
                    {
                        ans += x - q2.top();//左移 
                        x = q2.top();//更改当前位置 
                        q2.pop();//删除 
                    }
                }
                else if( !q.empty() )//如果左边没有蛋糕 
                {
                    t = 1;//方向向右 
                    ans += q.top() - x;//右移 
                    x = q.top();//更改当前位置 
                    q.pop();//删除 
                }
                else if( !q2.empty() )//如果右边没有 蛋糕 
                {
                    t = -1;//方向向左 
                    ans += x-q2.top();//左移 
                    x = q2.top();//更改当前位置 
                    q2.pop();//删除 
                }
            }

        }
        printf("Case %d: %d\n", iCase, ans);
    }
    return 0;
}


题意:给出一根管子,从0开始编号到L,给出2种命令,一种是在某个编号处放蛋糕,一种是老鼠吃蛋糕,老鼠的初始位置为0,并且老鼠会选择最近的蛋糕吃,当左右的蛋糕一样近时,则继续以原来的方向前进,吃完所有蛋糕后不需要回到0处,问移动距离。

思路:用2个有限队列分别表示老鼠当前位置左右的蛋糕位置,并且始终可以挑选出距离当前位置最近的蛋糕,记录吃完所有蛋糕所花的时间。

你可能感兴趣的:(Hduoj4302【优先队列】)