比赛 不平凡的世界 评测结果 AAAAAAAAAA
题目名称 不平凡的boss 最终得分 100
用户昵称 膜拜神犇王梦迪 运行时间 1.582 s
代码语言 C++ 内存使用 2.69 MiB
提交时间 2015-11-05 11:49:04
显示代码纯文本
#include <algorithm>
#include <cstdio>
#include <queue> 
#include <set> 
using namespace std;
const int kN = 1e5+10;
const int INF = 1e9+10;
int N;
struct Mon {
	int a, b, c;
	bool operator < (const Mon & m) const {
		if (a != m.a) return a < m.a;
		if (b != m.b) return b < m.b;
		if (c != m.c) return c < m.c;
		return false;
	}
}mon[kN];


int renb[kN], tmpbcnt[kN];

struct Solver2 {
	int bval[kN];
	bool inq[kN];
	set<int> q;
	priority_queue<pair<int, pair<int, int> > > ans;
	int find_pre(set<int>::iterator it) {
		if (it == q.begin()) return 0;
		--it;
		return *it;
	}
	int find_suf(set<int>::iterator it) {
		// printf("i = %d\n", *it);
		++it;
		if (it == q.end()) return 0;
		// printf("suf = %d\n", *it);
		return *it;
	}
	void insert(int a, int b) {
		bval[a] = b;
		set<int>::iterator ait = q.insert(a).first; inq[a] = true;
		
		int r = find_suf(ait);
		if (r && b <= bval[r]) {
			inq[a] = false;
			q.erase(ait);
			return;
		}
		
		int l = find_pre(ait);
		while (l && bval[l] <= b) {
			inq[l] = false;
			q.erase(q.find(l));
			l = find_pre(ait);
		}
	
		ans.push(make_pair(-(renb[l]+bval[a]), make_pair(l, a)));
		ans.push(make_pair(-(renb[a]+bval[r]), make_pair(a, r)));
	}
	int get() {
		while(true) {
			pair<int, pair<int, int> > t = ans.top();
			int ret = t.first, i = t.second.first, j = t.second.second;
			// printf("find_suf[%d] = %d\n", i, find_suf(q.find(i)));
			if (inq[i] && inq[j] && find_suf(q.find(i)) == j) {
				return -ret;
			} else {
				ans.pop();
			}
		}
	}
}sol2;

int main() {
	freopen("playwithboss.in", "r", stdin);
	freopen("playwithboss.out", "w", stdout);
	scanf("%d", &N);
	for (int i = 1; i <= N; i++) {
		scanf("%d %d %d", &mon[i].a, &mon[i].b, &mon[i].c);
		renb[i] = mon[i].b;
	}
	sort(renb+1, renb+1+N);
	for (int i = 1; i <= N; i++) {
		mon[i].b = lower_bound(renb+1, renb+1+N, mon[i].b) - renb;

		if (tmpbcnt[mon[i].b]) mon[i].b = ++tmpbcnt[mon[i].b];
		else tmpbcnt[mon[i].b] = mon[i].b;
	}
	sort(mon+1, mon+1+N);
	
	sol2.insert(0, INF);
	sol2.insert(N+1, 0);
	int ans = mon[N].a;
	for (int i = N; i >= 1; i--) { 
		sol2.insert(mon[i].b, mon[i].c);
		ans = min(ans, mon[i-1].a + sol2.get());
	}
	
	printf("%d\n", ans);
	return 0;
}