zoukankan      html  css  js  c++  java
  • HDU 4252 A Famous City

    A Famous City

    Time Limit: 3000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 4252
    64-bit integer IO format: %I64d      Java class name: Main
     
    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:

    Source

     
    解题:这个写法貌似最坏情况下复杂度是n*n的居然也可以过,足以见此题数据之弱。。。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 100010;
    18 int d[maxn],n,m;
    19 int main(){
    20     int cs = 1;
    21     while(~scanf("%d",&n)){
    22         for(int i = 0; i < n; ++i){
    23             scanf("%d",d+i);
    24         }
    25         m = n;
    26         if(d[0] == 0) --m;
    27         for(int i = 1; i < n; ++i){
    28             if(d[i] == 0) m--;
    29             else{
    30                 for(int j = i-1; j >= 0; --j){
    31                     if(d[j] < d[i]) break;
    32                     if(d[j] == d[i]){
    33                         --m;
    34                         break;
    35                     }
    36                 }
    37             }
    38         }
    39         printf("Case %d: %d
    ",cs++,m);
    40     }
    41     return 0;
    42 }
    View Code

    单调栈

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 stack<int>stk;
    18 int main(){
    19     int cs = 1,tmp,n;
    20     while(~scanf("%d",&n)){
    21         while(!stk.empty()) stk.pop();
    22         int ans = 0;
    23         for(int i = 0; i < n; ++i){
    24             scanf("%d",&tmp);
    25             while(!stk.empty() && stk.top() > tmp){
    26                 ans++;
    27                 stk.pop();
    28             }
    29             if(tmp && (stk.empty() || tmp > stk.top())) stk.push(tmp);
    30         }
    31         printf("Case %d: %d
    ",cs++,ans + stk.size());
    32     }
    33     return 0;
    34 }
    View Code
  • 相关阅读:
    WebService的学习
    什么是事物
    数组和链表的区别
    JDK6和JDK7中的substring()方法
    为什么存储密码字符数组比字符串更合适?
    java中Queue简介
    java中队列Queue的使用
    HashMap、Hashtable、TreeMap的区别
    笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树
    笔试算法题(35):最长递增子序列 & 判定一个字符串是否可由另一个字符串旋转得到
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4032771.html
Copyright © 2011-2022 走看看