[BZOJ1628][Usaco2007 Demo]City skyline

1628: [Usaco2007 Demo]City skyline

Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 569  Solved: 442 [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
 
维护一个单调增的栈,如果插入时发现栈顶和插入元素相等则可以少一个建筑
 
#include <cstdio>
#include <cstring>
char buf[10000000], *ptr = buf - 1;
inline int readint(){
    int n = 0;
    char ch = *++ptr;
    while(ch < '0' || ch > '9') ch = *++ptr;
    while(ch <= '9' && ch >= '0'){
        n = (n << 1) + (n << 3) + ch - '0';
        ch = *++ptr; 
    }
    return n;
}
const int maxn = 50000 + 10;
int sta[maxn], top = 0, ans;
int main(){
    fread(buf, sizeof(char), sizeof(buf), stdin);
    int N = ans = readint();
    readint();
    for(int x, y, i = 1; i <= N; i++){
        x = readint();
        y = readint();
        while(top && sta[top] > y) top--;
        if(sta[top] == y) ans--;
        else sta[++top] = y;
    }
    printf("%d\n", ans);
    return 0;
}

 

posted @ 2017-09-19 17:51  Elder_Giang  阅读(139)  评论(0编辑  收藏  举报