CRZbulabula的博客

学生党的博客

1113: [Poi2008]海报PLA

1113: [Poi2008]海报PLA

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 986  Solved: 648
[Submit][Status][Discuss]

Description

N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们.

Input

第一行给出数字N,代表有N个矩形.N在[1,250000] 下面N行,每行给出矩形的长与宽.其值在[1,1000000000]2 1/2 Postering

Output

最少数量的海报数.

Sample Input

5
1 2
1 3
2 2
2 5
1 4

Sample Output

4

HINT

Source

[Submit][Status][Discuss]

维护一个单调栈
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int maxn = 3E5;

int n,Ans,h[maxn];

stack <int> s;

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int x,y; scanf("%d%d",&x,&y);
		h[i] = y;
	}
	++n;
	for (int i = 1; i <= n; i++) {
		while (!s.empty()) {
			int now = s.top(); 
			if (now <= h[i]) break;
			s.pop(); ++Ans;
		}
		if (s.empty()) s.push(h[i]);
		else {
			int now = s.top();
			if (now == h[i]) continue;
			s.push(h[i]);
		}
	}
	cout << Ans;
	return 0;
}

阅读更多
版权声明:这个人很懒什么都没有留下 https://blog.csdn.net/CRZbulabula/article/details/52368861
个人分类: 单调队列
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭