| 
 | ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
| 疯狂WA后发现,无限大设置的数太小了- -orz 赠送C++代码#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<algorithm>
#define TIME std::ios::sync_with_stdio(false);
#define LL long long
#define MAX 666666
#define MAX_NUM 100005
using namespace std;
struct tai {
	int x1, x2, h;
};
bool cmp(tai a, tai b) {
	return a.h < b.h;
}
vector<tai> v;
int notai(int up_x, int h, int i, int max) {
	for (i--; i >= 0; i--) {
		if (up_x >= v[i].x1 && up_x <= v[i].x2) {
			return i;
		}
	}
	return -1;
}
int Min(int a, int b) {
	return a < b ? a : b;
}
int main() {
	TIME
		int N;
	cin >> N;
	while (N--) {
		int n, bx, bh, max;
		int left[MAX_NUM];
		int right[MAX_NUM];
		cin >> n >> bx >> bh >> max;
		while (n--) {
			tai t;
			cin >> t.x1 >> t.x2 >> t.h;
			v.push_back(t);
		}
		sort(v.begin(), v.end(), cmp);
		int flag = notai(bx, bh, v.size(), max);
		if (flag == -1) {
			cout << bh << endl;
		}
		else {
			for (int i = 0; i < v.size(); i++) {
				int temp = notai(v[i].x1, v[i].h, i, max);
				if (temp == -1) {
					if (v[i].h > max) {
						left[i] = MAX_NUM;
					}
					else {
						left[i] = v[i].h;
					}
				}
				else {
					if (v[i].h - v[temp].h > max) {
						left[i] = MAX_NUM;
					}
					else {
						left[i] = v[i].h - v[temp].h;
						if (left[temp] + v[i].x1 - v[temp].x1 < right[temp] + v[temp].x2 - v[i].x1) {
							left[i] += left[temp] + v[i].x1 - v[temp].x1;
						}
						else {
							left[i] += right[temp] + v[temp].x2 - v[i].x1;
						}
					}
				}
				temp = notai(v[i].x2, v[i].h, i, max);
				if (temp == -1) {
					if (v[i].h > max) {
						right[i] = MAX_NUM;
					}
					else {
						right[i] = v[i].h;
					}
				}
				else {
					if (v[i].h - v[temp].h > max) {
						right[i] = MAX_NUM;
					}
					else {
						right[i] = v[i].h - v[temp].h;
						if (left[temp] + v[i].x2 - v[temp].x1 < right[temp] + v[temp].x2 - v[i].x2) {
							right[i] += left[temp] + v[i].x2 - v[temp].x1;
						}
						else {
							right[i] += right[temp] + v[temp].x2 - v[i].x2;
						}
					}
				}
			}
			int ans = bh - v[flag].h;
			if (left[flag] + bx - v[flag].x1 < right[flag] + v[flag].x2 - bx) {
				ans += left[flag] + bx - v[flag].x1;
			}
			else {
				ans += right[flag] + v[flag].x2 - bx;
			}
			cout << ans << endl;
		}
		v.clear();
	}
	return 0;
}Followed by: Post your reply here: | 
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator