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());
        }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值