5 * Solution for NEERC'2009 Problem J: Java Certification
6 * This solution checks correctness of the input.
7 * @author Roman Elizarov
9 public class javacert_re
{
11 public static void main(String
[] args
) throws Exception
{
12 new javacert_re().go();
15 void go() throws Exception
{
26 void read() throws Exception
{
27 Scanner in
= new Scanner(new File("javacert.in"));
28 in
.useLocale(Locale
.US
);
33 assert k
>= 0 && k
<= n
;
34 assert n
>= 1 && n
<= 100;
35 assert m
>= 1 && m
<= 10;
37 for (int i
= 0; i
< m
; i
++) {
40 assert p
[i
] >= 0 && p
[i
] <= 100;
49 Pair(int wi
, int ni
) {
55 public String
toString() {
56 return wi
+ " " + ni
+ " " + (100.0*(ni
- wi
) / ni
);
65 int bestdiff
= Integer
.MAX_VALUE
;
71 dp
= new int[m
+ 1][w
+ 1][n
+ 1];
72 lp
= new Pair
[m
+ 1][w
+ 1][n
+ 1];
73 for (int minni
= 0; minni
<= n
; minni
++) {
74 for (int i
= 0; i
<= m
; i
++) {
75 for (int wi
= 0; wi
<= w
; wi
++) {
76 Arrays
.fill(dp
[i
][wi
], Integer
.MAX_VALUE
);
77 Arrays
.fill(lp
[i
][wi
], null);
81 for (int i
= 0; i
< m
; i
++) {
82 for (Pair p
: llp
.get(i
))
84 for (int ni
= 0; ni
<= n
; ni
++) {
87 for (int wi
= 0; wi
<= w
; wi
++) {
90 if (dp
[i
][wi
][ni
] < Integer
.MAX_VALUE
) {
91 int maxni
= Math
.max(dp
[i
][wi
][ni
], p
.ni
);
92 if (maxni
< dp
[i
+ 1][wj
][nj
]) {
93 dp
[i
+ 1][wj
][nj
] = maxni
;
94 lp
[i
+ 1][wj
][nj
] = p
;
100 int maxni
= dp
[m
][w
][n
];
101 if (maxni
== Integer
.MAX_VALUE
)
103 int diff
= maxni
- minni
;
104 if (diff
< bestdiff
) {
108 for (int i
= m
; --i
>= 0;) {
109 sol
[i
] = lp
[i
+ 1][ws
][ns
];
120 private void buildPairs() {
121 llp
= new ArrayList
<List
<Pair
>>();
122 for (int i
= 0; i
< m
; i
++) {
123 llp
.add(new ArrayList
<Pair
>());
125 for (int ni
= 1; ni
<= n
; ni
++) {
126 for (int wi
= 0; wi
<= ni
; wi
++) {
127 int ppp
= 100 * (ni
- wi
);
128 int pp
= (2 * ppp
+ ni
) / (2 * ni
);
129 if (2 * ppp
% ni
== 0 && 2 * ppp
/ ni
% 2 == 1 && pp
% 2 == 1)
131 for (int i
= 0; i
< m
; i
++) {
133 llp
.get(i
).add(new Pair(wi
, ni
));
139 void write() throws Exception
{
140 PrintWriter out
= new PrintWriter("javacert.out");
141 for (int i
= 0; i
< m
; i
++) {
142 out
.printf("%d %d%n", sol
[i
].wi
, sol
[i
].ni
);
147 //----------------- just for validation ------------------
150 * Strict scanner to veryfy 100% correspondence between input files and input file format specification.
151 * It is a drop-in replacement for {@link java.util.Scanner} that could be added to a soulution source
152 * (cut-and-paste) without breaking its ability to work with {@link java.util.Scanner}.
154 public class Scanner
{
155 private final BufferedReader in
;
156 private String line
= "";
159 private boolean localeset
;
161 public Scanner(File source
) throws FileNotFoundException
{
162 in
= new BufferedReader(new FileReader(source
));
166 public void close() {
167 assert line
== null : "Extra data at the end of file";
170 } catch (IOException e
) {
171 throw new AssertionError("Failed to close with " + e
);
175 public void nextLine() {
176 assert line
!= null : "EOF";
177 assert pos
== line
.length() : "Extra characters on line " + lineNo
;
179 line
= in
.readLine();
180 } catch (IOException e
) {
181 throw new AssertionError("Failed to read line with " + e
);
187 public String
next() {
188 assert line
!= null : "EOF";
189 assert line
.length() > 0 : "Empty line " + lineNo
;
191 assert line
.charAt(0) > ' ' : "Line " + lineNo
+ " starts with whitespace";
193 assert pos
< line
.length() : "Line " + lineNo
+ " is over";
194 assert line
.charAt(pos
) == ' ' : "Wrong whitespace on line " + lineNo
;
196 assert pos
< line
.length() : "Line " + lineNo
+ " is over";
197 assert line
.charAt(0) > ' ' : "Line " + lineNo
+ " has double whitespace";
199 StringBuilder sb
= new StringBuilder();
200 while (pos
< line
.length() && line
.charAt(pos
) > ' ')
201 sb
.append(line
.charAt(pos
++));
202 return sb
.toString();
205 public int nextInt() {
207 assert s
.length() == 1 || s
.charAt(0) != '0' : "Extra leading zero in number " + s
+ " on line " + lineNo
;
208 assert s
.charAt(0) != '+' : "Extra leading '+' in number " + s
+ " on line " + lineNo
;
210 return Integer
.parseInt(s
);
211 } catch (NumberFormatException e
) {
212 throw new AssertionError("Malformed number " + s
+ " on line " + lineNo
);
216 public double nextDouble() {
217 assert localeset
: "Locale must be set with useLocale(Locale.US)";
219 assert s
.length() == 1 || s
.startsWith("0.") || s
.charAt(0) != '0' : "Extra leading zero in number " + s
+ " on line " + lineNo
;
220 assert s
.charAt(0) != '+' : "Extra leading '+' in number " + s
+ " on line " + lineNo
;
222 return Double
.parseDouble(s
);
223 } catch (NumberFormatException e
) {
224 throw new AssertionError("Malformed number " + s
+ " on line " + lineNo
);
228 public void useLocale(Locale locale
) {
229 assert locale
== Locale
.US
;