题目描述
All the buildings in the east district of Byteburg were built in accordance with the old arbitecture:
they stand next to each other with no spacing inbetween.
Together they form a very long chain of buildings of diverse height, extending from east to west.
The mayor of Byteburg, Byteasar, has decided to have the north face of the chain covered with posters.
Byteasar ponders over the minimum number of posters sufficient to cover the whole north face.
The posters have rectangular shape with vertical and horizontal sides.
They cannot overlap, but may touch each other, i.e. have common points on the sides.
Every poster has to entirely adjoin the walls of certain buildings and the whole surface of the north face has to be covered.
Task Write a programme that:
reads the description of buildings from the standard input, determines the minimum number of posters needed to entirely cover their north faces, writes out the outcome to the standard output.
N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们.
Solution
这道题好眼熟a
通俗地讲,具有相同高度的矩形是可以用一张海报cover住的,那么我们先假定每个矩形cover一张海报,通过维护一个单调递增的单调栈寻找最左边与当前举行同高的矩形,然后这两个矩形就能用一张海报cover了
还有宽度?不影响的,忽略就好了
Code
#include <stdio.h>
#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)
#define N 1000001
int h[N], stack[N];
int main(void){
int n;
scanf("%d", &n);
rep(i, 1, n){
scanf("%*d%d", &h[i]);
}
int top = 0;
int ans = n;
rep(i, 1, n){
while (h[i] < stack[top]){
top -= 1;
}
if (h[i] == stack[top]){
top -= 1;
ans -= 1;
}
stack[++ top] = h[i];
}
printf("%d\n", ans);
return 0;
}