This summer is not ac version of java, but did not understand the greed greed is a natural thought, read, read it uses greedy

import java.util. * ;

class node implements Comparable<node>
{
     int begin;
     int end;
    @Override
    public int compareTo(node arg0) {
        if(this.begin==arg0.begin)return this.end-arg0.end;
        else return this.begin-arg0.begin;
    }
     
}

class solution
{
 private int T=0;
 private ArrayList<node> res=new ArrayList<node>();
 private Scanner iner=new Scanner(System.in);
 private int src=0;
 private int last=0;
 
 
 public void add()
 {
     while(iner.hasNext())
     {
         T = iner.nextInt ();
         src=0;
         last=0;
         res.clear();
         if(T==0)
         {
             break;
         }
         for(int i=0;i<T;++i)
         {   
             node temp=new node();
             temp.begin=iner.nextInt();
             temp.end = iner.nextInt ();
             res.add(temp);
         }
         Collections.sort(res);
         for(int i=0;i<T;++i)
         {
             node temp=res.get(i);     
             if(i==0)
             {
                 src++;
                 last=temp.end;
             }
             else if(i>0)
             {
                 if(temp.begin<last&&temp.end<=last)
                 {
                     last=temp.end;
                 }
                 if(temp.begin>=last)
                 {
                     src++;
                     last=temp.end;
                 }
             }     
         }
         System.out.println(src);
     }
 }
  
}

public class Main {
    public static void main(String[] args) {
        solution space = new solution();
        space.add(); 
    }
}

 

Guess you like

Origin www.cnblogs.com/z2529827226/p/11665123.html