1628: [usaco2007 demo] city skylinetime limit: 5 sec memory limit: 64 MB
Submit: 256 solved: 210
[Submit] [Status] Descriptionthe best part of the day for Farmer John's cows is when the sunsets. they can see the skyline of the distant city. bessie wondershow extends buildings the city has. write a program that assists thecows in calculating the minimum number of buildings in the city, given a profile of its skyline. the city in profile is quite dull turally, featuring onlybox-shaped buildings. the skyline of a city on the horizon issomewhere between 1 and w units wide (1 <= W <= 1,000,000) anddescribed using N (1 <= n <= 50,000) successive X and Y coordinates (1 <= x <= W, 0 <= Y <= 500,000), defining at what point the skylinechanges to a certain height. an example skyline cocould be :............................... XX ......... xxx ........ xxx. XX ....... xxxxxxx ..... xxxxxxxxxx .... xxxxxxxxxxxxand wocould be encoded ), (20, 2), (22, 1 ). for a graph constructed from some rectangles, we need to find the minimum number of rectangle blocks to overwrite it. However, this input is strange. For details, 1.1 represents the first column, A rectangle with a height of 1 is composed of "X. how wide is this rectangle? I didn't tell you here that 2.2 represents a rectangle with a height of 2 in the second column. This indirectly tells you how wide is the rectangle in front of it, the width (2-15.1) indicates that there is a rectangle with a height of 1 in the fifth column, which indirectly tells you how wide the rectangle is, the width is 5-2this skyline requires a minimum of 6 buildings to form; below isone possible set of six buildings whose cocould create the skylineabve :........................... .............................. 22 ......... 333 ............ XX ......... xxx ........ 111. 22 ....... xx333xx ...... xxx. XX ....... 5555555 ..... x111x22xxx .... xx333xxxxxxx 4444444444 .... 5555555xxxxx ............................... XX ......... xxx ........ xxx. XX ....... xxxxxxx ..... xxxxxxxxxx .... 666666666666input * Line 1: two space separated integers: N and W * lines 2 .. n + 1: two space separated integers, the X and Y coordinate of a point where the skyline changes. the X coordinates are presented in strictly increasing order, and the first X coordinate will always be 1. output * Line 1: the minimum number of buildings to create the described skyline. sample input10 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 output6
Hintsource
Silver
Question:
Cf original question? I totally forgot...
Relatively clever. First, we should know the ANS <= n, and then consider the ANS <n under what circumstances?
Obviously, there should be two blocks of the same height, and there is no lower block than them!
That is monotonous stack.
It is found that OI sometimes does not look at whether or not you have any algorithms, but whether you can think of an algorithm.
Code:
1 #include<cstdio> 2 3 #include<cstdlib> 4 5 #include<cmath> 6 7 #include<cstring> 8 9 #include<algorithm>10 11 #include<iostream>12 13 #include<vector>14 15 #include<map>16 17 #include<set>18 19 #include<queue>20 21 #include<string>22 23 #define inf 100000000024 25 #define maxn 50000+10026 27 #define maxm 500+10028 29 #define eps 1e-1030 31 #define ll long long32 33 #define pa pair<int,int>34 35 #define for0(i,n) for(int i=0;i<=(n);i++)36 37 #define for1(i,n) for(int i=1;i<=(n);i++)38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)42 43 using namespace std;44 45 inline int read()46 47 {48 49 int x=0,f=1;char ch=getchar();50 51 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}52 53 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}54 55 return x*f;56 57 }58 int a[maxn],sta[maxn],n,m;59 60 int main()61 62 {63 64 freopen("input.txt","r",stdin);65 66 freopen("output.txt","w",stdout);67 68 n=read();m=read();69 for1(i,n)a[i]=read(),a[i]=read();70 int top=0,ans=n;71 for1(i,n)72 {73 while(a[sta[top]]>a[i])top--;74 if(a[sta[top]]==a[i])ans--;else sta[++top]=i;75 }76 printf("%d\n",ans); 77 78 return 0;79 80 }
View code
Bzoj1628: [usaco2007 demo] city skyline