-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa 348 - Optimal Array Multiplication Sequence.Java
203 lines (181 loc) · 5.08 KB
/
UVa 348 - Optimal Array Multiplication Sequence.Java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.math.BigInteger;
/*
-> Written by <-
-----------
|J_O_B_E_E_L|
|___________|
| ___ |
| (^_^) |
| /( | )\ |
|____|_|____|
*/
public class Main{
static int n;
static int [] row,col;
static long [][] memo;
static StringBuilder ans;
static long dp(int i,int j) {
if(i==j) return 0;
if(memo[i][j]!=-1) return memo[i][j];
long min=(long)1e9;
for(int k=i;k<j;k++)
min=Math.min(min, dp(i, k)+dp(k+1, j)+row[i]*col[j]*col[k]);
return memo[i][j]=min;
}
static void trace(int i,int j) {
if(i==j) { ans.append("A"+(i+1)); return;}
for(int k=i;k<j;k++)
if(dp(i, j)==dp(i, k)+dp(k+1, j)+row[i]*col[j]*col[k]) {
ans.append("(");
trace(i, k);
ans.append(" x ");
trace(k+1, j);
ans.append(")");
break;
}
}
public static void main(String[] args) throws IOException {
n=sc.nextInt();
int t=1;
while(n!=0) {
row=new int [n];
col=new int [n];
for(int i=0;i<n;i++) {row[i]=sc.nextInt(); col[i]=sc.nextInt();}
memo=new long [n+1][n+1];
for(long [] a:memo) Arrays.fill(a,-1);
ans=new StringBuilder();
//pw.println(dp(0, n-1));
trace(0, n-1);
pw.print("Case "+(t++)+": ");
pw.println(ans);
n=sc.nextInt();
}
pw.flush();
pw.close();
}
static class Scanner {
private InputStream in;
private byte[] buffer = new byte[1024];
private int curbuf;
private int lenbuf;
public Scanner(InputStream in) {
this.in = in;
this.curbuf = this.lenbuf = 0;
}
public boolean hasNextByte() {
if (curbuf >= lenbuf) {
curbuf = 0;
try {
lenbuf = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return false;
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[curbuf++];
else
return -1;
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private void skip() {
while (hasNextByte() && isSpaceChar(buffer[curbuf]))
curbuf++;
}
public boolean hasNext() {
skip();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (!isSpaceChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer [] sortedIntArray(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
Arrays.sort(a);
return a;
}
public Long [] sortedLongArray(int n) {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
Arrays.sort(a);
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
}