码迷,mamicode.com
首页 > 其他好文 > 详细

HDU5200 Trees (离线处理)

时间:2015-04-05 14:38:31      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5200


题意:

每次去掉高度小于q的树,每次输出剩下的块数。


分析:

我们对高度从高到低进行排序,对要去掉的高度从低到高进行排序。

因此前面去掉的在后面一定会去掉,因可以离线处理,节省时间,

然后需要开一个辅助空间,记录这棵树去没有去掉。


代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

const int maxn = 50010;

int vis[maxn],ans[maxn];

struct tree{
    int h,id;
    bool operator <(const struct tree &tmp) const{
        return h > tmp.h;
    }
}p[maxn];

struct query{
    int h,id;
    bool operator <(const struct query &tmp) const{
        return h<tmp.h;
    }
}q[maxn];

inline int S(){
    int ret=0,ok=0;
    char c;
    while((c=getchar()))
    {
        if(c>='0'&&c<='9')
        ret=ret*10+c-'0',ok=1;
        else if(ok)
        return ret;
    }
    return ret;
}

int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        for(int i=0;i<n;i++){
            p[i].h=S();
            p[i].id=i+1;
        }
        sort(p,p+n);
        for(int i=0;i<m;i++){
            q[i].h=S();
            q[i].id=i+1;
        }
        sort(q,q+m);
        int tot = 0;
        memset(vis,0,sizeof(vis));
        for(int i = 0,j = m-1;j>=0;j--){
            for(;i<n;i++){
                if(p[i].h<=q[j].h)
                    break;
                vis[p[i].id]=1;
                if(!vis[p[i].id-1]&&!vis[p[i].id+1]) tot++;
                else if(vis[p[i].id-1]&&vis[p[i].id+1]) tot--;
            }
            ans[q[j].id]=tot;
        }
        for(int i=0;i<m;i++)
            printf("%d\n",ans[i+1]);
    }
    return 0;
}



HDU5200 Trees (离线处理)

标签:

原文地址:http://blog.csdn.net/bigbigship/article/details/44887429

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!