贪心算法--会议安排

[题目描述]

在大公司里,会议是很多的,开会得有场子,要场子你得先在电子流里预订。
如果你是项目组新来的小弟,那么恭喜你,每天抢订会议室的任务就光荣的分给你了。
老大要求你尽可能多的订会议室,但是这些会议室之间不能有时间冲突。

[Input]
input文件中可以包括多个测试案例。
T(T ≤ 20),输入文件的第一行表示文件中有多少个测试案例。
N(1 ≤ N ≤ 500),每个测试案例的第一行表示会议室的数目。
每个测试案例中,除第一行以外表示各个会议室的信息。每行会有3个数字,分别表示会议编号、会议起始时间、会议结束时间。


[Output]
输出可以安排的最大会议数目

[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


下面是C#的实现代码:

        static void Main(string[] args)
        {
            int count = 1;

            int N = Convert.ToInt32(Console.ReadLine());
            Test[] test = new Test[N];

            for (int i = 0; i < test.Length; i++)
            {
                test[i] = new Test();
                string[] ss = Console.ReadLine().Split();
                test[i].number = Convert.ToInt32(ss[0]);
                test[i].start = Convert.ToInt32(ss[1]);
                test[i].end = Convert.ToInt32(ss[2]);
            }
           test= test.OrderBy(s =>s.end).ToArray();//升序排序

            Console.WriteLine();
             foreach (var item in test)
             {
                 Console.WriteLine(item.number+" "+item.start+" "+item.end);
             }
            int index = 0;
            for (int i = index; i < test.Length; i++)
            {
                Console.WriteLine("第一遍");
                for (int j = index+1; j < test.Length; j++)//j初始为0+1的原因是因为排序后,第一场是肯定要选取的,所以不用在比较第一场了
                {
                    if (j >= test.Length) break;//防止数组下标越界

                    if(test[j].start>=test[i].end)//寻找之后会议开始的时间,有没有大于等于上一场结束时间的,如果有
                    {
                        index = j;
                        i = index-1;
                        count++;
                        break;
                    }
                }
            }
            Console.WriteLine(count);

        }

猜你喜欢

转载自blog.csdn.net/w199753/article/details/80028685