hdoj 4302 Holedox Eating 【优先队列】

来源:互联网 发布:js载入html 编辑:程序博客网 时间:2024/05/18 14:24

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
310 80 10 510 20 0111 10 70 10 510 20 01110 80 10 10 510 20 011

Sample Output
Case 1: 9Case 2: 4Case 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;}


0 0
原创粉丝点击