hdu4268 NavigableSet

Alice和Bob有很多卡片,Alice想用自己的卡片覆盖Bob的卡片;
如果一张卡片的h和w都大于等于另一张,则可以覆盖,每张卡片只能用一次,
且卡片不能旋转

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NavigableSet;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;

import org.omg.CORBA.Object;




public class Main {


    public static void main(String[] args) throws IOException{

           StreamTokenizer cin = new StreamTokenizer(new BufferedInputStream(System.in)); 
           InputReader in = new InputReader(System.in)  ;
           PrintWriter out = new PrintWriter(System.out) ;

           int t = in.nextInt() ;
           while(t-- > 0){

                new Task().solve(in, out)  ;   //out.flush() ;
           }

           out.flush() ; 


    }


}

class Task{
        static class Rec implements Comparable<Rec>{
               int h ;
               int w ;

               Rec(int h , int w){
                    this.h = h ;
                    this.w = w ;        
               }

               public int compareTo(Rec o) {
                      if(h != o.h) return Integer.compare(h, o.h) ;
                      else return Integer.compare(w, o.w) ;
               }
        }


        Rec[] alice , bob ;
        public void solve(InputReader in , PrintWriter out) throws IOException{
               int n = in.nextInt() ;
               alice = new Rec[n] ;
               bob = new Rec[n] ;
               for(int i = 0 ; i < n ; i++) alice[i] = new Rec(in.nextInt() , in.nextInt()) ;
               for(int i = 0 ; i < n ; i++) bob[i] = new Rec(in.nextInt() , in.nextInt()) ;
               Arrays.sort(alice);
               Arrays.sort(bob) ;

               NavigableSet<Rec> st = new TreeSet<Rec>() ;

               int sum = 0 ;
               int k = -1 , j ; 
               for(int i = 0 ; i < n ; i++){
                    for(j = k+1 ; j < n && bob[j].h <= alice[i].h; j++){
                          st.add(new Rec(bob[j].w , j)) ;      
                    }
                    k = j - 1 ;
                    Rec r = st.floor(new Rec(alice[i].w , n)) ;
                    if(r != null){
                          sum++ ;
                          st.remove(r) ;
                    }
               }
               out.println(sum) ;
        }

}


class InputReader{
        public BufferedReader  reader;
        public StringTokenizer  tokenizer;

        public InputReader(InputStream stream){
                    reader = new BufferedReader(new InputStreamReader(stream), 32768) ;
                    tokenizer = null ;
        }

        public String next(){
                    while(tokenizer == null || ! tokenizer.hasMoreTokens()){
                        try{
                                tokenizer = new StringTokenizer(reader.readLine());
                        }catch (IOException e) {
                                throw new RuntimeException(e);
                        }
                    }
                    return tokenizer.nextToken();  
        }

        public int  nextInt() {
                    return Integer.parseInt(next());
        }

        public long nextLong() {
                    return Long.parseLong(next());
        }

}

你可能感兴趣的:(hdu4268 NavigableSet)