HDU 4302 Holedox Eating (兩個優先隊列)

比賽時隊友嘗試用一個優先隊列做,後來發現用兩個更合適。

第一次用priority_queue。。。

每次在喫cake時儘量不改變方向,喫最近的cake,每次pop之後並不改變左右隊列。

菜鳥忘了刷新隊列WA了好幾次。。。。教訓啊T——T


HDU 4302

Holedox Eating

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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

//#pragma comment(linker, "/STACK:102400000,102400000")
#include "iostream"
#include "cstring"
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "sstream"
#include "queue"
#include "vector"
#include "string"
#include "stack"
#include "cstdlib"
#include "deque"
#include "fstream"
#include "map"
using namespace std;
typedef long long LL;
const int INF = 0x1fffffff;
const int MAXN = 1000000+100;
#define eps 1e-14
const int mod = 95041567;

int main()
{
    //freopen("in","r",stdin);
    //freopen("out","w",stdout);
    int L,n,pos,t,dir,dis;
    scanf("%d",&t);
    for (int kase=1; kase<=t; kase++)
    {
        dis=0;
        pos=0;
        dir=1;
        priority_queue<int ,vector<int> ,greater<int> > right;//之前放在main裏了。。。深刻教訓。。。。
        priority_queue<int> left;
        scanf("%d%d",&L,&n);//L似乎沒什麼用

        for (int i=0; i<n; i++)
        {
            int a,b;
            scanf("%d",&a);
            if (a==0)
            {
                scanf("%d",&b);
                if (b<=pos) left.push(b);//把小的和在一個位置的都放在左邊,後面處理時加一個判斷
                else right.push(b);
            }
            else if (a==1)
            {
                if (!left.empty()&&!right.empty())
                {
                    int posl=left.top();
                    int posr=right.top();
                    if (pos-posl==posr-pos)
                    {
                        if (dir==1)//判斷方向
                        {
                            dis+=posr-pos;
                            pos=posr;
                            right.pop();
                        }
                        else
                        {
                            dis+=pos-posl;
                            pos=posl;
                            left.pop();
                        }

                    }
                    else if (pos-posl>posr-pos)
                    {
                        dis+=posr-pos;
                        pos=posr;
                        dir=1;
                        right.pop();
                    }
                    else if (pos-posl<posr-pos)
                    {
                        if (posl!=pos)//特判
                        {
                            dis+=pos-posl;
                            pos=posl;
                            dir=-1;
                        }
                        left.pop();
                    }
                }
                else if (!left.empty())
                {
                    if (left.top()!=pos)//同上
                    {
                        dis+=pos-left.top();
                        pos=left.top();
                        dir=-1;//改變方向
                    }
                    left.pop();
                }
                else if (!right.empty())
                {
                    dis+=right.top()-pos;
                    pos=right.top();
                    dir=1;
                    right.pop();
                }

            }
        }
        printf("Case %d: %d\n",kase,dis);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章