• POJ 2528 Mayor's posters(线段树+离散化)


    地址: http://poj.org/problem?id=2528

    Description

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:
    • Every candidate can place exactly one poster on the wall.
    • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
    • The wall is divided into segments and the width of each segment is one byte.
    • Each poster must completely cover a contiguous number of wall segments.

    They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
    Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.

    Input

    The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.

    Output

    For each input data set print the number of visible posters after all the posters are placed.

    The picture below illustrates the case of the sample input.

    Sample Input

    1
    5
    1 4
    2 6
    8 10
    3 4
    7 10
    

    Sample Output

    4
    


    给定一些海报,可能相互重叠,告诉你每个海报的宽度(高度都一样的)和先后叠放顺序,问没有被完全盖住的有多少张?

    海报最多10000张,但是墙有10000000块瓷砖长,海报不会落在瓷砖中间。


    题意很简单,但是如果是直接建立线段树的话肯定是超内存的,开始的时候不知道离散化,借鉴了kuangbin的代码才了解,用到离散化思想,其实离散化就是通过哈希的方式(这里用到的是最简单的哈希表,也就是直接映射的方式),把原来大的应射程小的,再根据映射完成建树过程


    具体思路就是线段树+离散化

    代码:

    #include<stdio.h>
    #include<algorithm>
    #include<math.h>
    using namespace std;
    const int MAXN=10010;
    struct Cpost
    {
        int l,r;
    } posters[MAXN];
    
    
    int x[MAXN*2];
    int hash[10000005];
    struct Node
    {
        int l,r;
        bool bCovered;//标记是否被完全覆盖
    }segTree[MAXN*8];
    
    
    void Build(int i,int l,int r)//建立线段树
    {
        segTree[i].l=l , segTree[i].r=r , segTree[i].bCovered=false;
        if(l==r)  return;
        int mid=(l+r)>>1;
        Build(i<<1,l,mid);
        Build(i<<1|1,mid+1,r);
    }
    
    
    bool query(int i,int l,int r)//贴上一个海报,同时判断是否被完全覆盖  返回是否已经覆盖  true为未覆盖
    {
        if(segTree[i].bCovered)  return false;  ///如果到当前节点已经出现了覆盖的话,肯定就是已经覆盖了
    
        if( segTree[i].l==l&&segTree[i].r==r )
        {
            segTree[i].bCovered=true;
            return true;
        }
    
        bool bResult;
        int mid=(segTree[i].l+segTree[i].r)>>1;
        if(r<=mid)      bResult=query(i<<1,l,r);
        else if(l>mid)  bResult=query(i<<1|1,l,r);
        else         //   bResult = query(i<<1,l,mid) || query(i<<1|1,mid+1,r);//不能直接或上去,因为如果前面的真,后面的会不做的、、
        {
            bool b1=query(i<<1,l,mid);
            bool b2=query(i<<1|1,mid+1,r);
            bResult=b1||b2;
         }
        //这个很重要,要反馈回原结点,如果左右儿子都被完全覆盖了,自然也完全覆盖了
        if(segTree[i<<1].bCovered && segTree[i<<1|1].bCovered) ///i<<1|1  == i*2+1
            segTree[i].bCovered=true;
        return bResult;
    }
    
    
    int main()
    {
        int T;
        int i,j,k;
        int n;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            int nCount=0;
            for(i=0; i<n; i++)
            {
                scanf("%d%d",&posters[i].l,&posters[i].r);
                x[nCount++]=posters[i].l;
                x[nCount++]=posters[i].r;
            }
            sort(x,x+nCount);//先排序
            nCount=unique(x,x+nCount)-x;  ///去除的是相邻的重复元素    返回的是num去重后的尾地址
            for(i=0; i<nCount; i++)
                hash[   x[i]   ] = i; ///建立哈希表
            Build(1,0,nCount-1);
            int res=0;
            for(i=n-1; i>=0; i--) ///要从上面开始判断
                if(  query( 1 , hash[ posters[i].l ]   ,  hash[ posters[i].r ])  )   ///从根节点向下看
                    res++;
            printf("%d
    ",res);
        }
        return 0;
    }
    

    最后贴一下关于线段树+离散化的小拓展:

    平衡树可以做的事情其实线段数也可以做。

        用线段数,查第k大、插入元素、删除元素也都是O(logn),但是由于它开的空间太大,一般情况下数组要开到2^k,刚好2^(k-1)大于最大的那个数max的时候。不过这样的话有很多的空间都浪费了,所以空间太大的话就不得不用平衡树了。
         其实在处理数据之前可以把数据离散化,先排个序,用一个数组记录它们是第几大,这样就把非常大的数压缩得很小,线段数中叶子节点记录的就是压缩后的数据而不是原数,这样的化2^k只跟n有关,而和那个max无关。当然,如果是处理过程和数据的插入是一起进行的话,就先把处理过程记录下来。所以,这种算法的缺点是不能进行在线算法。不过线段数的代码比所有的平衡树都要简洁些(至少我是这么觉得的),所以线段数+离散化还是比平衡树更科学。


  • 相关阅读:
    python定义函数的三种形式
    python函数的返回值
    python函数的调用
    python函数的定义
    python文件操作
    Python2和3字符编码的区别
    python的字符编码
    python异常处理
    python深浅拷贝
    python色彩缤纷的python(改变字体颜色以及样式)
  • 原文地址:https://www.cnblogs.com/zswbky/p/5431926.html
Copyright © 2020-2023  润新知