BZOJ 1628: [Usaco2007 Demo]City skyline

1628: [Usaco2007 Demo]City skyline

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 555  Solved: 432
[Submit][Status][Discuss]

Description

Input

第一行給出N,W
第二行到第N+1行:每行給出二個整數x,y,輸入的x嚴格遞增,並且第一個x總是1

Output

輸出一個整數,表示城市中最少包含的建築物數量

Sample Input

10 26
1 1
2 2
5 1
6 3
8 1
11 0
15 2
17 3
20 2
22 1

INPUT DETAILS:

The case mentioned above

Sample Output

6

HINT

Source

Silver


題解:單調棧。。。首先我們可以肯定的是答案的上限 肯定是 n, 何時會減一呢? 當有兩座樓高度相等且它們的中間沒有比它們低的樓。所以要維護的是一個單調遞增的序列, 每次彈出比它大的直到遇到一個和它相等的, 沒有相等的話就把 它加入這個序列中。


代碼實現也很簡單啊。。。

#include<cstdio>
using namespace std;
const int maxn=50001;
int n,m,top,ans;
int a[maxn],s[maxn];
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int main()
{
	n=read();m=read();
	ans=n;
	for(int i=1;i<=n;i++)
		a[i]=read(),a[i]=read();
	for(int i=1;i<=n;i++)
	{
		while(s[top]>a[i])top--;
		if(s[top]==a[i])ans--;
		else s[++top]=a[i];
	}
	printf("%d\n",ans);
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章