-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1803.java
More file actions
36 lines (32 loc) · 1.02 KB
/
Copy pathP1803.java
File metadata and controls
36 lines (32 loc) · 1.02 KB
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
import java.util.Scanner;
import java.util.Comparator;
import java.util.Arrays;
//贪心算法中会议安排的核心思想就是
//1、将会议按照结束时间由小到大排列
//2、在确定第一场会议的基础上寻找下一场开始时间在此会议结束时间之后的会议
public class P1803 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] arr = new int[n][2];
for (int i = 0; i < n; i++) {
arr[i][0] = sc.nextInt();
arr[i][1] = sc.nextInt();
}
// 将会议按照结束时间升序排列
Arrays.sort(arr, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[1] - b[1];
}
});
int cnt = 1;
int j = 0;
for (int i = 0; i < n; i++) {
if (arr[i][0] >= arr[j][1]) {
cnt++;
j = i;
}
}
System.out.println(cnt);
}
}