2013
11-11

# Toy Storage

Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for Reza to find his favorite toys anymore.

Reza’s parents came up with the following idea. They put cardboard partitions into the box. Even if Reza keeps throwing his toys into the box, at least toys that get thrown into different partitions stay separate. The box looks like this from the top:

We want for each positive integer t, such that there exists a partition with t toys, determine how many partitions have t, toys.

The input consists of a number of cases. The first line consists of six integers n, m, x1, y1, x2, y2. The number of cardboards to form the partitions is n (0 < n <= 1000) and the number of toys is given in m (0 < m <= 1000). The coordinates of the upper-left corner and the lower-right corner of the box are (x1, y1) and (x2, y2), respectively. The following n lines each consists of two integers Ui Li, indicating that the ends of the ith cardboard is at the coordinates (Ui, y1) and (Li, y2). You may assume that the cardboards do not intersect with each other. The next m lines each consists of two integers Xi Yi specifying where the ith toy has landed in the box. You may assume that no toy will land on a cardboard.

A line consisting of a single 0 terminates the input.

For each box, first provide a header stating “Box” on a line of its own. After that, there will be one line of output per count (t > 0) of toys in a partition. The value t will be followed by a colon and a space, followed the number of partitions containing t toys. Output will be sorted in ascending order of t for each box.

4 10 0 10 100 0
20 20
80 80
60 60
40 40
5 10
15 10
95 10
25 10
65 10
75 10
35 10
45 10
55 10
85 10
5 6 0 10 60 0
4 3
15 30
3 1
6 8
10 10
2 1
2 8
1 5
5 5
40 10
7 9
0


Box
2: 5
Box
1: 4
2: 1


/* @author:张龙acmilan_fan@yahoo.cn */
import java.io.*;
import java.util.Map;
import java.util.TreeMap;

public class Main {

public static void main(String[] args) throws Exception{
int n, m, y1, y2, i, t;
String s;
String[] ss;
Point p,p2;
ss = s.split(" ",6);
n=parseInt(ss[0]);
m=parseInt(ss[1]);
y1=parseInt(ss[3]);
y2=parseInt(ss[5]);
LineSet ls = new LineSet(n);
Statistics st = new Statistics();
for(i=0;i< n;i++){
ss=s.split(" ",2);
t=parseInt(ss[0]);
p=new Point(t,y1);
p2=new Point(parseInt(ss[1]),y2);
}
for(i=0;i< m;i++){
ss=s.split(" ",2);
t=ls.binFind(new Point(parseInt(ss[0]),parseInt(ss[1])));
st.register(t);
}
st.printFre();
}
br.close();
}

static int parseInt(String s){
if(s.startsWith("-"))
return -parseInt(s.substring(1));
int t = 0;
for(char ch: s.toCharArray()){
t *= 10;
t += ch-'0';
}
return t;
}
}

class Statistics{
Map< Integer,Integer> map=new TreeMap< Integer,Integer>();
Map< Integer,Integer> fre=new TreeMap< Integer,Integer>();
void register(int x){
Integer t = map.get(x);
if(t==null){
map.put(x, 1);
Integer t2 = fre.get(1);
if(t2==null){
fre.put(1, 1);
}
else{
fre.put(1, t2+1);
}
}
else{
map.put(x, t+1);
Integer t2 = fre.get(t);
fre.put(t, t2-1);
t2 = fre.get(t+1);
if(t2==null){
fre.put(t+1, 1);
}
else{
fre.put(t+1, t2+1);
}
}
}

void printFre(){
System.out.println("Box");
int t;
for(Map.Entry< Integer, Integer> me: fre.entrySet()){
if((t=me.getValue())>0)
System.out.println(me.getKey()+": "+t);
}
}
}
class LineSet{
Map< Integer,Line> mil;
Object[] lines;
LineSet(int n){
mil = new TreeMap< Integer,Line>();
}

mil.put(key, line);
}
//Binary search method
int binFind(Point p){
lines=mil.values().toArray();
int i=binFind0(p,0,lines.length-1);
return i;
}

private int binFind0(Point p, int left, int right){
if(p.compareTo((Line)lines[left])< 0)
return left;
if(p.compareTo((Line)lines[right])>0)
return right+1;
int mid = (left+right)/2;
if(p.compareTo((Line)lines[mid])< 0)
return binFind0(p,left,mid-1);
else return binFind0(p,mid+1,right);
}
}

class Point implements Comparable< Line>{

int x;
int y;
Point(int x,int y){
this.x=x;
this.y=y;
}
@Override
public int compareTo(Line line) {
return x*line.x0+y*line.y0+line.c;
}
}

class Line{
//x0 must be positive
int x0;
int y0;
int c;
//p must be up, p2 must be down
Line(Point p, Point p2){
x0=p.y-p2.y;
y0=p2.x-p.x;
c=-(p.x*x0+p.y*y0);
}

@Override
public String toString(){
return "Line: "+x0+"x+"+y0+"y+"+c+"=0.";

}
}

1. 第一句可以忽略不计了吧。从第二句开始分析，说明这个花色下的所有牌都会在其它里面出现，那么还剩下♠️和♦️。第三句，可以排除2和7，因为在两种花色里有。现在是第四句，因为♠️还剩下多个，只有是♦️B才能知道答案。