1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| import java.util.ArrayList; import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int cowNumber = scan.nextInt(); int end=scan.nextInt();
ArrayList<Cow> cow = new ArrayList<Cow>(cowNumber);
for(int i=0;i<cowNumber;i++){ int a = scan.nextInt(); int b = scan.nextInt(); Cow newcow = new Cow(a,b); cow.add(newcow); }
int workCowNum=0; int worktime=0; int workstart = cow.get(0).start; int workend = cow.get(0).end; boolean iswork=false; boolean isend=false; for (int i=0;i<cow.size();i++){ for(int j=i;j<cow.size();j++){ if(cow.get(j).end>=end){ i=j; iswork = true; isend = true; break; } if(workstart<=cow.get(j).start&&workend>=cow.get(j).end){ if(worktime<=cow.get(j).end-workend) { iswork=true; i=j; } } }
if(iswork){ workstart = workend; workend = cow.get(i).end; worktime = cow.get(i).end - workend; workCowNum++; iswork = false; }
if(isend){ break; } }
System.out.println(workCowNum+1); } }
class Cow{ int start; int end; public Cow(int a,int b){ this.start = a; this.end = b; } }
|