-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookScanning.java
211 lines (180 loc) · 5.73 KB
/
BookScanning.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
import java.util.TreeMap;
import org.apache.commons.io.IOUtils;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class BookScanning{
List<Integer> bookScoreList;
TreeMap<Integer, Library> libraries;
TreeMap<Integer, String> bl;
List<String> booksScanned;
int B, L, D; /* here B = total number of books, L = total number of libraries, D = available days */
int tBooks, tDays, tBooksPerDay;
void getData(String filepath) throws FileNotFoundException{
libraries = new TreeMap<Integer, Library>(Collections.reverseOrder());
File file = new File(filepath);
//BufferedReader br = new BufferedReader(new FileReader(file));
try {
FileInputStream fisTargetFile = new FileInputStream(new File(filepath));
String targetFileStr = IOUtils.toString(fisTargetFile, "UTF-8");
//System.out.println("filedata : " + targetFileStr);
String[] fileData = targetFileStr.split("\n");
int line = 0;
String[] params = fileData[line].split(" ");
//System.out.println("params :: " + params.toString());
B = Integer.parseInt(params[0]);
L = Integer.parseInt(params[1]);
D = Integer.parseInt(params[2]);
line++;
params = fileData[line].split(" ");
bookScoreList = new ArrayList<Integer>();
for(String s : params){
bookScoreList.add(Integer.parseInt(s));
}
int i = 0;
int libid = 0;
while(i < L){
line++;
params =fileData[line].split(" ");
tBooks = Integer.parseInt(params[0]);
tDays = Integer.parseInt(params[1]);
tBooksPerDay = Integer.parseInt(params[2]);
line++;
params =fileData[line].split(" ");
bl = new TreeMap<Integer,String>(Collections.reverseOrder());
for(String s : params){
int x = Integer.parseInt(s);
bl.put(bookScoreList.get(x), s);
}
Library library = new Library(libid, tBooks, tDays, tBooksPerDay, bl);
libraries.put(tDays, library);
libid++;
i++;
}
} catch (Exception e) {
System.out.println("EXCEPTION : " + e.getMessage());
e.printStackTrace();
}
try {
}
finally {
//sc.close();
}
}
void GenerateOutput(){
//System.out.println("libraries = " + libraries.toString());
TreeMap<Integer, Library> result = new TreeMap<Integer, Library>(Collections.reverseOrder());
booksScanned = new ArrayList<String>();
int day = 0;
for(Map.Entry<Integer, Library> entry : libraries.entrySet() ) {
if(day > D)
break;
Library lib = (Library) entry.getValue();
day += lib.getDaysRequiredForSignup();
//int days_required = lib.getMaxBooksPerDay() * (D - day);
result.put(entry.getKey(), lib);
}
System.out.println(result.size());
day = 0;
for(Map.Entry<Integer, Library> entry : result.entrySet() ) {
//System.out.println("day = " + day);
if(day >= D)
break;
List<String> booksToBeScanned = new ArrayList<String>();
Library lib = (Library) entry.getValue();
day += lib.getDaysRequiredForSignup();
int days_required = lib.getMaxBooksPerDay() * (D - day);
TreeMap<Integer, String> books = lib.getBookList();
int s = books.size();
int i=0;
for(Map.Entry<Integer, String> en : books.entrySet()) {
//if(booksScanned.contains(en.getValue()))
//continue;
booksToBeScanned.add(en.getValue());
booksScanned.add(en.getValue());
i++;
}
System.out.println(lib.getLibrryId() + " " + booksToBeScanned.size());
for(i=0, s = booksToBeScanned.size(); i < s; i++) {
if(i == s - 1)
System.out.print(booksToBeScanned.get(i) + "\n");
else
System.out.print(booksToBeScanned.get(i) + " ");
}
//System.out.println("day = " + day);
}
}
// driver program
public static void main(String[] args){
String filepath = "E://hashcode//testecases";
String filename = "b_read_on.txt";
BookScanning bs = new BookScanning();
try {
bs.getData( filepath + File.separator + filename);
bs.GenerateOutput();
} catch (Exception e) {
System.out.println("EXCEPTION IN MAIN");
e.printStackTrace();
}
}
}
class Library{
int librryId, totalBooks, daysRequiredForSignup, maxBooksPerDay;
TreeMap<Integer, String> bookList;
public Library() {
super();
}
@Override
public String toString() {
return "Library [librryId=" + librryId + ", totalBooks=" + totalBooks + ", daysRequiredForSignup="
+ daysRequiredForSignup + ", maxBooksPerDay=" + maxBooksPerDay + ", bookList=" + bookList + "]";
}
public Library(int librryId, int totalBooks, int daysRequiredForSignup, int maxBooksPerDay,
TreeMap<Integer, String> bookList) {
super();
this.librryId = librryId;
this.totalBooks = totalBooks;
this.daysRequiredForSignup = daysRequiredForSignup;
this.maxBooksPerDay = maxBooksPerDay;
this.bookList = bookList;
}
public int getLibrryId() {
return librryId;
}
public void setLibrryId(int librryId) {
this.librryId = librryId;
}
public int getTotalBooks() {
return totalBooks;
}
public void setTotalBooks(int totalBooks) {
this.totalBooks = totalBooks;
}
public int getDaysRequiredForSignup() {
return daysRequiredForSignup;
}
public void setDaysRequiredForSignup(int daysRequiredForSignup) {
this.daysRequiredForSignup = daysRequiredForSignup;
}
public int getMaxBooksPerDay() {
return maxBooksPerDay;
}
public void setMaxBooksPerDay(int maxBooksPerDay) {
this.maxBooksPerDay = maxBooksPerDay;
}
public TreeMap<Integer, String> getBookList() {
return bookList;
}
public void setBookList(TreeMap<Integer, String> bookList) {
this.bookList = bookList;
}
}