• 今年暑假不ac的java版,虽然不懂的贪心但贪心是一种很自然的思想,写着写着就用到了贪心


    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(); 
        }
    }
  • 相关阅读:
    Linux基本结构
    Linux诞生
    Python之克隆
    Python之数据类型转换
    gb18030与utf-8
    for循环与while循环
    code::blocks调试
    关于隐式创建vue实例实现简化弹出框组件显示步骤
    blob canvas img dataUrl的互相转换和用处
    观察者模式与发布订阅模式的区别
  • 原文地址:https://www.cnblogs.com/z2529827226/p/11665123.html
Copyright © 2020-2023  润新知