题目
输入输出样例: 输入:
3
0 2
2 4
1 3
输出:
2
思路:参考不相交区间调度问题
代码
import java.util.Arrays
;
import java.util.Comparator
;
import java.util.Scanner
;
public class Main
{
public static void main
(String
[] args
) {
Scanner sc
=new Scanner
(System.in
);
int n
=sc.nextInt
();
Games
[] games
=new Games
[n
];
for (int i
= 0
; i
< n
; i++
) {
int s
=sc.nextInt
();
int t
=sc.nextInt
();
games
[i
]=new Games
(s,t
);
}
Arrays.sort
(games,new CompareTo
());
int count
=0
;
int tt
=0
;
for (int i
= 0
; i
< n
; i++
) {
if (tt
<=games
[i
].s
){//前一个数的尾小于当前的头
count++
;
tt
=games
[i
].t
;//取尾
}
}
System.out.println
(count
);
}
}
class Games
{
public int s
;//开始时间
public int t
;//结束时间
public Games
(int s, int t
) {
this.s
=s
;
this.t
=t
;
}
}
class CompareTo implements Comparator
<Games
>{
public int compare
(Games t1,Games t2
){
return t1.t-t2.t
;
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-13872.html