hdu 4252 A Famous City (map+rmq)

A Famous City

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1498    Accepted Submission(s): 560


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:
 

Source


题意:
给你一张照片,判断照片中至少有多少大楼(一栋大楼的高度是不变的)。

感想:
这题数据太水了,网上的一些O(n^2)的解法都能过,我就不想吐槽了。

思路: O(n*log(n))
预处理dp[i],a[i]的上一次出现的位置。然后循环,到一个点,如果这个点第一次出现,ans++,如果它和上一次出现的位置的这个区间内都不小于它(即区间最小值>=a[i]),说明它可以和上一个点是同一栋大楼,不必相加,反之则ans++。区间最小值用rmq来求,然后注意a[i]=0的情况就够了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 100005
#define mod 100000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int n,m,ans;
int a[maxn],f[maxn<<2][20];
int dp[maxn];
map<int,int>mp;

void init_rmq()              // 预处理  O(n*log(n))
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        f[i][0]=a[i];
    }
    for(j=1;(1<<j)<=n;j++)
    {
        for(i=1;i+j-1<=n;i++)
        {
            f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
        }
    }
}
int query_rmq(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=r-l+1) k++;
    return min(f[l][k],f[r-(1<<k)+1][k]);
}
void presolve()
{
    int i,j;
    mp.clear();
    for(i=1;i<=n;i++)
    {
        if(!mp[a[i]]) mp[a[i]]=i,dp[i]=0;
        else
        {
            dp[i]=mp[a[i]];
            mp[a[i]]=i;
        }
    }
}
void solve()
{
    int i,j,u,v,val;
    ans=0;
    for(i=1;i<=n;i++)
    {
        if(a[i]==0) continue ;
        if(dp[i]==0) ans++;
        else
        {
            u=dp[i],v=i;
            val=query_rmq(u,v);
            if(val<a[i]) ans++;
        }
    }
}
int main()
{
    int i,j,test=0;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        init_rmq();
        presolve();
        solve();
        printf("Case %d: ",++test);
        printf("%d\n",ans);
    }
    return 0;
}




 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值