5 * Solution for NEERC'2009 Problem B: Business Center
6 * This solution checks correctness of the input.
7 * @author Roman Elizarov
9 public class business_re
{
11 public static void main(String
[] args
) throws Exception
{
12 new business_re().go();
15 void go() throws Exception
{
26 void read() throws Exception
{
27 Scanner in
= new Scanner(new File("business.in"));
28 in
.useLocale(Locale
.US
);
32 assert n
>= 1 && n
<= 1000000;
33 assert m
>= 1 && m
<= 2000;
36 for (int i
= 0; i
< m
; i
++) {
40 assert u
[i
] >= 1 && u
[i
] <= 1000;
41 assert d
[i
] >= 1 && d
[i
] <= 1000;
46 int lowest
= Integer
.MAX_VALUE
;
49 for (int i
= 0; i
< m
; i
++) {
50 // min { a * u - (n - a) * d }
51 // a * (u + d) = n * d
52 // a = n * d / (u + d)
53 int a
= n
* d
[i
] / (u
[i
] + d
[i
]) + 1;
54 int res
= a
* u
[i
] - (n
- a
) * d
[i
];
55 lowest
= Math
.min(lowest
, res
);
59 void write() throws Exception
{
60 PrintWriter out
= new PrintWriter("business.out");
65 //----------------- just for validation ------------------
68 * Strict scanner to veryfy 100% correspondence between input files and input file format specification.
69 * It is a drop-in replacement for {@link java.util.Scanner} that could be added to a soulution source
70 * (cut-and-paste) without breaking its ability to work with {@link java.util.Scanner}.
72 public class Scanner
{
73 private final BufferedReader in
;
74 private String line
= "";
77 private boolean localeset
;
79 public Scanner(File source
) throws FileNotFoundException
{
80 in
= new BufferedReader(new FileReader(source
));
85 assert line
== null : "Extra data at the end of file";
88 } catch (IOException e
) {
89 throw new AssertionError("Failed to close with " + e
);
93 public void nextLine() {
94 assert line
!= null : "EOF";
95 assert pos
== line
.length() : "Extra characters on line " + lineNo
;
98 } catch (IOException e
) {
99 throw new AssertionError("Failed to read line with " + e
);
105 public String
next() {
106 assert line
!= null : "EOF";
107 assert line
.length() > 0 : "Empty line " + lineNo
;
109 assert line
.charAt(0) > ' ' : "Line " + lineNo
+ " starts with whitespace";
111 assert pos
< line
.length() : "Line " + lineNo
+ " is over";
112 assert line
.charAt(pos
) == ' ' : "Wrong whitespace on line " + lineNo
;
114 assert pos
< line
.length() : "Line " + lineNo
+ " is over";
115 assert line
.charAt(0) > ' ' : "Line " + lineNo
+ " has double whitespace";
117 StringBuilder sb
= new StringBuilder();
118 while (pos
< line
.length() && line
.charAt(pos
) > ' ')
119 sb
.append(line
.charAt(pos
++));
120 return sb
.toString();
123 public int nextInt() {
125 assert s
.length() == 1 || s
.charAt(0) != '0' : "Extra leading zero in number " + s
+ " on line " + lineNo
;
126 assert s
.charAt(0) != '+' : "Extra leading '+' in number " + s
+ " on line " + lineNo
;
128 return Integer
.parseInt(s
);
129 } catch (NumberFormatException e
) {
130 throw new AssertionError("Malformed number " + s
+ " on line " + lineNo
);
134 public double nextDouble() {
135 assert localeset
: "Locale must be set with useLocale(Locale.US)";
137 assert s
.length() == 1 || s
.startsWith("0.") || s
.charAt(0) != '0' : "Extra leading zero in number " + s
+ " on line " + lineNo
;
138 assert s
.charAt(0) != '+' : "Extra leading '+' in number " + s
+ " on line " + lineNo
;
140 return Double
.parseDouble(s
);
141 } catch (NumberFormatException e
) {
142 throw new AssertionError("Malformed number " + s
+ " on line " + lineNo
);
146 public void useLocale(Locale locale
) {
147 assert locale
== Locale
.US
;