UVA 11020 Efficient Solutions set

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Houheshuai/article/details/51456470

点击打开题目链接



#include <cstdio>
#include <cmath> 
#include <cstdlib>
#include <set>
#include <iostream>
using namespace std;
struct Point
{
	int a, b;
	bool operator < (const Point& rhs) const {
		return a < rhs.a || (a == rhs.a && b < rhs.b);
	}
};

multiset<Point> S;
multiset<Point>::iterator it;

int main()
{

	int T;
	scanf("%d", &T);
	for (int kase = 1; kase <= T; kase++) {
		if (kase > 1) printf("\n");
		printf("Case #%d:\n", kase);
		
		int n, a, b;
		scanf("%d", &n);
		S.clear();
		while (n--) {
			scanf("%d%d", &a, &b);
			Point P = (Point){a, b};
			it = S.lower_bound(P);
	
			if (it == S.begin() || (--it)->b > b) {	//判断这个新增点是否有优势 
				S.insert(P);				//有优势,加入集合 
				it = S.upper_bound(P);		//刚加入的点让其他点失去优势,将它们删除 
				while (it != S.end() && it->b >= b) S.erase(it++);
			}
			printf("%d\n", S.size());
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/Houheshuai/article/details/51456470