您的位置:首页 > 其它

poj 2528 Mayor's posters(线段树 离散化 区间更新 贴海报)

2015-08-10 13:02 435 查看
这个题目本来对大神来说可能是水题, 对我就不行了,昨晚非折腾到下半夜一点 搞定, 并且可以总结出 ,只有把问题想清楚,或着看人家解题报告自己把问题和代码思路

搞清楚,才能谈的上调bug,否则根本就不知道错在哪儿。说说这个题目的理解,他是如何转化为线段树问题的呢?我们知道线段树有一个区间更新的东西,每张海报的宽度不就是一个区间么?那么我们可以用一棵树中的部分结点

来表示整张海报的可视部分,也就是说每个结点只允许表示一张完整的或着不完整的海报(好几个结点才可以表示成完整的一张海报),那么如何表示这个节点只被一张海报覆盖呢? 那么添加一个结点属性c,c > 0,表示一种海报覆盖,c == 0,表示多张或着没有任何一种海报覆盖,从根节点往下看,最上层的那一层 c > 0,的结点组合起来恰好就是整个墙面的可视化部分吧 ? 这是线段树所能表达出来的内容。

细节上:

是不是发现给的墙面宽度特别宽?所以我学会了离散化思想,把离散的点先按照大小关系排序,同时有必要记住每个点对(左右端点)的所属第几张海报,然后按照大小关系依次编号,这样确实能够维系每个点对之间的原来的关系

但是存在一个问题,人家的博客中给出了这样的样例


如三张海报为:1~10 1~4 6~10

离散化时 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10

第一张海报时:墙的1~4被染为1;

第二张海报时:墙的1~2被染为2,3~4仍为1;

第三张海报时:墙的3~4被染为3,1~2仍为2。

最终,第一张海报就显示被完全覆盖了,于是输出2,但实际上明显不是这样,正确输出为3。

那么处理方法是在每个排序好的点之间如果发现某两点作差 > 1那么在之间添加新的结点,这样就可以避免上述错误,并且即使在一张海报点对之间多加了一个中间数也并不会影响结果,顶多离散化后这张海报宽度增加 1。

数据结构上用了一个dict二维数组 如dict【i】【j】表示第 i 副图画 第 j 个端点值, 用一个结构体Node表示每个端点值和所属第几个海报的信息。通过Node中的海报位置信息 我们还能把端点值还原到dict数组,此时dict数组就是离散化后的各张海报信息。如dict【node【i】.num】【0】 = node 【i】.val 就是第node【i】.num 张海报,值为此张海报左端点值。

另外值得一提的是不考虑上例提到的那种情况竟然也可以ac,那样就简单多了,但是我们要有一中求真务实的态度。



Mayor's posters

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 50053Accepted: 14536
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

代码:
/*=============================================================================
#
#      Author: liangshu - cbam 
#
#      QQ : 756029571 
#
#      School : 哈尔滨理工大学 
#
#      Last modified: 2015-08-10 12:59
#
#     Filename: B.cpp
#
#     Description: 
#        The people who are crazy enough to think they can change the world, are the ones who do ! 
=============================================================================*/

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF = 10010;
struct Tree
{
    int l, r, c;
} tree[INF * 14];

struct Node
{
    int val,num;
} node[INF<<2];

set<int>cnt;

int cmp(Node a, Node b)
{
    return a.val < b.val;
}

void create(int t, int l, int r)
{
    tree[t].l = l;
    tree[t].r = r;
    tree[t].c = 0;
    if(l == r)
        return ;
    int a, b, mid = (l + r)>>1;
    create(t<<1, l, mid);
    create(t<<1|1, mid + 1, r);
}

void update(int t, int val, int l, int r)
{

    if(tree[t].l >= l && tree[t].r <= r)
    {
        tree[t].c = val;

        return ;
    }

    if(tree[t].c > 0)
    {
        tree[t<<1].c = tree[t].c;
        tree[t<<1|1].c =tree[t].c;
        tree[t].c = 0;

    }
    if(tree[t].l == tree[t].r)
        return ;
    int mid = (tree[t].l + tree[t].r) >>1;
//    if(l <= mid)
//    {
//        update(t<<1, val, l, r);
//    }
//    if(mid < r)
//    {
//        update(t<<1|1, val, l, r);
//    }

    if( l > mid)update(t<<1 | 1, val, l, r);
    else if(r <= mid)
        update(t<<1, val, l, r);
    else
    {
        update(t<<1,val, l, mid);
        update(t<<1|1,val, mid + 1, r);
    }
}

int flag[INF<<2];
int coun = 0;
void cal(int t)
{
    if(tree[t].c > 0)
    {
        if(!flag[tree[t].c])
        {
            coun++;
            flag[tree[t].c] = 1;
        }
        return ;
    }
    if(tree[t].l == tree[t].r)
        return ;
    cal(t<<1);
    cal(t<<1|1);
}

int main()
{
    int dict[INF][3];
    int t;
    cin>>t;
    int n,tx;
    while(t--)
    {
        memset(flag, 0, sizeof(flag));
        memset(dict, 0 ,sizeof(dict));
        coun = 0;
        tx = 1;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &dict[i][0], &dict[i][1]);
            node[2 * i - 1]. val = dict[i][0];
            node[2 * i - 1].num = i;
            node[2 * i].val = dict[i][1];
            node[2 * i].num = -1 * i;
        }
        sort(node + 1, node + 2 * n  + 1 , cmp);
        if(n >= 2)
        {
            for(int i = 2; i <= 2 * n ; i += 1)
            {
                if(node[i].val - node[i-1].val > 1)
                {
                    node[2 * n  + tx].val = node[i].val - 1;
                    node[2 * n + 1 + tx].num = INF<<3;
                    tx++;
                }
            }
        }
        sort(node + 1, node + 2 * n  + tx  , cmp);

        int x = 1;
        dict[abs(node[1].num)][node[1].num > 0 ? 0 : 1] = x;
        for(int i = 2; i <= 2 *n +tx -1 ; i++)
        {
            if(node[i].val != node[i-1].val)
            {
                if(node[i].num == INF<<3)
                {
                    x++;
                    continue;
                }
                x++;
            }
            if(node[i].num > 0)
                dict[node[i].num][0] = x;
            else
                dict[-1 * node[i].num][1] = x;
        }
        create(1, 1, x);
        for(int i = 1; i<= n; i++)
        {
            update(1, i, dict[i][0], dict[i][1]);
        }
        cal(1);
        printf("%d\n",coun );

    }
    return 0;

}


4
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: