Skip to content

Commit e516936

Browse files
authored
Merge pull request ashutosh97#141 from rishabhdeepsingh/fun
Added Chocolate Spoj Problem
2 parents 6b159e4 + f37b68b commit e516936

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

Chocolate_SPOJ/question.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
We are given a bar of chocolate composed of m*n square pieces. One should break the chocolate into single squares. Parts of the chocolate may be broken along the vertical and horizontal lines as indicated by the broken lines in the picture.
2+
3+
A single break of a part of the chocolate along a chosen vertical or horizontal line divides that part into two smaller ones. Each break of a part of the chocolate is charged a cost expressed by a positive integer. This cost does not depend on the size of the part that is being broken but only depends on the line the break goes along. Let us denote the costs of breaking along consecutive vertical lines with x1, x2, ..., xm-1 and along horizontal lines with y1, y2, ..., yn-1.
4+
5+
The cost of breaking the whole bar into single squares is the sum of the successive breaks. One should compute the minimal cost of breaking the whole chocolate into single squares.
6+
7+
8+
For example, if we break the chocolate presented in the picture first along the horizontal lines, and next each obtained part along vertical lines then the cost of that breaking will be y1+y2+y3+4*(x1+x2+x3+x4+x5).
9+
10+
Task
11+
Write a program that for each test case:
12+
13+
Reads the numbers x1, x2, ..., xm-1 and y1, y2, ..., yn-1
14+
Computes the minimal cost of breaking the whole chocolate into single squares, writes the result.
15+
Input
16+
One integer in the first line, stating the number of test cases, followed by a blank line. There will be not more than 20 tests.
17+
18+
For each test case, at the first line there are two positive integers m and n separated by a single space, 2 <= m,n <= 1000. In the successive m-1 lines there are numbers x1, x2, ..., xm-1, one per line, 1 <= xi <= 1000. In the successive n-1 lines there are numbers y1, y2, ..., yn-1, one per line, 1 <= yi <= 1000.
19+
20+
The test cases will be separated by a single blank line.
21+
22+
Output
23+
For each test case : write one integer - the minimal cost of breaking the whole chocolate into single squares.
24+
25+
26+
Example
27+
Input:
28+
1
29+
30+
6 4
31+
2
32+
1
33+
3
34+
1
35+
4
36+
4
37+
1
38+
2
39+
40+
Output:
41+
42

Chocolate_SPOJ/solution.java

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import java.io.OutputStream;
2+
import java.io.IOException;
3+
import java.io.InputStream;
4+
import java.io.OutputStream;
5+
import java.io.PrintWriter;
6+
import java.util.Arrays;
7+
import java.io.BufferedWriter;
8+
import java.io.Writer;
9+
import java.io.OutputStreamWriter;
10+
import java.util.InputMismatchException;
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
14+
/**
15+
* Built using CHelper plug-in
16+
* Actual solution is at the top
17+
*
18+
* @author Rishabhdeep Singh
19+
*/
20+
public class Main {
21+
public static void main(String[] args) {
22+
InputStream inputStream = System.in;
23+
OutputStream outputStream = System.out;
24+
InputReader in = new InputReader(inputStream);
25+
OutputWriter out = new OutputWriter(outputStream);
26+
MultipleTestcases solver = new MultipleTestcases();
27+
int testCount = Integer.parseInt(in.next());
28+
for (int i = 1; i <= testCount; i++)
29+
solver.solve(i, in, out);
30+
out.close();
31+
}
32+
33+
static class MultipleTestcases {
34+
public void solve(int testNumber, InputReader in, OutputWriter out) {
35+
int n = in.nextInt();
36+
int m = in.nextInt();
37+
--n;
38+
--m;
39+
int[] x = in.nextIntArray(n);
40+
int[] y = in.nextIntArray(m);
41+
int s1 = 0, s2 = 0;
42+
for (int i = 0; i < n; i++) {
43+
s1 += x[i];
44+
}
45+
for (int i = 0; i < m; i++) {
46+
s2 += y[i];
47+
}
48+
Arrays.sort(x);
49+
Arrays.sort(y);
50+
int ans = s1 + s2;
51+
for (int i = n - 1, j = m - 1; i >= 0 && j >= 0; ) {
52+
if (y[j] >= x[i]) {
53+
ans += s1;
54+
s2 -= y[j];
55+
--j;
56+
} else {
57+
ans += s2;
58+
s1 -= x[i];
59+
--i;
60+
}
61+
}
62+
out.println(ans);
63+
}
64+
65+
}
66+
67+
static class OutputWriter {
68+
private final PrintWriter writer;
69+
70+
public OutputWriter(OutputStream outputStream) {
71+
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
72+
}
73+
74+
public OutputWriter(Writer writer) {
75+
this.writer = new PrintWriter(writer);
76+
}
77+
78+
public void close() {
79+
writer.close();
80+
}
81+
82+
public void println(int i) {
83+
writer.println(i);
84+
}
85+
86+
}
87+
88+
static class InputReader {
89+
private InputStream stream;
90+
private byte[] buf = new byte[1024];
91+
private int curChar;
92+
private int numChars;
93+
private InputReader.SpaceCharFilter filter;
94+
95+
public InputReader(InputStream stream) {
96+
this.stream = stream;
97+
}
98+
99+
public int read() {
100+
if (numChars == -1) {
101+
throw new InputMismatchException();
102+
}
103+
if (curChar >= numChars) {
104+
curChar = 0;
105+
try {
106+
numChars = stream.read(buf);
107+
} catch (IOException e) {
108+
throw new InputMismatchException();
109+
}
110+
if (numChars <= 0) {
111+
return -1;
112+
}
113+
}
114+
return buf[curChar++];
115+
}
116+
117+
public int nextInt() {
118+
int c = read();
119+
while (isSpaceChar(c)) {
120+
c = read();
121+
}
122+
int sgn = 1;
123+
if (c == '-') {
124+
sgn = -1;
125+
c = read();
126+
}
127+
int res = 0;
128+
do {
129+
if (c < '0' || c > '9') {
130+
throw new InputMismatchException();
131+
}
132+
res *= 10;
133+
res += c - '0';
134+
c = read();
135+
} while (!isSpaceChar(c));
136+
return res * sgn;
137+
}
138+
139+
public String nextString() {
140+
int c = read();
141+
while (isSpaceChar(c)) {
142+
c = read();
143+
}
144+
StringBuilder res = new StringBuilder();
145+
do {
146+
if (Character.isValidCodePoint(c)) {
147+
res.appendCodePoint(c);
148+
}
149+
c = read();
150+
} while (!isSpaceChar(c));
151+
return res.toString();
152+
}
153+
154+
public boolean isSpaceChar(int c) {
155+
if (filter != null) {
156+
return filter.isSpaceChar(c);
157+
}
158+
return isWhitespace(c);
159+
}
160+
161+
public static boolean isWhitespace(int c) {
162+
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
163+
}
164+
165+
public String next() {
166+
return nextString();
167+
}
168+
169+
public int[] nextIntArray(int n) {
170+
int[] array = new int[n];
171+
for (int i = 0; i < n; ++i) array[i] = nextInt();
172+
return array;
173+
}
174+
175+
public interface SpaceCharFilter {
176+
public boolean isSpaceChar(int ch);
177+
178+
}
179+
180+
}
181+
}
182+

0 commit comments

Comments
 (0)