-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF - Find great mentors.java
265 lines (226 loc) · 8.32 KB
/
F - Find great mentors.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import java.sql.Connection;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(new FileReader(args[0] + ".in.txt"));
PrintWriter pw = new PrintWriter( new File(args[0] + ".out.txt"));
int number_of_projects=0;
StringBuilder ans=new StringBuilder();
int c=sc.nextInt();
int p=sc.nextInt();
Contributer[]con=new Contributer[c];
for(int i=0;i<c;i++){
String name=sc.next();
con[i]=new Contributer(name);
int n=sc.nextInt();
for(int j=0;j<n;j++){
String skilName=sc.next();
int v=sc.nextInt();
con[i].addSkill(skilName,v);
}
}
project[]pro=new project[p];
for(int i=0;i<p;i++){
String name=sc.next();
int d=sc.nextInt();
int s=sc.nextInt();
int b=sc.nextInt();
int r=sc.nextInt();
String[]na=new String[r];
int[]values=new int[r];
for(int j=0;j<r;j++){
na[j]=sc.next();
values[j]=sc.nextInt();
}
pro[i]=new project(name,d,s,b,r,na,values);
}
Arrays.sort(pro);
for(project x : pro) {
Contributer[] cons = x.Do(con);
if(cons!=null) {
number_of_projects ++;
ans.append(x.name+"\n");
for(Contributer y: cons) ans.append(y.name+" ");
ans.append("\n");
}
}
pw.println(number_of_projects);
pw.println(ans);
pw.close();
}
static void shuffle(project[] arr){
for (int i = 1; i < arr.length; i++) {
int j = (int) (Math.random() * (i + 1));
project temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
static class project implements Comparable<project> {
String name;
int days;
int score;
int deadline;
int num_roles;
String[]skills;
int[]levels;
int sqSumOfLevels;
public project(String name,int days,int score,int deadline,int num_roles,String[]skills,int[]levels) {
this.name = name;
this.days=days;
this.score=score;
this.deadline=deadline;
this.num_roles=num_roles;
this.skills=skills.clone();
this.levels=levels.clone();
sqSumOfLevels = 0;
for (int i = 0; i < num_roles; i++)
sqSumOfLevels += levels[i] * levels[i];
}
public Contributer[] Do(Contributer[]a) {
int n = a.length;
int max = 0;
boolean[] busy = new boolean[n];
Contributer[] ret = new Contributer[num_roles];
HashMap<String, Integer> hs = new HashMap<>();
for (int i = 0; i < num_roles; i++) {
int ind = -1;
for (int j = 0; j < n; j++) {
if (!busy[j] && ((a[j].canDoRole(skills[i], levels[i]) != null && a[j].canDoRole(skills[i], levels[i])) ||
(a[j].canDoRole(skills[i], levels[i]) == null && hs.getOrDefault(skills[i], 0) >= levels[i]))) {
if (ind == -1) {
ind = j;
} else if (a[j].busy_until < a[ind].busy_until) {
ind = j;
}
}
}
if (ind == -1) return null;
busy[ind] = true;
ret[i] = a[ind];
max = Math.max(max, a[ind].busy_until);
for (String s : a[ind].skills.keySet()) {
hs.put(s, Math.max(hs.getOrDefault(s, 0), a[ind].skills.get(s)));
}
// return null;
}
if (0 >= deadline + score - max - days) {
return null;
}
for (int i = 0; i < num_roles; i++) {
ret[i].busy_until = max + days;
ret[i].doesRole(skills[i], levels[i]);
}
// number_of_projects++;
return ret;
}
public int compareTo(project p){
double x = (1d/(score * 1d/days)) * deadline * Math.pow(sqSumOfLevels, 0.83);
double y = (1d / (p.score * 1d/p.days)) * p.deadline * Math.pow(p.sqSumOfLevels, 0.83);
return Double.compare(x, y);
}
}
static public class Contributer {
String name;
HashMap<String, Integer> skills;
int busy_until;
public Contributer(String name){
this.name = name;
skills = new HashMap<>();
busy_until=0;
}
public void addSkill(String skillName, int skillLevel) {
skills.put(skillName, skillLevel);
}
public void doesRole(String roleSkill, int skillLevel){
int skill = skills.getOrDefault(roleSkill, 0);
if(skill <= skillLevel)
skills.put(roleSkill, skill + 1);
}
public Boolean canDoRole(String roleSkill, int skillLevel){
int currSkillLevel = skills.getOrDefault(roleSkill, 0);
if(currSkillLevel >= skillLevel)
return true;
if(currSkillLevel == skillLevel - 1)
return null;
return false;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
}