Holedox Eating
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 857 Accepted Submission(s): 292
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.
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
Source
/*第一次用优先队列写,无比的不顺啊~~~
不过个人还是认为这个题用线段树比这个麻烦
有结构体定义的,所以push进去的结构体,但是因为结构体的元素只有一个,所以是赋值给它的x
因为测试数据是多组,所以但是前面蛋糕没有取完会影响后面数据的结构,所以一定一定要在测试组数输入后定义优先队列
既然是队列必然应该考虑是否为空的问题,用赋初始最大值来选择最小,同时判了是否为空*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define maxc 1000005
using namespace std;
struct node1
{
int x;
friend bool operator<(node1 a,node1 b)
{
return a.x<b.x;
}
};
struct node2
{
int x;
friend bool operator <(node2 a ,node2 b)
{
return a.x>b.x;
}
};
int main()
{
int t;
int l,n;
scanf("%d",&t);
int r=1;
while(t--)
{
priority_queue<node1> p;//左边的最大值
priority_queue<node2> q;//右边的最小值
scanf("%d%d",&l,&n);
int flg=0;
int now=0;
int sum=0;
int dis;
while(n--)
{
int test;
scanf("%d",&test);
if(test==0)
{
node1 tmp;
int mm;
scanf("%d",&mm);
node2 tp;
if(mm<=now)
{
tmp.x=mm;
p.push(tmp);
}
else
{
tp.x=mm;
q.push(tp);
}
}
else if(test==1)
{
int x,y;
x=y=maxc; //赋最大值
if(!p.empty())
x=now-p.top().x;
if(!q.empty())
y=q.top().x-now;
if(x==y && x==maxc)
continue;
if(y<x)
{
sum+=y;
now=q.top().x;
q.pop();
if(y!=0)
dis=1;
}
else if(y>x)
{
sum+=x;
now=p.top().x;
p.pop();
if(x!=0)
dis=2;
}
else
{
if(dis==1)
{
sum+=y;
now=q.top().x;
q.pop();
}
else
{
sum+=x;
now=p.top().x;
p.pop();
}
}
}
}
printf("Case %d: %d\n",r++,sum);
}
return 0;
}