首页 > 其他 > 详细

POJ2528___

时间:2014-08-07 19:16:20      阅读:446      评论:0      收藏:0      [点我收藏+]

本文出自Svitter的blog

——踏踏实实的做事儿啊!

POJ2528

题意


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.

给你n张海报,海报都是正好一块,不会在中间。每张海报给你两个l, r两个边界,求没有被盖住的海报的个数

输入输出分析


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

算法数据结构分析


线段树+哈希表(离散化) 边利用离散化进行处理,来减少多余的区间。通过存左右边到数组,然后排序,把中间多余的部分使用哈希函数处理掉,去掉重复的值。
利用nc = unique(x,x+nc)-x去掉重复的值得到个数。 注意贴海报的,先贴最外面的,然后把里面的往里插入,如果能露出来说明没有被盖住。如果线段树左右两边都被盖住, 整个区间都被盖住。

AC代码:


//author: svtter
//

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>

#define INF 0xffffff
#define lln long long

#ifdef ONLINE_JUDGE
#define FOI(file) 0
#define FOW(file) 0
#else
#define FOI(file) freopen(file,"r",stdin);
#define FOW(file) freopen(file,"w",stdout);
#endif

using namespace std;

struct CNode
{
    int l, r;
    bool cover;
    CNode *pLeft, *pRight;
    int mid()
    {
        return (l+r)/2;
    }
};

struct poster
{
    int l, r;
};

#define N 10000000
CNode tree[N];
poster post[10100];
int x[20200];
int hash[N+10];

int ncount = 0;
void BuildTree(CNode *root, int l, int r)
{
    root->l = l;
    root->r = r;
    root->cover = false;
    if(l == r)
        return;
    ncount++;
    root->pLeft = tree + ncount;
    ncount++;
    root->pRight= tree + ncount;
    BuildTree(root->pLeft, l, (l+r)/2);
    BuildTree(root->pRight, (l+r)/2+1, r);
}

bool Post(CNode *root, int l, int r )
{
    if(root->cover) return 0;
    if(root->l == l && r == root->r)
        return root->cover = 1;

    bool res;
    if(r <= root-> mid())
        res = Post(root->pLeft, l, r);
    else if(l > root->mid())
        res = Post(root->pRight, l, r);
    else
    {
        bool a = Post(root->pLeft, l, root->mid());
        bool b = Post(root->pRight, root->mid()+1, r);
        res = a||b;
    }

    //两边都被覆盖了, 那么本身就被覆盖了。
    if(root->pLeft->cover && root->pRight->cover)
        root->cover = true;
    return res;
}

int main()
{
    //FOI("input");
    //FOW("output");
    //write your programme here

    int c, n;
    int i;
    int sum;
    int nc = 0;
    int hashkey=1;
    scanf("%d", &c);
    while(c--)
    {
        nc = 0;
        scanf("%d", &n);
        
        for(i = 1; i <= n ;i ++)
        {
            scanf("%d%d", &post[i].l, &post[i].r);
            x[nc++] = post[i].l;
            x[nc++] = post[i].r;
        }

        sort(x, x+nc);
        nc = unique(x, x+nc) - x;
        hashkey = 1;
        for(i = 0; i < nc; i++)
            hash[x[i]] = hashkey++;
        
        CNode *root = tree;
        BuildTree(root, 1, hashkey);
        sum = 0;
        for(i = n; i >=1 ; i--)
        {
            if(Post(root, hash[post[i].l], hash[post[i].r]))
                sum++;
        }
        
        //printf("MAX:%d\n", MAX);
        printf("%d\n", sum);
    }
    return 0;
}

POJ2528___,布布扣,bubuko.com

POJ2528___

原文:http://blog.csdn.net/svitter/article/details/38420117

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!