hdoj 4302 Holedox Eating 【优先队列】

Holedox Eating

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3468    Accepted Submission(s): 1188


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
 
 
优先队列:一个记录左边食物,一个记录右边食物。
 
#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;
}


你可能感兴趣的:(hdoj 4302 Holedox Eating 【优先队列】)