zcmu1052

1052: Holedox Eating

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 57   Solved: 50
[ Submit][ Status][ Web Board]

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

HINT

Source

线段树加二分,O(nlog^2n)
#include <iostream>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define pi acos(-1)
#define mod 100000007
struct tree {
	int l;
	int r;
	int sum;
}a[400005];
void maketree(int l,int r,int o) {
	if (l == r) {
		a[o].l = a[o].r = l;
		a[o].sum = 0;
		return;
	}
	else {
		a[o].l = l, a[o].r = r;
		a[o].sum = 0;
		int lo = o << 1, ro = (o << 1) | 1;
		int mid = (l + r) >> 1;
		maketree(l, mid,lo);
		maketree(mid + 1, r, ro);
	}
}
void updatetree(int l, int r, int o,int value) {
	if (a[o].l > r || a[o].r < l) {
		return;
	}
	if (a[o].l >= l && a[o].r <= r) {
		a[o].sum += value;
		return;
	}
	updatetree(l, r, o << 1,value);
	updatetree(l, r, (o << 1) | 1, value);
	a[o].sum = a[(o << 1)].sum + a[(o << 1) | 1].sum;
}
int querytree(int l, int r, int o) {
	if (a[o].l > r || a[o].r < l) {
		return 0;
	}
	if (a[o].l >= l && a[o].r <= r) {
		return a[o].sum;
	}
	return querytree(l, r, o << 1) + querytree(l, r, (o << 1) | 1);
}
int main() {
	int T, L, n, k = 0;
	cin >> T;
	while (T--) {
		k++;
		scanf("%d%d", &L, &n);
		maketree(0, L, 1);
		int x, y, cur = 0, sum = 0;
		bool flag = true;
		while (n--) {
			scanf("%d", &x);
			if (x == 0) {
				scanf("%d", &y);
				updatetree(y, y, 1,1);
			}
			else {
				int l = 0, r = cur - 1, mid;
				int leftans, rightans;
				leftans = rightans = -1;
				while (l <= r) {
					mid = (l + r) >> 1;
					int t = querytree(mid, r, 1);
					if (t > 0) {
						leftans = mid;
						l = mid + 1;
					}
					else {
						t = querytree(l, mid - 1, 1);
						if (t > 0) {
							leftans = mid - 1;
							r = mid - 1;
						}
						else {
							break;
						}
					}
				}
				l = cur, r = L;
				while (l <= r) {
					mid = (l + r) >> 1;
					int t = querytree(l, mid, 1);
					if (t > 0) {
						rightans = mid;
						r = mid - 1;
					}
					else {
						t = querytree(mid + 1, r, 1);
						if (t > 0) {
							rightans = mid + 1;
							l = mid + 1;
						}
						else {
							break;
						}
					}
				}
				if (leftans != -1 && rightans != -1) {
					if (abs(leftans - cur) == abs(rightans - cur)) {
						if (flag) {
							updatetree(rightans, rightans, 1, -1);
							sum += abs(rightans - cur);
							cur = rightans;
							flag = true;
						}
						else {
							updatetree(leftans, leftans, 1, -1);
							sum += abs(leftans - cur);
							cur = leftans;
							flag = false;
						}
					}
					else if (abs(leftans - cur) > abs(rightans - cur)) {
						updatetree(rightans, rightans, 1, -1);
						sum += abs(rightans - cur);
						cur = rightans;
						flag = true;
					}
					else {
						updatetree(leftans, leftans, 1, -1);
						sum += abs(leftans - cur);
						cur = leftans;
						flag = false;
					}
				}
				else if(leftans == -1 && rightans != -1){
					updatetree(rightans, rightans, 1, -1);
					sum += abs(rightans - cur);
					cur = rightans;
					flag = true;
				}
				else if (leftans != -1 && rightans == -1) {
					updatetree(leftans, leftans, 1, -1);
					sum += abs(leftans - cur);
					cur = leftans;
					flag = false;
				}
			}
		}
		printf("Case %d: ", k);
		cout << sum << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值