1628: [Usaco2007 demo]city Skyline Time Limit:5 Sec Memory limit:64 MB
submit:558 solved:435
[Submit] [Status] [Discuss] Description The first line of input gives N,w the second line to the n+1 line: Each row gives two integers x, y, and the input is strictly incremented, and the first x is always 1 Output
Outputs an integer representing the minimum number of buildings in the city Sample Input 1 1 2 2 5 1 6 3 8 1 One 0 2 3 2 Sample Output 1
x because the input is guaranteed to increment, so meaningless
This maintains a priority queue (better with the stack), each input a Y, will >=y all pop up, each popup a ans++, and then y into the queue
Finally, remember to count a 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;
}