HDU 4302 Holedox Eating(优先队列)

Holedox Eating

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

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 <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define mod 1000000000
#define mem(a) memset(a,0,sizeof(a))

using namespace std;
const int maxn = 1000000 + 5, inf = 0x3f3f3f;
//自定义小于
struct cmp{
    bool operator()(int x,int y){
        return x>y;
    }
};
priority_queue<int> q2;
priority_queue<int,vector<int>,cmp> q1;
int main(){
    int kase,t,ans,a,b,l,n,x,k=0;
    scanf("%d",&kase);
    while(kase--){
        ans = 0;
        t = 1;
        x = 0;
        while(!q1.empty()) q1.pop();
        while(!q2.empty()) q2.pop();
        scanf("%d %d",&l,&n);
        while(n--){
            scanf("%d",&a);
            if(a==0){
                scanf("%d",&b);
                if(b>=x) q1.push(b);
                else q2.push(b);
            }
            else{
                if(!q1.empty()&&!q2.empty()){
                    int temp1 = q1.top();
                    int temp2 = q2.top();
                    if(temp1-x<x-temp2){
                        t = 1;
                        ans += q1.top()-x;
                        x = q1.top();
                        q1.pop();
                    }
                    else if(temp1-x>x-temp2){
                        t = 0;
                        ans += x-q2.top();
                        x = q2.top();
                        q2.pop();
                    }
                    else if(t==1){
                        ans += q1.top() - x;
                        x = q1.top();
                        q1.pop();
                    }
                    else{
                        ans += x - q2.top();
                        x = q2.top();
                        q2.pop();
                    }
                }
                else if(!q1.empty()){
                    t = 1;
                    ans += q1.top() - x;
                    x = q1.top();
                    q1.pop();
                }
                else if(!q2.empty()){
                    t = 0;
                    ans += x - q2.top();
                    x = q2.top();
                    q2.pop();
                }
            }
        }
        printf("Case %d: %d\n",++k,ans);
    }
    return 0;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值