1628: [Usaco2007 Demo]City skyline
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 555 Solved: 432
[Submit][Status][Discuss]
Description
Input
Output
輸出一個整數,表示城市中最少包含的建築物數量
Sample Input
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
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;
}