hdu 4302 #priority_queue

来源:互联网 发布:linux 查看用户组 编辑:程序博客网 时间:2024/05/22 13:57

赛后一听到这题维护两个priority_queue,瞬间崩溃,苦逼了自己的那棵线段树。

就怕想不到呢。

Holedox Eating

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


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 <queue>using namespace std;#define INF 1000000int main(){    priority_queue<int,vector<int>,less<int> > lef;    priority_queue<int,vector<int>,greater<int> > rig;    int L,q,t,cas = 0;    int dist,cur;    int ll,rr = 0;    scanf("%d",&t);    while(t--)    {        while(!lef.empty())            lef.pop();        while(!rig.empty())            rig.pop();        cur = 0;        dist = 0;        scanf("%d%d",&L,&q);        int f,x,dirc = 1;        while(q--)        {            scanf("%d",&f);            if(f)            {                if(lef.empty())                    ll = INF;                else                    ll =cur - lef.top();                if(rig.empty())                    rr = INF;                else                    rr = rig.top() - cur;                if(ll != INF || rr != INF)                {                    if(ll == rr)                    {                        if(dirc)                        {                            dist += rr;                            cur = rig.top();                            rig.pop();                        }                        else                        {                            dist += ll;                            cur = lef.top();                            lef.pop();                        }                    }                    else if(ll < rr)                    {                        dist += ll;                        dirc = 0;                        cur = lef.top();                        lef.pop();                    }                    else                    {                        dist += rr;                        dirc = 1;                        cur = rig.top();                        rig.pop();                    }                }            }            else            {                scanf("%d",&x);                if(x >= cur)                    rig.push(x);                else                    lef.push(x);            }        }        printf("Case %d: %d\n",++cas,dist);    }    return 0;}


原创粉丝点击