-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathGoogle_Hash_Code_2020.java
More file actions
105 lines (74 loc) · 2.71 KB
/
Copy pathGoogle_Hash_Code_2020.java
File metadata and controls
105 lines (74 loc) · 2.71 KB
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
import java.util.*;
// Class representing a Library
class Library {
int id;
int books;
int signup;
int booksPerDay;
ArrayList<Integer> bookIDs;
Library(int id, int books, int signup, int booksPerDay, ArrayList<Integer> bookIDs) {
this.id = id;
this.books = books;
this.signup = signup;
this.booksPerDay = booksPerDay;
this.bookIDs = bookIDs;
}
}
// Comparator for sorting libraries by signup time (greedy heuristic)
class SortBySignup implements Comparator<Library> {
public int compare(Library a, Library b) {
return a.signup - b.signup;
}
}
public class hc {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int totalBooks = sc.nextInt();
int numOfLib = sc.nextInt();
int totalDays = sc.nextInt();
int[] bookScore = new int[totalBooks];
for (int i = 0; i < totalBooks; i++) {
bookScore[i] = sc.nextInt();
}
// Store libraries
HashMap<Integer, Library> map = new HashMap<>();
for (int i = 0; i < numOfLib; i++) {
int books = sc.nextInt();
int signup = sc.nextInt();
int bpd = sc.nextInt();
ArrayList<Integer> bid = new ArrayList<>();
for (int j = 0; j < books; j++) {
bid.add(sc.nextInt());
}
map.put(i, new Library(i, books, signup, bpd, bid));
}
// Convert to list for sorting
ArrayList<Library> libs = new ArrayList<>(map.values());
libs.sort(new SortBySignup());
// Result storage
ArrayList<Integer> selectedLibs = new ArrayList<>();
int remainingDays = totalDays;
for (Library lib : libs) {
if (remainingDays <= lib.signup) break;
remainingDays -= lib.signup;
int shipCapacity = remainingDays * lib.booksPerDay;
if (shipCapacity > 0) {
selectedLibs.add(lib.id);
}
}
// Output
System.out.println(selectedLibs.size());
for (int id : selectedLibs) {
Library lib = map.get(id);
int remainingDaysAfterSignup = totalDays - lib.signup;
int shipCapacity = remainingDaysAfterSignup * lib.booksPerDay;
shipCapacity = Math.min(shipCapacity, lib.bookIDs.size());
System.out.println(id + " " + shipCapacity);
for (int k = 0; k < shipCapacity; k++) {
System.out.print(lib.bookIDs.get(k) + " ");
}
System.out.println();
}
sc.close();
}
}