2013
11-11

# 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]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */"
import java.util.*;
import java.io.*;

public class Main
{
public static int N = 10000;

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();
for(int i = 0; i < n; i++)
{
int s = sc.nextInt();
int e = sc.nextInt();
segments[i] = new Segment(s,e);
}
for(Integer endpoint : endpoints)
{
}
int result = 0;
for(int i = n-1; i >= 0; i--)
{
Segment seg = segments[i];
int left = seg.start;
int right = seg.end;
{
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)
{
}else if(left >= rightStart)
{
}else
{
result = (r1 || r2);
}
current.isCovered = (current.leftChild.isCovered && current.rightChild.isCovered);
}
}
return result;
}

public static Node buildSegmentTree(LinkedList< Node> current)
{
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;
}
if(current.size() > 0)
{
}
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 . 谁能解释下？

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;