Double bill screens
HardOnline Assessment
Assign the films of a one-day festival to its two screens, screen 1 and screen 2, so that every programming rule holds.
There are n films, numbered 0 to n - 1, and every film plays on exactly one of the two screens. rules is a list of [kind, i, j] entries. In every rule i and j are film numbers (they may be equal); the rule kinds are:
"apart": filmsiandjmust be on different screens."same": filmsiandjmust be on the same screen."not1": filmsiandjmust not both be on screen 1."not2": filmsiandjmust not both be on screen 2."on": filmimust be on screenj(herejis1or2, not a film number).
A rule with i == j applies to that single film as written, so ["apart", 4, 4] can never be satisfied and ["not1", 4, 4] means film 4 is not on screen 1.
Many assignments can be valid. The festival director wants the one that sends films to screen 1 as eagerly as possible: compare assignments film by film from film 0, and prefer the assignment that has screen 1 at the first film where two assignments differ.
Input: n and rules.
Output: a string of n characters, "1" or "2", where character k is the screen of film k in the preferred assignment. If no assignment satisfies every rule, return "IMPOSSIBLE". With n = 0 the answer is the empty string "".
There can be a few hundred rules, so trying every assignment, or backtracking film by film, is far too slow.
Example 1
- Input:
- [4,[["same",0,3],["on",3,2],["apart",1,2]]]
- Output:
- "2122"
- Explanation
- Film 0 must share a screen with film 3, and film 3 is pinned to screen 2, so film 0 is forced onto screen 2 even though screen 1 looks harmless at first glance. Film 1 prefers screen 1, which forces film 2 (apart from film 1) onto screen 2. Film 3 is on screen 2. The answer is
"2122".
Example 2
- Input:
- [3,[["apart",0,1],["apart",1,2],["apart",0,2]]]
- Output:
- "IMPOSSIBLE"
- Explanation
- Films 0, 1 and 2 would each have to be on a different screen from both others (three films, two screens), which is impossible, so the answer is
"IMPOSSIBLE".
Example 3
- Input:
- [3,[["apart",0,1],["not2",1,2]]]
- Output:
- "121"
- Explanation
- Film 0 prefers screen 1, which forces film 1 (apart from film 0) onto screen 2. Film 1 and film 2 are not both allowed on screen 2, so film 2 must be on screen 1. The answer is
"121".
Constraints
0 <= n <= 1500 <= rules.length <= 500- for every rule except
"on",0 <= i, j < n; for"on",0 <= i < nandjis1or2