明天Java上机考试,今天初学Java,今日成果:学会了Java的输入输出,明天考试,未来可期。
题目描述
描述
假设:
- 有n个讲座,每个讲座都有固定的开始时间和结束时间。
- 每个讲座都需要一个报告厅。
- 同一时间内只能在一个报告厅举行一场讲座。
- 如果第二场讲座的开始时间等于第一场讲座的结束时间,仍然允许在同一个报告厅中举行连续的两场讲座
你需要找到安排所有讲座所需的最小报告厅数量。
输入
第一行是一个正整数n,表示要安排的讲座数量。
接下来的n行中,每行有两个整数,表示每个讲座的开始时间和结束时间。时间从0开始计算。
输出
打印所需的最小报告厅数量
样例输入输出
样例1
输入:
5
1 23
12 28
25 35
27 80
36 50
输出:
3
样例2
输入:
4
1 3
5 7
5 8
2 5
输出:
2
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Main{
public static class Lectures {
public int l_start;
public int l_end;
public Lectures(int l_start, int l_end) {
this.l_start = l_start;
this.l_end = l_end;
}
public int getL_start() {
return l_start;
}
public int getL_end() {
return l_end;
}
public static class Com_start implements Comparator<Lectures> {
public int compare(Lectures l1, Lectures l2)
{
return l1.l_start - l2.l_start;
}
}
public static class Com_end implements Comparator<Lectures> {
public int compare(Lectures l1, Lectures l2)
{
return l1.l_end - l2.l_end;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Lectures> lectures = new ArrayList();
for (int i = 0; i < n; i++) {
int l_start = sc.nextInt();
int l_end = sc.nextInt();
Lectures lecture = new Lectures(l_start, l_end);
lectures.add(lecture);
}
Collections.sort(lectures, new Lectures.Com_start());
ArrayList<Lectures> lec_ed = new ArrayList();
int sum = 1;
lec_ed.add(lectures.get(0));
lectures.remove(0);
while (!lectures.isEmpty())
{
if(lec_ed.get(0).getL_end() > lectures.get(0).getL_start())
{
sum++;
}
else
{
lec_ed.remove(0);
}
lec_ed.add(lectures.get(0));
Collections.sort(lec_ed, new Lectures.Com_end());
lectures.remove(0);
}
System.out.println(sum);
}
}