hdu - 4302 Holedox Eating (優先隊列)

題目:

Holedox Eating


題意:

有一根長爲L的管道和一個初始位置在最左端的動物,輸入數據"0 x"表示此時在管道的左數x位置出現了一個食物,"1"表示動物想喫東西了:他會喫最近的食物,如果有多個最近的食物,就按照最近一次的移動方法移動,求所有命令結束後的動物移動路程。


思路:構造兩個優先隊列left和right,因爲動物和食物的相對位置確定以後,一定是不會再改的。直接模擬所有情況即可


代碼:

//#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 t;
    scanf("%d",&t);

    for(int ka = 1 ; ka <= t ; ka++)
    {
        int L,n;
        scanf("%d%d",&L,&n);
        int pos=0;
        int last=1;
        int dis=0;

        priority_queue< int , vector<int> , greater<int> > right;
        priority_queue< int > left;

        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 pos1 = left.top();
                    int pos2 = right.top();

                    if(pos - pos1 == pos2 - pos)
                    {
                        if(last == 1)
                        {
                            dis+=pos2-pos;
                            pos = pos2;
                            right.pop();
                        }
                        else
                        {
                            dis+=pos-pos1;
                            pos = pos1;
                            left.pop();
                        }
                    }
                    else if(pos - pos1 > pos2 - pos)
                    {
                        right.pop();
                        dis+=pos2-pos;
                        pos = pos2;
                        last=1;
                    }
                    else if(pos - pos1 < pos2 - pos)
                    {
                        left.pop();
                        if(pos != pos1)
                        {
                            dis+=pos-pos1;
                            pos = pos1;
                            last=-1;
                        }
                    }
                }
                else if(!left.empty())
                {
                    int pos1 = left.top();
                    left.pop();
                    if(pos != pos1)
                        {
                            dis+= pos-pos1;
                            pos = pos1;
                            last=-1;
                        }
                }
                else if(!right.empty())
                {
                    int pos2 = right.top();
                    right.pop();
                    dis+=pos2-pos;
                    pos = pos2;
                    last=1;
                }
            }
        }

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

    return 0;

}


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