[bzoj1113][Poi2008]海报PLA

题目大意:有$n(nleqslant2.5 imes10^5)$个矩形并排放置,给出每个矩形的高和宽,问最少用几个矩形可以覆盖这$n$个矩形

题解:贪心,一定是横着放矩形,可以用单调栈(单调递增)来维护,若这个矩形比前面的一个矮,且在单调栈中出现就不需要加矩形,否则都要加一个矩形

卡点:

C++ Code:

#include <cstdio>
#include <iostream>
#define maxn 250010

int n, ans;
int S[maxn], top;
int main() {
	std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
	std::cin >> n;
	for (int i = 0, a, b; i < n; ++i) {
		std::cin >> a >> b;
		while (top && S[top] > b) --top;
		if (!top || S[top] != b) S[++top] = b, ++ans;
	}
	std::cout << ans << '
';
	return 0;
}

  

原文地址:https://www.cnblogs.com/Memory-of-winter/p/10358035.html