-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParentingParterningReturns.java
executable file
·146 lines (120 loc) · 4.14 KB
/
ParentingParterningReturns.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd27/000000000020bdf9?show=progress
import java.util.*;
enum Person {
Cameron,
Jamie,
None
}
class Activity {
int startTime;
int endTime;
int index;
char assignment;
public Activity(int s, int e, int i) {
startTime = s;
endTime = e;
index = i;
}
public static boolean isOverlapping(Activity a1, Activity a2) {
return (a1.endTime > a2.startTime);
}
public static int getIndex(char person) {
if (person == 'C') {
return 0;
} else if (person == 'J') {
return 1;
} else {
return -1;
}
}
public static char getOther(char c) {
if (c == 'C') {
return 'J';
} else if (c == 'J') {
return 'C';
} else {
System.out.println("Invalid person char");
return '0';
}
}
}
class ActivityTimeComparator implements Comparator<Activity> {
public int compare(Activity a1, Activity a2) {
if (a1.startTime < a2.startTime) {
return -1;
} else if (a1.startTime > a2.startTime) {
return 1;
} else {
return 0;
}
}
}
class ActivityIndexComparator implements Comparator<Activity> {
public int compare(Activity a1, Activity a2) {
if (a1.index < a2.index) {
return -1;
} else if (a1.index > a2.index) {
return 1;
} else {
return 0;
}
}
}
public class Solution{
private static boolean assignment(Activity[] activities, int N, char assignment[]){
int endTime[] = {0 , 0};
char currentPerson = 'C';
assignment[0] = currentPerson;
endTime[Activity.getIndex(currentPerson)] = activities[0].endTime;
activities[0].assignment = currentPerson;
for (int i = 1; i < N; ++i) {
char previous = assignment[i - 1];
// not overlapping with previous assign same person
if (!Activity.isOverlapping(activities[i - 1], activities[i])) {
assignment[i] = previous;
endTime[Activity.getIndex(previous)] = activities[i].endTime;
activities[i].assignment = previous;
} else {
// if overlappig with previous
char other = Activity.getOther(previous);
if (endTime[Activity.getIndex(other)] <= activities[i].startTime) {
assignment[i] = other;
endTime[Activity.getIndex(other)] = activities[i].endTime;
activities[i].assignment = other;
} else {
return false;
}
}
}
return true;
}
private static void assignment(Activity[] activities, int N, int t) {
Arrays.sort(activities, new ActivityTimeComparator());
char assignment[] = new char[N];
boolean isValid = assignment(activities, N, assignment);
System.out.print("Case #" + t + ": ");
if (!isValid) {
System.out.println("IMPOSSIBLE");
return;
}
Arrays.sort(activities, new ActivityIndexComparator());
for (Activity a : activities) {
System.out.print(a.assignment);
}
System.out.println("");
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 1; i <= t; ++i) {
int n = sc.nextInt();
Activity activities[] = new Activity[n];
for (int j = 0; j < n; ++j) {
int start = sc.nextInt();
int end = sc.nextInt();
activities[j] = new Activity(start, end, j);
}
assignment(activities, n, i);
}
}
}