Assigning a Meeting Room

In our company, there is a popular meeting room. Many meetings are reserved to use this room with starting time and finishing time, and the place can’t hold two meetings at the same time if the time is overlapped. Thus only one of the same time-framed meetings can be held in here and the others should give up. However, it is not overlapped for the same starting time of a meeting and the finishing time of another meeting.
When the number of meetings, starting time and finishing time of each meetings are given, you want to hold as many meetings as possible.
In a given number of meetings and each given starting/finishing time, you want to hold as many meetings as possible. Create a program that can assign meetings to the meeting room as many as it can.

Time limit: 1 second (java: 2 seconds)

[Input]
Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 20) are given in a row.
N, the number of meetings is given in the first row of each test case (1 ≤ N ≤ 500).
Numbers of each meeting, starting time, and finishing time are given separately from the second row to the number of N. (Each number is natural numbers below 500)

[Output]
Output the maximum number of meetings which can assign in the first row of each test case.

[I/O Example]

Input
2
6
1 1 10
2 5 6
3 13 15
4 14 17
5 8 14
6 3 12
15
1 4 8
2 2 5
3 2 6
4 4 6
5 2 3
6 1 6
7 4 7
8 3 5
9 3 8
10 1 2
11 1 7
12 2 4
13 5 6
14 4 5
15 7 8

Output
3
5

 

 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class AssignMeeting {

    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        //        sc = new Scanner(new FileInputStream("agreenMeeting.txt"));

        ArrayList<Meetting> mSet = new ArrayList<Meetting>();
        //        TreeSet<Meetting> mSet = new TreeSet<Meetting>(); // 不能用TreeSet, TreeSet会把compareTo相同元素合并, TreeSet不是根据Hashcode去判断;

        int T = sc.nextInt();
        for (int tc = 0; tc < T; tc++) {
            int n = sc.nextInt();
            for (int i = 0; i < n; i++) {
                Meetting m = new Meetting();
                m.index = sc.nextInt();
                m.start = sc.nextInt();
                m.end = sc.nextInt();
                mSet.add(m);
                //                System.out.println("index:" + m.index + ", mSet.size(): " + mSet.size());
            }

            Collections.sort(mSet);

            int selectCount = 0;
            int endtime = 0;
            int i = 0;
            // 从第一个查找开始后面的, 条件:比当时间结束时间大或者相同的,优先选择相同的;
            for (Meetting m : mSet) {
                //                m.toStr();
                if (i == 0) {
                    endtime = m.end;
                    selectCount++;
                    //                    m.toStr();
                } else {
                    if (m.start == endtime) {
                        endtime = m.end;
                        selectCount++;
                        //                        m.toStr();
                    } else if (m.start > endtime) {
                        endtime = m.end;
                        selectCount++;
                        //                        m.toStr();
                    }
                }
                i++;
            }

            System.out.println(selectCount);
            mSet.clear();
        }

    }

    static class Meetting implements Comparable {
        @Override
        public int hashCode() {
            return index;
        }

        public int index;

        public int start;

        public int end;

        public void toStr() {
            System.out.println("index:" + index + ", start: " + start + ", end: " + end);
        }

        @Override
        public int compareTo(Object o) {
            Meetting m = (Meetting) o;
            return this.end - m.end;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值