Baozi Training Google Code Jam 2020 Qualification Round: Parenting Partnering Returns Solution

Problem Statement

Problem

Cameron and Jamie's kid is almost 3 years old! However, even though the child is more independent now, scheduling kid activities and domestic necessities is still a challenge for the couple.

Cameron and Jamie have a list of N activities to take care of during the day. Each activity happens during a specified interval during the day. They need to assign each activity to one of them, so that neither of them is responsible for two activities that overlap. An activity that ends at time t is not considered to overlap with another activity that starts at time t.

For example, suppose that Jamie and Cameron need to cover 3 activities: one running from 18:00 to 20:00, another from 19:00 to 21:00 and another from 22:00 to 23:00. One possibility would be for Jamie to cover the activity running from 19:00 to 21:00, with Cameron covering the other two. Another valid schedule would be for Cameron to cover the activity from 18:00 to 20:00 and Jamie to cover the other two. Notice that the first two activities overlap in the time between 19:00 and 20:00, so it is impossible to assign both of those activities to the same partner.

Given the starting and ending times of each activity, find any schedule that does not require the same person to cover overlapping activities, or say that it is impossible.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case starts with a line containing a single integer N, the number of activities to assign. Then, N more lines follow. The i-th of these lines (counting starting from 1) contains two integers Si and Ei. The i-th activity starts exactly Si minutes after midnight and ends exactly Ei minutes after midnight.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is IMPOSSIBLE if there is no valid schedule according to the above rules, or a string of exactly N characters otherwise. The i-th character in y must be C if the i-th activity is assigned to Cameron in your proposed schedule, and J if it is assigned to Jamie.

If there are multiple solutions, you may output any one of them. (See "What if a test case has multiple correct solutions?" in the Competing section of the FAQ. This information about multiple solutions will not be explicitly stated in the remainder of the 2020 contest.)

Limits

Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
0 ≤ Si < Ei ≤ 24 × 60.

Test set 1 (Visible Verdict)

2 ≤ N ≤ 10.

Test set 2 (Visible Verdict)

2 ≤ N ≤ 1000.

Sample


Input
 

Output
 
4
3
360 480
420 540
600 660
3
0 1440
1 3
2 4
5
99 150
1 100
100 301
2 5
150 250
2
0 720
720 1440

  
Case #1: CJC
Case #2: IMPOSSIBLE
Case #3: JCCJJ
Case #4: CC

  

Sample Case #1 is the one described in the problem statement. As mentioned above, there are other valid solutions, like JCJ and JCC.

In Sample Case #2, all three activities overlap with each other. Assigning them all would mean someone would end up with at least two overlapping activities, so there is no valid schedule.

In Sample Case #3, notice that Cameron ends an activity and starts another one at minute 100.

In Sample Case #4, any schedule would be valid. Specifically, it is OK for one partner to do all activities.

 

Problem link
 

Video Tutorial

You can find the detailed video tutorial here

 

Thought Process

If you remember the leetcode "merge interval" question, this is quite similar. Baozi Training "Merge Interval"
Model the time range in internal, sort the intervals in ascending order based on the start time, then schedule either "C" or "J" as long as they are available. Update the end time with the assigned task.
The only caveat is we have to remember the original task index for output purpose because we sort the interval using the start time. 
 
An alternate solution is a "bipartite graph coloring" problem. In essence, model the interval into an interval graph. Each interval is a vertex in the graph, and if it overlaps with another interval, there would be an edge. Now the problem becomes starting with one node, if we could color the nodes in two colors without any conflicts. Note the graph doesn't need to be fully connected, a simple BFS with a visited hash map should do the trick. https://www.geeksforgeeks.org/bipartite-graph/
The time complexity of "bipartite graph coloring" is O(V+E)
 

 

Solutions

Greedy solution

 1 import java.io.BufferedReader;
 2 import java.io.InputStreamReader;
 3 import java.util.*;
 4 
 5 public class Solution {
 6     public static void main(String[] args) {
 7         Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
 8 
 9         int testCases = s.nextInt();
10         int caseNum = 1;
11         Solution p = new Solution();
12 
13         while (caseNum <= testCases) {
14             int n = s.nextInt();
15             int[][] a = new int[n][2];
16             for (int i = 0; i < n; i++) {
17                 for (int j = 0; j < 2; j++)
18                     a[i][j] = s.nextInt();
19             }
20             System.out.println(String.format("Case #%d: %s", caseNum, p.calSchedule(a)));
21 
22             caseNum++;
23         }
24     }
25 
26     private String calSchedule(int[][] scheudle) {
27         if (scheudle == null || scheudle.length == 0 || scheudle[0].length == 0) {
28             return null;
29         }
30 
31         List<IntervalWithIndex> l = new ArrayList<>();
32         for (int i = 0; i < scheudle.length; i++) {
33             l.add(new IntervalWithIndex(scheudle[i][0], scheudle[i][1],i));
34         }
35 
36         Collections.sort(l);
37 
38         int cEndTime = Integer.MIN_VALUE;
39         int jEndTime = Integer.MIN_VALUE;
40 
41         char[] result = new char[l.size()];
42 
43         for (int i = 0; i < l.size(); i++) {
44             IntervalWithIndex it = l.get(i);
45             if (it.start >= cEndTime) {
46                 result[it.originalIndex] = 'C';
47                 cEndTime = it.end;
48             } else if (it.start >= jEndTime) {
49                 result[it.originalIndex] = 'J';
50                 jEndTime = it.end;
51             } else {
52                 return "IMPOSSIBLE";
53             }
54         }
55         StringBuilder sb = new StringBuilder();
56         for (char c : result) {
57             sb.append(c);
58         }
59 
60         return sb.toString();
61     }
62 
63     private class IntervalWithIndex implements Comparable<IntervalWithIndex> {
64         public int start;
65         public int end;
66         public int originalIndex;
67 
68         public IntervalWithIndex(int start, int end, int originalIndex) {
69             this.start = start;
70             this.end = end;
71             this.originalIndex = originalIndex;
72         }
73 
74         // natural ordering ascending
75         public int compareTo(IntervalWithIndex t) {
76             return this.start - t.start;
77         }
78     }
79 }

 

 

Time Complexity: O(lgN) since we sort
Space Complexity: O(N) since we used extra list for the intervals

References

posted @ 2020-05-11 15:25  包子模拟面试  阅读(262)  评论(0编辑  收藏  举报