标签:end bre else next imp vat ati ble port
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(); } }
今年暑假不ac的java版,虽然不懂的贪心但贪心是一种很自然的思想,写着写着就用到了贪心
标签:end bre else next imp vat ati ble port
原文地址:https://www.cnblogs.com/z2529827226/p/11665123.html