| Task: | Thieves and Prisons |
| Sender: | Luis Galan |
| Submission time: | 2019-03-06 13:18:53 +0200 |
| Language: | Java |
| Status: | READY |
| Result: | 8 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 8 |
| #2 | TIME LIMIT EXCEEDED | 0 |
| #3 | TIME LIMIT EXCEEDED | 0 |
| #4 | TIME LIMIT EXCEEDED | 0 |
| #5 | TIME LIMIT EXCEEDED | 0 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.32 s | 2, 4, 5 | details |
| #2 | ACCEPTED | 0.22 s | 2, 4, 5 | details |
| #3 | ACCEPTED | 0.22 s | 2, 4, 5 | details |
| #4 | ACCEPTED | 0.22 s | 2, 4, 5 | details |
| #5 | ACCEPTED | 0.23 s | 2, 4, 5 | details |
| #6 | ACCEPTED | 0.31 s | 4, 5 | details |
| #7 | ACCEPTED | 0.23 s | 4, 5 | details |
| #8 | ACCEPTED | 0.29 s | 4, 5 | details |
| #9 | ACCEPTED | 0.32 s | 1, 3, 4, 5 | details |
| #10 | ACCEPTED | 0.30 s | 1, 3, 4, 5 | details |
| #11 | ACCEPTED | 0.32 s | 1, 3, 4, 5 | details |
| #12 | ACCEPTED | 0.23 s | 1, 3, 4, 5 | details |
| #13 | ACCEPTED | 0.32 s | 1, 3, 4, 5 | details |
| #14 | ACCEPTED | 0.31 s | 1, 3, 4, 5 | details |
| #15 | ACCEPTED | 0.31 s | 1, 3, 4, 5 | details |
| #16 | ACCEPTED | 0.23 s | 1, 3, 4, 5 | details |
| #17 | ACCEPTED | 0.23 s | 1, 2, 3, 4, 5 | details |
| #18 | ACCEPTED | 0.31 s | 1, 3, 4, 5 | details |
| #19 | TIME LIMIT EXCEEDED | -- | 2, 5 | details |
| #20 | TIME LIMIT EXCEEDED | -- | 2, 5 | details |
| #21 | TIME LIMIT EXCEEDED | -- | 2, 5 | details |
| #22 | TIME LIMIT EXCEEDED | -- | 5 | details |
| #23 | TIME LIMIT EXCEEDED | -- | 5 | details |
| #24 | ACCEPTED | 0.38 s | 3, 4, 5 | details |
| #25 | ACCEPTED | 0.36 s | 3, 4, 5 | details |
| #26 | ACCEPTED | 0.38 s | 3, 4, 5 | details |
| #27 | ACCEPTED | 0.38 s | 3, 4, 5 | details |
| #28 | TIME LIMIT EXCEEDED | -- | 4, 5 | details |
| #29 | TIME LIMIT EXCEEDED | -- | 4, 5 | details |
| #30 | TIME LIMIT EXCEEDED | -- | 4, 5 | details |
| #31 | TIME LIMIT EXCEEDED | -- | 4, 5 | details |
| #32 | TIME LIMIT EXCEEDED | -- | 2, 4, 5 | details |
| #33 | TIME LIMIT EXCEEDED | -- | 2, 4, 5 | details |
| #34 | TIME LIMIT EXCEEDED | -- | 2, 4, 5 | details |
| #35 | TIME LIMIT EXCEEDED | -- | 2, 4, 5 | details |
| #36 | TIME LIMIT EXCEEDED | -- | 3, 5 | details |
| #37 | TIME LIMIT EXCEEDED | -- | 3, 5 | details |
| #38 | TIME LIMIT EXCEEDED | -- | 3, 5 | details |
| #39 | TIME LIMIT EXCEEDED | -- | 3, 5 | details |
| #40 | TIME LIMIT EXCEEDED | -- | 5 | details |
| #41 | TIME LIMIT EXCEEDED | -- | 5 | details |
| #42 | TIME LIMIT EXCEEDED | -- | 5 | details |
| #43 | TIME LIMIT EXCEEDED | -- | 5 | details |
| #44 | TIME LIMIT EXCEEDED | -- | 2, 5 | details |
| #45 | TIME LIMIT EXCEEDED | -- | 2, 5 | details |
| #46 | TIME LIMIT EXCEEDED | -- | 2, 5 | details |
| #47 | TIME LIMIT EXCEEDED | -- | 2, 5 | details |
Code
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
public class brute {
public static boolean isInPrison(int thief_prisonIndex) {
return thief_prisonIndex != -1;
}
public static boolean isOpenValid(int[] thieves, int thief, int tentativePrison) {
boolean prisonEmpty = true;
for (int i = 0; i < thieves.length; i++) {
if (thieves[i] == tentativePrison) {
prisonEmpty = false;
break;
}
}
if (prisonEmpty) {
return false;
}
return true;
}
public static class Event {
boolean type; // false = C, true = O
int thief;
int prison;
public Event(boolean type, int thief) {
this.type = type;
this.thief = thief;
this.prison = -1;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.next());
int k = Integer.parseInt(scanner.next());
int m = Integer.parseInt(scanner.next());
Event[] events = new Event[m];
for (int i = 0; i < m; i++) {
boolean type = scanner.next().contains("O");
int thief = Integer.parseInt(scanner.next());
events[i] = new Event(type, thief);
}
// backtracking algorithm
boolean possible = true;
int[] thieves = new int[n];
Arrays.fill(thieves, -1);
ArrayList<int[]> history = new ArrayList<>();
history.add(thieves);
int index = 0;
while (true) {
if (index >= events.length) {
break;
}
if (index < 0) {
possible = false;
break;
}
int[] curThieves = history.get(history.size() - 1);
Event curEvent = events[index];
curEvent.prison++;
if (curEvent.prison >= k || // we have exhausted all possible configurations
isInPrison(curThieves[curEvent.thief - 1])) { // thief is in prison, can't take an action
// backtrack
curEvent.prison = -1;
history.remove(history.size() - 1);
index--;
continue;
}
if (events[index].type == false) { // a thief has been caught
// catch the thief
int[] nextThieves = curThieves.clone();
nextThieves[curEvent.thief - 1] = curEvent.prison;
history.add(nextThieves);
index++;
} else { // a thief has opened a gate
if (isOpenValid(curThieves, curEvent.thief - 1, curEvent.prison)) {
int[] nextThieves = curThieves.clone();
// free all prisoners
for (int i = 0; i < nextThieves.length; i++) {
if (nextThieves[i] == curEvent.prison) {
nextThieves[i] = -1;
}
}
history.add(nextThieves);
index++;
continue;
} else {
// illegal action
continue;
}
}
}
if (possible) {
String outStr = "";
for (int i = 0; i < events.length; i++) {
outStr += (events[i].prison + 1) + " ";
// System.out.println(events[i].prison + 1);
}
System.out.println(outStr);
} else {
System.out.println("IMPOSSIBLE");
}
scanner.close();
}
}
Test details
Test 1
Group: 2, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1 1 1 C 1 |
| correct output |
|---|
| 1 |
| user output |
|---|
| 1 |
Test 2
Group: 2, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1 1 1 O 1 |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 3
Group: 2, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1 1 2 C 1 C 1 |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 4
Group: 2, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1 1 2 C 1 O 1 |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 5
Group: 2, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 1 1 2 O 1 C 1 |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 6
Group: 4, 5
Verdict: ACCEPTED
| input |
|---|
| 2 1 2 C 1 C 2 |
| correct output |
|---|
| 1 1 |
| user output |
|---|
| 1 1 |
Test 7
Group: 4, 5
Verdict: ACCEPTED
| input |
|---|
| 2 1 2 C 1 O 1 |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 8
Group: 4, 5
Verdict: ACCEPTED
| input |
|---|
| 2 1 2 C 1 O 2 |
| correct output |
|---|
| 1 1 |
| user output |
|---|
| 1 1 |
Test 9
Group: 1, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 3 2 5 C 1 C 2 O 3 C 1 ... |
| correct output |
|---|
| 1 1 1 1 1 |
| user output |
|---|
| 1 1 1 1 1 |
Test 10
Group: 1, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 3 2 5 C 1 C 2 O 3 O 3 ... |
| correct output |
|---|
| 2 1 2 1 1 |
| user output |
|---|
| 1 2 1 2 1 |
Test 11
Group: 1, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 3 2 5 C 1 C 2 O 3 O 1 ... |
| correct output |
|---|
| 2 1 2 1 1 |
| user output |
|---|
| 1 2 1 2 1 |
Test 12
Group: 1, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 3 2 5 C 1 C 2 O 1 O 3 ... |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 13
Group: 1, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 3 2 4 C 1 O 2 C 1 O 3 |
| correct output |
|---|
| 1 1 1 1 |
| user output |
|---|
| 1 1 1 1 |
Test 14
Group: 1, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 3 2 4 C 1 O 2 C 2 O 1 |
| correct output |
|---|
| 1 1 1 1 |
| user output |
|---|
| 1 1 1 1 |
Test 15
Group: 1, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 3 2 3 C 1 C 2 C 3 |
| correct output |
|---|
| 1 1 1 |
| user output |
|---|
| 1 1 1 |
Test 16
Group: 1, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 3 2 3 O 1 C 2 C 3 |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 17
Group: 1, 2, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 2 2 7 C 1 O 2 O 2 O 2 ... |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 18
Group: 1, 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 4 2 5 C 2 O 3 C 1 O 4 ... |
| correct output |
|---|
| 1 1 1 1 1 |
| user output |
|---|
| 1 1 1 1 1 |
Test 19
Group: 2, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 100000 100000 C 1 C 2 C 3 C 4 ... |
| correct output |
|---|
| 50000 49999 49998 49997 49996 ... |
| user output |
|---|
| (empty) |
Test 20
Group: 2, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 100000 100000 C 1 C 2 C 3 C 4 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| (empty) |
Test 21
Group: 2, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 100000 100000 C 1 C 2 C 3 C 4 ... |
| correct output |
|---|
| 20000 20000 20000 20000 20000 ... |
| user output |
|---|
| (empty) |
Test 22
Group: 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 100 100000 C 1 C 2 C 3 C 4 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| (empty) |
Test 23
Group: 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 99 100000 C 1 C 2 C 3 C 4 ... |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| (empty) |
Test 24
Group: 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 500 2 500 C 384 O 62 C 387 O 473 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... Truncated |
Test 25
Group: 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 500 2 500 C 384 O 62 C 387 O 473 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 ... |
| user output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 ... Truncated |
Test 26
Group: 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 500 2 500 C 384 O 62 C 387 O 473 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 ... |
| user output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 ... Truncated |
Test 27
Group: 3, 4, 5
Verdict: ACCEPTED
| input |
|---|
| 500 2 500 C 384 O 62 C 387 C 473 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... Truncated |
Test 28
Group: 4, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 500 250 500 C 384 O 62 C 387 O 473 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| (empty) |
Test 29
Group: 4, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 500 250 500 C 384 O 62 C 387 O 473 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 2 1 3 ... |
| user output |
|---|
| (empty) |
Test 30
Group: 4, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 500 250 500 C 384 O 62 C 387 O 473 ... |
| correct output |
|---|
| 1 1 1 1 1 3 2 3 3 2 2 2 5 4 2 ... |
| user output |
|---|
| (empty) |
Test 31
Group: 4, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 500 250 500 C 384 O 62 C 387 C 473 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| (empty) |
Test 32
Group: 2, 4, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 500 500 500 C 384 O 62 C 387 O 473 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| (empty) |
Test 33
Group: 2, 4, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 500 500 500 C 384 O 62 C 387 O 473 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 2 1 3 ... |
| user output |
|---|
| (empty) |
Test 34
Group: 2, 4, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 500 500 500 C 384 O 62 C 387 O 473 ... |
| correct output |
|---|
| 1 1 1 1 2 1 3 3 3 2 2 2 2 4 5 ... |
| user output |
|---|
| (empty) |
Test 35
Group: 2, 4, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 500 500 500 C 384 O 62 C 387 C 473 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| (empty) |
Test 36
Group: 3, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 2 100000 C 89384 O 54062 C 85387 O 53318 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| (empty) |
Test 37
Group: 3, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 2 100000 C 89384 O 54062 C 85387 O 53318 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 ... |
| user output |
|---|
| (empty) |
Test 38
Group: 3, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 2 100000 C 89384 O 54062 C 85387 O 53318 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 ... |
| user output |
|---|
| (empty) |
Test 39
Group: 3, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 2 100000 C 89384 O 54062 C 85387 C 53318 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| (empty) |
Test 40
Group: 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 50000 100000 C 89384 O 54062 C 85387 O 53318 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| (empty) |
Test 41
Group: 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 50000 100000 C 89384 O 54062 C 85387 O 53318 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 3 2 1 ... |
| user output |
|---|
| (empty) |
Test 42
Group: 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 50000 100000 C 89384 O 54062 C 85387 O 53318 ... |
| correct output |
|---|
| 1 1 1 1 1 3 2 3 3 3 3 3 3 4 5 ... |
| user output |
|---|
| (empty) |
Test 43
Group: 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 50000 100000 C 89384 O 54062 C 85387 C 53318 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| (empty) |
Test 44
Group: 2, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 100000 100000 C 89384 O 54062 C 85387 O 53318 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| (empty) |
Test 45
Group: 2, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 100000 100000 C 89384 O 54062 C 85387 O 53318 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 3 2 1 ... |
| user output |
|---|
| (empty) |
Test 46
Group: 2, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 100000 100000 C 89384 O 54062 C 85387 O 53318 ... |
| correct output |
|---|
| 1 1 1 1 2 1 3 3 3 3 3 3 4 5 3 ... |
| user output |
|---|
| (empty) |
Test 47
Group: 2, 5
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 100000 100000 C 89384 O 54062 C 85387 C 53318 ... |
| correct output |
|---|
| 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| (empty) |
