MDOLLS - Nested Dolls

Tác giả: happyboy99x

Ngôn ngữ: C++

#include<cstdio>
#include<algorithm>
using namespace std;

const int N = 20000 + 5;
pair<int, int> a[N];
int n, lst[N];


void enter() {
	scanf("%d", &n);
	for(int i = 0; i < n; ++i)
		scanf("%d%d", &a[i].first, &a[i].second);
}
const bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
	return a.first == b.first ? a.second > b.second : a.first < b.first;
}

void solve() {
	sort(a, a+n, cmp); int res = 0;
	for(int i = n-1; i >= 0; --i) {
		int dp = upper_bound(lst, lst+res, a[i].second) - lst;
		if(dp == res) ++res, lst[dp] = a[i].second;
		else lst[dp] = min(lst[dp], a[i].second);
	}
	printf("%d\n", res);
}

int main() {
	int tc; scanf("%d", &tc);
	while(tc--) enter(), solve();
	return 0;
}

Download