Make the Most (Hackerrank Codeagon)

题目链接

Problem Statement

Codenation is sending N of its employees to a High Profile Business Conference and the goal is to cover maximum number of presentations, to create maximum business opportunities. It has a list of all the presentations with their start and end times. You need to help Codenation decide an optimal allocation.

The start and end times of a presentation are provided to you as a single string and are separated by a space.

Each time is given in the form HH:MM and represents a time between 08:00 in the morning and 08:00 in the evening inclusive.

Input Format
First line contains an integer N (number of employees). 
The second line contains integer P (number of presentations). 
P lines follow each having conference start time and end time separated by a space.

Constraints
1N10 
1P50 
Each of P lines will contain 11 characters in the form HH:MM HH:MM
Each HH:MM will represent a time between 8 am and 8 pm inclusive. 
Each MM will be between 00 and 59, inclusive. 
In each element of presentations the second time will be strictly later than the first.

Output Format
Print the maximum number of presentations that can be attended.

Note: We can assume the time taken for going from one presentation to another is negligible; i.e. if end time of one presentation is same time as start time of another, a single person can cover both presentations from start to end.

Sample Input#00

3
5
08:00 08:00
08:00 08:00
08:00 08:00
08:00 08:00
08:00 08:00

Sample Output#00

3

Explanation#00

All these presentations last all day long. Nobody can cover more than one.

Sample Input#01

2
5
09:00 08:00
08:00 12:00
12:00 08:00
08:00 08:00
08:00 08:00

Sample Output#01

3

Explanation#01
One person can cover the 8-12 presentation and the 12-8 presentation. The other person can cover one of the all-day presentation.

Sample Input#02

1
5
08:00 01:00
08:25 12:50
12:00 12:30
12:30 08:00
08:00 08:00

Sample Output#02

2
题目分析:
  实际上是个作业安排问题的扩展问题。即给定N个会议,每个会议有开始时间和结束时间,有M个人,问M个人最多总共可以参加多少场会议。
如果M=1,当然就是按照结束时间排序,即可。
这里只需要简单的变形一下,设b[j]为第j个人当前所位于的时间点(初始都为0), 对于第i个会议,如果有多个满足条件的人,选择b[j]最大的那个。
附上代码:
 1 #include <cmath>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 typedef pair<int, int> pii;
 9 #define F first
10 #define S second
11 
12 int main() {
13     int N, P;
14     scanf("%d %d", &N, &P);
15     pii t[55];
16     for (int i = 0; i < P; i++) {
17         int h1, m1, h2, m2, start, end;
18         scanf("%d:%d %d:%d", &h1, &m1, &h2, &m2);
19         start = (h1 >= 8 ? (h1 - 8) * 60 : (h1 + 4) * 60) + m1;
20         end = ((h2 > 8 || h2 == 8 && m2) ? (h2 - 8) * 60 : (h2 + 4) * 60) + m2;
21         t[i] = pii(end, start);
22     }
23     sort(t, t + P);
24     for (int i = 0; i < P; i++) cerr << t[i].S << ' ' << t[i].F << endl;
25     int ans = 0;
26     int now[55] = {0};
27     for (int i = 0; i < P; i++) {
28         int x = -1, last = -1;
29         for (int j = 0; j < N; j++) {
30             if (t[i].S >= now[j]) {
31                 if (now[j] > last) {
32                     x = j;
33                     last = now[j];
34                 }
35             }
36         }
37         if (x != -1) {
38             ans++;
39             now[x] = t[i].F;
40         }
41     }
42     printf("%d\n", ans);
43     
44     return 0;
45 }

                            

你可能感兴趣的:(code)