zoukankan      html  css  js  c++  java
  • poj- 2528 Mayor's posters

    Mayor’s posters

    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
    题意:n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000) 。求出最后还能看见多少张海报。
    思路:线段树+离散化 具体请参见 kuang_bin.bolg
    这个题怎么说?有点玄学我sum开int就过不去 , 线段树还必须开到1e5的8倍才能过,可是按理论应该是能过得,可能数据有问题
    AC code:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <map>
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    
    using namespace std;
    
    const int maxn = 1e5+5;
    typedef long long ll;
    int vis[maxn];
    
    struct segtree{
        int l,r;
        bool sum;
    }ss[maxn<<3];
    
    struct Node{
        int l,r;
    }node[maxn];
    
    void pushUp(int rt){
        ss[rt].sum = ss[rt<<1].sum && ss[rt<<1|1].sum;
    }
    
    void build(int l,int r,int rt){
        ss[rt].l = l; ss[rt].r = r;
        ss[rt].sum = false;
        if(l == r) return;
        int mid = (l+r)>>1;
        build(lson); build(rson);
    }
    
    bool update(int l,int r,int rt){
        if(ss[rt].sum) return false;
        if ( ss[rt].l == l && ss[rt].r == r ) {
            ss[rt].sum = true; return true;
        }
        int mid = (ss[rt].l+ss[rt].r)>>1; bool res;
        if ( r<=mid ) {
            res = update(l,r,rt<<1);
        } else if ( mid<l ) {
            res = update(l,r,rt<<1|1);
        } else {
            bool res1 = update(lson);
            bool res2 = update(rson);
            res = res1||res2;
        }
        pushUp(rt);
        return res;
    }
    
    int main(){
        int t; cin>>t;
        while(t--)
        {
            int n; scanf("%d",&n); int pos = 0;
            for (int i = 0;i<n;i++) {
                scanf("%d %d",&node[i].l,&node[i].r);
                vis[++pos] = node[i].l ,vis[++pos] = node[i].r;
            }
            sort(vis+1,vis+pos+1);
            int len = unique(vis+1,vis+pos+1)-vis; len--;
            long long sum = 0; build(1,len,1);
            for (int i = n-1;i>=0;i--) {
                int l = lower_bound(vis+1,vis+len+1,node[i].l)-vis;
                int r = lower_bound(vis+1,vis+len+1,node[i].r)-vis;
                bool flag = update(l,r,1);
                if(flag) sum ++;
            }
            printf("%lld
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    网站图片上传,水印,预览,截图
    go语言中的数组切片:特立独行的可变数组
    Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案
    MongoDB删除文档
    顺为资本CEO许达来:为什么说中国创业者很幸福?(附PPT)
    星瀚资本杨歌:我七次创业失败的内心感悟(比较真实,可以看看创业的36条军规)
    晨兴资本刘芹:入行16年我才刚理解创投,有8个最深感悟
    20 个免费开源的 CSS3 用户界面工具包
    Google浏览器的缓存文件过大(mega网站导致的)
    FastSocket客户端/服务端通讯示例
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745967.html
Copyright © 2011-2022 走看看