-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
133 lines (129 loc) · 5.42 KB
/
Main.java
File metadata and controls
133 lines (129 loc) · 5.42 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
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
import java.io.*;
import java.util.*;
public class Main {
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<>();
public static class CustomComparator implements Comparator<Library> {
@Override
public int compare(Library o1, Library o2) {
return o1.getTotalScore().compareTo(o2.getTotalScore());
}
}
public static class CompareToBookCount implements Comparator<Library> {
@Override
public int compare(Library o1, Library o2) {
return o1.getBookCount().compareTo(o2.getBookCount());
}
}
public static class CustomComparator2 implements Comparator<Library> {
@Override
public int compare(Library o1, Library o2) {
return o1.getSignUp().compareTo(o2.getSignUp());
}
}
public static class CustomComparator3 implements Comparator<Library> {
@Override
public int compare(Library o1, Library o2) {
return o1.getPerDay().compareTo(o2.getPerDay());
}
}
@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);
// System.out.println(myReader.nextInt());
book = myReader.nextInt();
// System.out.println(myReader.nextInt());
// System.out.println(myReader.nextInt());
// System.out.println();
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);;
libraries.add(library);
}
List<Library> libraries2 = libraries;
// Comparator<Library> comparator =
// Comparator.comparing(Library::getMaxDays).thenComparing(Library::getSignUp)
// .thenComparing(Library::getPerDay).reversed().
// thenComparing(Library::getTotalScore).reversed();
Comparator<Library> libraryComparator = Comparator.
comparingInt(Library::getTotalScore).reversed().thenComparingInt(Library::getMaxDays);
// Collections.sort(libraries2, new CompareToBookCount().reversed());
// Collections.sort(libraries2, new CustomComparator().reversed());
// Collections.sort(libraries2, new CustomComparator2());
// Collections.sort(libraries2, new CustomComparator3());
Collections.sort(libraries2, libraryComparator);
// System.out.println(library);
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();
}
}
}