解题思路:首先将时间点转化成时间段,通过上课时间和通勤时间计算最晚起床时间,排序,找到排在最晚起床时间的前一个值,再转化成时间点即可。
import java.util.;
public class Main{
public static void main(String arg[]){
Scanner scan=new Scanner(System.in);
int N=scan.nextInt();
int[] H=new int[N];
int[] M=new int[N];
int[] C=new int[N];
int[] D=new int[N+1];
int x;
int A,B;
int flag=0;
for(int i=0;i<N;i++){
H[i]=scan.nextInt();
M[i]=scan.nextInt();
C[i]=H[i]
60+M[i];
D[i+1]=C[i];
}
x=scan.nextInt();
A=scan.nextInt();
B=scan.nextInt();
D[0]=A*60+B-x;
flag=quickSort(D,0,N);
for(int i=0;i<N;i++){
if(C[i]==D[flag]){
System.out.print((C[i]/60));
System.out.print(' ');
System.out.print((C[i]%60));
break;
}
else if(C[i]==D[flag-1]){
System.out.print(C[i]/60);
System.out.print(' ');
System.out.print(C[i]%60);
break;
}
}
}
public static int quickSort(int array[], int low, int high) {// 传入low=0,high=array.length-1;
int pivot, p_pos, i, t;// pivot->位索引;p_pos->轴值。
p_pos=0;
if (low < high) {
p_pos = low;
pivot = array[p_pos];
for (i = low + 1; i <= high; i++)
if (array[i] <= pivot) {
p_pos++;
t = array[p_pos];
array[p_pos] = array[i];
array[i] = t;
}
t = array[low];
array[low] = array[p_pos];
array[p_pos] = t;
// 分而治之
quickSort(array, low, p_pos - 1);// 排序左半部分
quickSort(array, p_pos + 1, high);// 排序右半部分
}
return p_pos;
}
}