首页 > 专题系列 > Java解POJ > POJ 2528 Mayor’s posters [解题报告] Java
2013
11-11

POJ 2528 Mayor’s posters [解题报告] Java

Mayor’s posters

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

4

解题代码:

//* @author: Yeming Hu"[email protected]"
import java.util.*;
import java.io.*;

public class Main 
{
    public static int N = 10000;
    public static Node head;
    
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        Set< Integer> endpoints = new TreeSet< Integer>();
        Segment[] segments = new Segment[N];
        int t = sc.nextInt();
        while(t-- > 0)
        {
            int n = sc.nextInt();
            LinkedList< Node> nodes = new LinkedList< Node>();
            for(int i = 0; i < n; i++)
            {
                int s = sc.nextInt();
                int e = sc.nextInt();
                segments[i] = new Segment(s,e);
                endpoints.add(s);
                endpoints.add(e);
            }
            for(Integer endpoint : endpoints)
            {
                nodes.addLast(new Node(endpoint,endpoint,endpoint,endpoint));
            }
            head = buildSegmentTree(nodes);
            int result = 0;
            for(int i = n-1; i >= 0; i--)
            {
                Segment seg = segments[i];
                int left = seg.start;
                int right = seg.end;
                if(updateSegmentTree(left,right,head))
                {
                    result++;
                }
            }
            System.out.println(result);
        }
    }
    
    public static boolean updateSegmentTree(int left, int right, Node current)
    {
        boolean result;
        if(current.isCovered)
        {
            result = false;
        }else
        {
            if(left == current.left && right == current.right)
            {
                current.isCovered = true;
                result = true;
            }else
            {
                int leftEnd = current.leftEnd;
                int rightStart = current.rightStart;
                if(right <= leftEnd)
                {
                    result = updateSegmentTree(left,right,current.leftChild);
                }else if(left >= rightStart)
                {
                    result = updateSegmentTree(left,right,current.rightChild);
                }else
                {
                    boolean r1 = updateSegmentTree(left,leftEnd,current.leftChild);
                    boolean r2 = updateSegmentTree(rightStart,right,current.rightChild);
                    result = (r1 || r2);
                }
                current.isCovered = (current.leftChild.isCovered && current.rightChild.isCovered);
            }
        }
        return result;
    }
    
    public static Node buildSegmentTree(LinkedList< Node> current)
    {
        LinkedList< Node> next = new LinkedList< Node>();
        while(current.size() >= 2)
        {
            Node leftChild = current.removeFirst();
            Node rightChild = current.removeFirst();
            Node parent = new Node(leftChild.left,rightChild.right,leftChild.right,rightChild.left);
            parent.leftChild = leftChild;
            parent.rightChild = rightChild;
            next.add(parent);
        }
        if(current.size() > 0)
        {
            next.add(current.removeFirst());
        }
        if(next.size() >= 2)
        {
            return buildSegmentTree(next);
        }else // it's the root left
        {
            return next.removeFirst();
        }
    }
}

class Node
{
    int left;
    int right;
    int leftEnd;
    int rightStart;
    int mid;
    Node parent;
    Node leftChild;
    Node rightChild;
    boolean isCovered;
    
    Node(int left, int right, int leftEnd, int rightStart)
    {
        this.left = left;
        this.right = right;
        this.leftEnd = leftEnd;
        this.rightStart = rightStart;
        this.parent = null;
        this.leftChild = null;
        this.rightChild = null;
        this.isCovered = false;
    }
    
}

class Segment
{
    int start;
    int end;
    
    Segment(int s, int e)
    {
        this.start = s;
        this.end = e;
    }
}

  1. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
    O(n) = O(n-1) + O(n-2) + ….
    O(n-1) = O(n-2) + O(n-3)+ …
    O(n) – O(n-1) = O(n-1)
    O(n) = 2O(n-1)

  2. #include <stdio.h>
    int main(void)
    {
    int arr[] = {10,20,30,40,50,60};
    int *p=arr;
    printf("%d,%d,",*p++,*++p);
    printf("%d,%d,%d",*p,*p++,*++p);
    return 0;
    }

    为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下?

  3. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.

  4. for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
    dp = dp [j-1] + 1;
    if(s1.charAt(i-1) == s3.charAt(i+j-1))
    dp = dp[i-1] + 1;
    if(s2.charAt(j-1) == s3.charAt(i+j-1))
    dp = Math.max(dp [j - 1] + 1, dp );
    }
    }
    这里的代码似乎有点问题? dp(i)(j) = dp(i)(j-1) + 1;这个例子System.out.println(ils.isInterleave("aa","dbbca", "aadbbcb"));返回的应该是false

  5. int half(int *array,int len,int key)
    {
    int l=0,r=len;
    while(l<r)
    {
    int m=(l+r)>>1;
    if(key>array )l=m+1;
    else if(key<array )r=m;
    else return m;
    }
    return -1;
    }
    这种就能避免一些Bug
    l,m,r
    左边是l,m;右边就是m+1,r;