加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

今年暑假不ac的java版,虽然不懂的贪心但贪心是一种很自然的思想

发布时间:2020-12-15 07:24:42 所属栏目:Java 来源:网络整理
导读:import java.util.* ; class node implements Comparablenode { 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{
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(); 
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读