您的位置:首页 > 其它

HDOJ 4302 —— 线段树 或 优先队列

2013-10-01 17:39 369 查看


Holedox Eating

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

Total Submission(s): 2933    Accepted Submission(s): 986


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

 

Author

BUPT

 

Source

2012 Multi-University Training Contest 1

 

Recommend

zhuyuanchen520

 
首先,线段树有两种思路,第一种是去维护区间中的食物数sum , 第二种是去维护区间最大值和最小值,每次去比较左区间的最大值和右区间的最小值。
然后,优先队列的方法比较好写。在动物两边建两个堆每次取左堆的最大值和右堆的最小值即可。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//#define mid ((l + r) >> 1)
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 100000 + 500;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const double e = 2.718281828;
#define eps 1e-8
#define DEBUG 1
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")__int64 a[10050];
int n;
struct Node
{
int l , r  , sum;
}t[MAXN << 2];
int num[MAXN];
int place , suml , sumr , to , ans , L , R;
void build(int l , int r , int rt)
{
t[rt].r = r , t[rt].l = l , t[rt].sum = 0;
if(l == r)return;
int mid = (l + r) >> 1;
build(lson) , build(rson);
}
void PushUp(int rt)
{
t[rt].sum = t[rt << 1].sum + t[rt << 1 | 1].sum;
}
void update(int rt , int pos , int num)
{
if(t[rt].l == t[rt].r)
{
t[rt].sum += num;
return;
}
int mid = (t[rt].l + t[rt].r) >> 1;
if(mid >= pos) update(rt << 1 , pos , num);
else update(rt << 1 | 1 ,  pos , num);
PushUp(rt);
}
int Search(int rt , int d)
{
if(t[rt].l == t[rt].r)
{
return t[rt].r;
}
if(t[rt << 1].sum >= d)return Search(rt << 1 , d);
else return Search(rt << 1 | 1 , d - t[rt << 1].sum);
}
void EatLeft()
{
to = 0;
suml -= num[L];
update(1 , L , -num[L]);
num[L]--;
ans += place - L;
place = L;

}
void EatRight()
{
to = 1;
sumr -= num[R];
update(1 , R , -num[R]);
num[R]--;
ans += R - place;
place = R;
}
int main()
{
int t;
scanf("%d" , &t);
FORR(kase , 1 , t)
{
int op , add , m;
ans = 0;
to = 1;
place = suml = sumr = 0;
scanf("%d%d" , &n , &m);
build(0 , n , 1);
clr(num , 0);
while(m--)
{
scanf("%d" , &op);
if(op == 0)
{
scanf("%d" , &add);
num[add] ++;
if(add != place)
{
update(1 , add , 1);
}
if(add < place)suml ++;
else if(add > place)sumr ++;
}
else
{
if(num[place])
{
num[place] --;
continue;
}
else if(suml ==0 && sumr == 0)continue;
else if(sumr == 0)
{
L = Search(1 , suml);
EatLeft();
}
else if(suml == 0)
{
R = Search(1 , 1);
EatRight();
}
else if(suml > 0 && sumr > 0)
{
L = Search(1 , suml);
R = Search(1 , suml + 1);
if(place - L < R - place || (place * 2 == L + R && to == 0))
{
EatLeft();
}
else EatRight();
}
}
}

printf("Case %d: %d\n" , kase , ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM 线段树 优先队列