1628: [Usaco2007 Demo]City skyline
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 558 Solved: 435
[ 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
Sample Output
6
x因为输入保证递增,所以毫无意义
这样维护一个优先队列(用栈更好),每输入一个y,就将>=y的全部弹出,每弹出一个ans++,然后y进队列
最后记得要多算一个y=0!
#include<stdio.h>
#include<queue>
using namespace std;
priority_queue<int> q;
int main(void)
{
int n, w, i, x, ans = 0;
scanf("%d%d", &n, &w);
for(i=1;i<=n;i++)
{
scanf("%*d%d", &x);
while(q.empty()==0 && q.top()>x)
{
ans++;
q.pop();
}
if(q.empty() || q.top()<x)
q.push(x);
}
while(q.empty()==0)
{
if(q.top()!=0)
ans++;
q.pop();
}
printf("%d\n", ans);
return 0;
}