Problem:
This is called greedy algorithm introduction, the idea is to first the various time periods according to the end time to sort (by the end of the earlier traversal, the more programs),
From the first show, if the next program start time is greater than the previous program's start time, then the program, in turn.
Input, requires the input two number of a set of a number of columns, began to use two array effect is not obvious, then use a two-dimensional array,
It's a bit of a hassle, but the structure is clear.
Not AC this summer holidayTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 34333 Accepted Submission (s): 18301
Problem Description "Not AC this summer?" ”
Yes ”
"Then what do you do?" ”
"Watch the World Cup, you idiot!" ”
"@#$%^&*% ..."
Indeed, the World Cup has come, the fans of the festival has come, it is estimated that many acmer will also throw away the computer, to the TV.
As fans, must want to see as much as possible complete game, of course, as a new era of good youth, you will certainly see some other programs, such as news broadcast (never forget about the state of affairs), very 6+7, Super girls, and Wang Xiao Ya's "Happy Dictionary" and so on, If you already know the schedule of all the TV shows you like to watch, will you make reasonable arrangements? (The goal is to be able to see as many full programs as possible)
Input data contains multiple test instances, and the first line of each test instance has only one integer n (n<=100), representing the total number of shows you like to see, followed by n rows of data, each row containing two data ti_s,ti_e (1<=i<=n), Represents the start and end times of the I program, respectively, in order to simplify the problem, each time is represented by a positive integer. N=0 indicates that the input is finished and not processed.
Output for each test instance, outputs the number of TV shows that can be fully seen, one row for each test instance output.
Sample Input
121 33 40 73 815 1915 2010 158 186 125 104 142 90
Sample Output
5
Code:
Import java.util.*;p ublic class main{public static void Main (string[] args) {Scanner cin=new Scanner (system.in); while ( Cin.hasnext ()) {int n=cin.nextint (); if (n==0) break;else{int a[][]=new int[2][n];for (int i=0;i<n;i++) {a[0][i]= Cin.nextint (); A[1][i]=cin.nextint ();} for (int i=0;i<n-1;i++) {for (int j=i+1;j<n;j++) {if (a[1][i]>a[1][j]) {int t=a[0][i];a[0][i]=a[0][j];a[0][j]= T;int c=a[1][i];a[1][i]=a[1][j];a[1][j]=c;}}} int s;int count=1;s=a[1][0];for (int i=1;i<n;i++) {if (a[0][i]>=s) {count++;s=a[1][i];}} System.out.println (count);}}}
HDU 2037 this summer holiday does not AC (Java)