0基础学Java:Lab7-回溯算法-间隔分割

        明天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);
		}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值