-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain2.java
More file actions
99 lines (95 loc) · 3.86 KB
/
Main2.java
File metadata and controls
99 lines (95 loc) · 3.86 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
import java.io.*;
import java.util.*;
public class Main2 {
public static Integer book;
public static Integer library;
public static Integer days;
public static List<Book> scores = new ArrayList<>();
public static List<Library> libraries = new ArrayList<>();
@SuppressWarnings("Duplicates")
public static void main(String[] args) {
try {
File myObj = new File("./src/in.txt");
BufferedWriter br = new BufferedWriter(new FileWriter(new File("./src/out.txt")));
Scanner myReader = new Scanner(myObj);
book = myReader.nextInt();
library = myReader.nextInt();
days = myReader.nextInt();
for (int i = 0; i < book; i++) {
Book book = new Book();
book.setId(i);
book.setScore(myReader.nextInt());
scores.add(book);
}
for (int i = 0; i < library; i++) {
Library library = new Library();
library.setID(i);
int bookcount= myReader.nextInt();
library.setBookCount(bookcount);
library.setSignUp(myReader.nextInt());
library.setPerDay(myReader.nextInt());
List<Book> a = new ArrayList<>();
for (int j = 0; j < library.getBookCount(); j++) {
int tm2 = myReader.nextInt();
Book temp = new Book();
temp.setScore(scores.get(tm2).getScore());
temp.setId(tm2);
a.add(temp);
}
Collections.sort(a, new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
return o1.getScore().compareTo(o2.getScore());
}
}.reversed());
library.setBooks(a);
library.setMaxDays(days);
library.setTotalScore(scores,days);
library.setPerSignupDay();
libraries.add(library);
}
List<Library> libraries2 = libraries;
Comparator<Library> libraryComparator = Comparator.comparingInt(Library::getTotalScore).reversed();
libraries2.sort(libraryComparator);
List<Book> books2 = scores;
Comparator<Book> bookComparator = Comparator.comparingInt(Book::getScore).reversed();
books2.sort(bookComparator);
int days2 = days;
int maxlib = 0;
for (int i = 0; i <library ; i++) {
Library temp = libraries2.get(i);
days2-=temp.getSignUp();
if (days2<=0){
maxlib = i;
break;
}
}
br.write(maxlib + "\n");
for (int i = 0; i <maxlib ; i++) {
Library temp = libraries2.get(i);
if (temp.getSignUp()<days){
// System.out.print(temp.getID() + " ");
br.write(temp.getID() + " ");
br.write(temp.getBookCount() + " \n");
// System.out.print(temp.getBookCount() + " \n");
List<Book> aa = temp.getBooks();
for (int j = 0; j <temp.getBookCount() ; j++) {
// System.out.print(aa.get(j)+ " ");
br.write(aa.get(j).getId()+ " ");
}
// System.out.println();
if (i+1 != maxlib){
br.write('\n');
}
}
}
myReader.close();
br.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}