# HDOJ4252A Famous City --- 单调栈

Problem Description

After Mr. B arrived in Warsaw, he was shocked by the skyscrapers and took several photos. But now when he looks at these photos, he finds in surprise that he isn't able to point out even the number of buildings in it. So he decides to work it out as follows:
- divide the photo into n vertical pieces from left to right. The buildings in the photo can be treated as rectangles, the lower edge of which is the horizon. One building may span several consecutive pieces, but each piece can only contain one visible building, or no buildings at all.
- measure the height of each building in that piece.
- write a program to calculate the minimum number of buildings.
Mr. B has finished the first two steps, the last comes to you.

Input

Each test case starts with a line containing an integer n (1 <= n <= 100,000). Following this is a line containing n integers - the height of building in each piece respectively. Note that zero height means there are no buildings in this piece at all. All the input numbers will be nonnegative and less than 1,000,000,000.

Output

For each test case, display a single line containing the case number and the minimum possible number of buildings in the photo.

Sample Input




3 1 2 3 3 1 2 1

Sample Output




Case 1: 3 Case 2: 2

Hint

The possible configurations of the samples are illustrated below: 题解：这题如果不给任何提示的话我多半想不到好的方法，但是因为是专门搜单调栈练习题，所以才知道这题怎么做。。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;
stack<int> s;
int n;
int main()
{
int Case = 1;
while(scanf("%d",&n) != EOF) {
while(!s.empty()) s.pop(); // 清空栈
int cnt = 0;
int t;
for(int i = 0;i < n;i++) {
scanf("%d",&t);
if(s.empty()) {
if(t != 0) {
s.push(t);
cnt++;
}
} else {
while( !s.empty() && (t < s.top()) ) {
s.pop();
}
if(s.empty()) {
if(t != 0) {
s.push(t);
cnt++;
}
} else if(t > s.top()) {
cnt++;
s.push(t);
}
}
}

printf("Case %d: %d\n",Case++,cnt);

}

return 0;
}
{"ft_lang_label":"__label__en","ft_lang_prob":0.52971655,"math_prob":0.9725217,"size":2505,"snap":"2019-13-2019-22","text_gpt3_token_len":1151,"char_repetition_ratio":0.092762895,"word_repetition_ratio":0.03943662,"special_character_ratio":0.2766467,"punctuation_ratio":0.15767635,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95431584,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,1,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-03-22T08:13:33Z\",\"WARC-Record-ID\":\"<urn:uuid:59ebb61e-b723-4882-9c0e-a2ba9a966da9>\",\"Content-Length\":\"183861\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dfdab983-0b2e-4b72-8cef-8f4faa96b269>\",\"WARC-Concurrent-To\":\"<urn:uuid:90e4de00-9f81-4b45-9a12-d1fd171b87e8>\",\"WARC-IP-Address\":\"47.95.47.253\",\"WARC-Target-URI\":\"https://blog.csdn.net/lmhlmh_/article/details/84064937\",\"WARC-Payload-Digest\":\"sha1:LQ3BJVQP7J6EWX7JC4HKVQ6MVZTGQMM4\",\"WARC-Block-Digest\":\"sha1:K5SB7M7M354OPJIDGT756YVHV5KEQTS4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-13/CC-MAIN-2019-13_segments_1552912202640.37_warc_CC-MAIN-20190322074800-20190322100800-00523.warc.gz\"}"}