-
Notifications
You must be signed in to change notification settings - Fork 0
/
dp.tex
executable file
·395 lines (350 loc) · 9.96 KB
/
dp.tex
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
\section{Dynamic Programming}
\subsection{LCS (Longest Common Subsequence)}
\begin{verbatim}
int L[MAX][MAX] = {{0}};
int LCS(char A[], char B[]) {
for (int i = m;i >= 0; i--) { /* m = strlen(A) */
for (int j = n;j >= 0; j--) { /* n = strlen(B) */
if (!A[i] || !B[j])
L[i][j] = 0;
else if (A[i] == B[j])
L[i][j] = 1 + L[i+1][j+1];
else L[i][j] = max(L[i+1][j], L[i][j+1]);
}
}
return L[0][0];
}
int LCSString(int L[MAX][MAX]) {
int i, j;
i = j = 0;
while (i < m && j < n) {
if (A[i] == B[j]) {
poe A[i] no fim da str-solucao
i++; j++;
}
if (L[i+1][j] >= L[i][j+1]) i++;
else j++;
}
}
\end{verbatim}
\subsection{LIS (Longest Increasing Subsequence)}
\begin{verbatim}
int pred[MAX_SIZE],lasti;
int LIS(int C[], int n) {
int s[MAX_SIZE], max=INT_MIN;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; i++) {
if (C[i] > C[j] && s[i] <= s[j]) {
pred[i] = j;
if ((s[i] = s[j] + 1) > max)
lasti = i;
max = s[i];
}
}
}
return max;
}
void PrintLIS() {
int i, j, aux[MAX_SIZE];
for (j = max-1, i = lasti; j >= 0; j--) {
aux[j] = C[i];
i = pred[i];
}
for (j = 0;j < max; j++)
printf(``%d\n'', aux[j]);
}
\end{verbatim}
\subsection{MCM (Matrix Chain Multiplication)}
\begin{verbatim}
int Calc(int i, int j) {
int res = INT_MAX;
for (k = i;k < j; k++) {
tmp = m[i][k] + m[k+1][j]+
Line[i] * Col[k] * Col[j];
if (tmp < res) {
res = tmp;
s[i][j] = k;
}
}
return res;
}
void MCM() {
int i, j, n = 3;
for (i = 0;i < n; i++)
m[i][i] = 0;
for (i = n-1; i >= 0; i--)
for (j = i + 1; j <= n; j++)
m[i][j] = Calc(i, j);
}
//PrintMCM(0,N-1);
void PrintMCM(int i, int j) {
if (i == j) printf("A%d",i);
else {
putchar('(');
PrintMCM(i, s[i][j]);
putchar('*');
PrintMCM(s[i][j] + 1, j);
putchar(')');
}
}
\end{verbatim}
\subsection{Knapsack}
\begin{verbatim}
int n[WSIZE][ISIZE] = {{0}}
/*
* put one zero in weight and value;
* ex: weight={>0<,3,4,5} & value={>0<,3,4,5,6};
*/
int knapsack(int items, int W,
int value[], int weight[]){
for (int i = 1;i <= items; i++) {
for (int j = 0; j <= W; j++) {
if (weight[i] <= j) {
if (value[i] + n[i-1][j-weight[i]]
> n[i-1][j])
n[i][j] = value[i] +
n[i-1][j-weight[i]];
else n[i][j]=n[i-1][j];
} else n[i][j]=n[i-1][j];
}
}
return n[items][W];
}
void print_sequence(int items, int W, int weight[]) {
int i = items, k = W;
while (i > 0 && k > 0) {
if (n[i][k] != n[i-1][k]) {
printf("item %d is in\n", i);
k = k-weight[i-1];
}
i--;
}
}
\end{verbatim}
\subsection{Counting Change}
De quantas maneiras podemos obter um dado valor $N$, usando somente moedas com determinados valores.
\begin{verbatim}
int coins[] = {50,25,10,5,1};
int CoinChange(int n) {
table[0] = 1;
for (i = 0; i < 5; i++) {
c = coins[i];
for (j = c; j <= n; j++)
table[j] += table[j - c];
}
return table[n];
}
\end{verbatim}
\subsection{Coin Changing}
Dada uma lista de $N$ moedas e seus valores $(V_1,V_2,\ldots, V_n)$ e uma soma total $S$, determine o menor número de moedas da lista cuja soma seja $S$.
\begin{verbatim}
int n[10000],i,N, coins[]={50,25,10,5,1},k;
int main() {
scanf("%d", &N);
for (int i = 0; i <= N; i++) n[i] = INT_MAX;
n[0] = 0;
for (int i = 0; i < 5; i++)
for (k = 0;k <= N - coins[i]; k++)
n[k + coins[i]] =
min(n[k] + 1, n[k + coins[i]]);
printf("%d\n", n[N]);
return 0;
}
\end{verbatim}
\subsection{Biggest Sum}
\begin{verbatim}
#define SIZE 20000
int n[SIZE];
int main() {
int k, s, b;
int xl, xr, best, prevx;
cin>>k;
for (int i = 1; i <= k; i++) {
xr = xl = 0;
cin >> s;
for (int j = 0; j < s - 1; j++)
cin >> n[j];
prevx = xl = xr = 0;
best = b = n[0];
for (int j = 1; j < s - 1; j++) {
if (b < 0)
prevx = j;
b = n[j] + max(0, b);
if (b > best ||
(b == best && j - prevx > xr - xl)) {
xl = prevx;
xr = j;
best = b;
}
}
if (best > 0)
cout<<"The nicest part of route "<<i<<"
is between stops "<<xl+1<<"
and "<<xr+2<<endl;
}
return 0;
}
\end{verbatim}
\subsection{Edit Distance}
\begin{enumerate}
\item Delete a character
\item Insert a new character
\item Replace a letter
\end{enumerate}
\begin{verbatim}
int DE(char *str1, char *str2) {
int n[SIZE][SIZE];
int i, j, value;
for (i = 0; i <= str1_len; i++) n[i][0] = i;
for (j = 0; j <= str2_len; j++) n[0][j] = j;
for (i = 1; i <= str1_len; i++) {
for (j = 1;j <= str2_len; j++) {
value = (str1[i - 1] != str2[j - 1]);
n[i][j] = min(n[i - 1][j - 1] + value,
n[i - 1][j] + 1,
n[i][j - 1] + 1);
}
}
return n[str1_len][str2_len];
}
\end{verbatim}
\begin{verbatim}
T(i,j) = \min(C_d + T(i-1,j),
T(i, j-1) + C_i,
T(i-1, j-1) + (A[i]==B[j] ? 0 : C_r))
\end{verbatim}
\subsection{Integer Partitions}
$P(n)$ represents the number of possible partitions of a natural number $n$.
$P(4)=5,{{4},{3+1},{2+2},{2+1+1},{1+1+1+1}}$\\
$P(0)=1$\\
$P(n)=0, n<0$\\
$P(n)=p(1,n)$\\
$p(k,n)=p(k+1,n)+p(k,n-k)$\\
$p(k,n)=0$ if $k>n$\\
$p(k,n)=1$ if $k=n$
\subsection{Box Stacking}
A set of boxes is given. $Box_i={h_i, w_i, d_i}$.\\
We can only stack box $i$ on box $j$ if $w_i<w_j$ and $d_i<d_j$.\\
To consider all the orientations of the boxes, replace each box with $3$ boxes such that $w_i\le d_i$ and $box_1[0]=h_i,box_2[0]=w_i,box_3[0]=d_i$.\\
Then, sort the boxes by decreasing area($w_i*d_i)$.\\
$H(j)=$tallest stack of boxes with box $j$ on top.\\
$H(j)=\max_{i<j \& w_i>w_j \& d_i>dj}(H(i))+h_j$\\
Check $H(j)$ for all values of $j$.\\
\subsection{Building Bridges}
Maximize number of non-crossing bridges.
Ex:\\
bridge1:$2,5,1,n,\cdots,4,3$\\
bridge2:$1,2,3,4,\cdots,n$\\
Let $X(i)$ be the index of the corresponding city on northern bank. $X(1) =3, X(2)=1,\ldots$.\\
Find longest increasing subsequence of $X(1),\cdots ,X(n)$.
\subsection{Partition Problem}
\textbf{Input:} A given arrangement $S$ of non-negative numbers ${s_1,\ldots,s_n}$ and an integer $k$.\\
\textbf{Output:} Partition $S$ into $k$ ranges, so as to minimize the maximum sum over all the ranges.\\
\begin{verbatim}
int M[1000][100], D[1000][100];
void partition_i(vector<int> &v, int k) {
int p[1000], i, n = v.size();
v.insert(v.begin(),0);
p[0] = 0;
for(i = 1;i < v.size(); i++)
p[i] = p[i - 1] + v[i];
for (i = 1; i <= n; i++)
M[i][1]=p[i];
for (i = 1; i <= k; i++)
M[1][i] = v[1];
for (i = 2; i <= n; i++) {
for (int j = 2; j<=k; j++) {
M[i][j] = INT_MAX<<1 - 1;
int s = 0;
for (int x = 1; x <= i - 1; x++) {
s = max(M[x][j-1], p[i] - p[x]);
if (M[i][j] > s) {
M[i][j] = s;
D[i][j] = x;
}
}
}
}
printf("%d\n", M[n][k]);
}
//n = number of elements of the initial set
void reconstruct_partition(
const vector<int> &S, int n, int k) {
if (k == 1) {
for (int i = 1; i <= n; i++)
printf("%d ", S[i]);
putchar('\n');
} else {
reconstruct_partition(S, D[n][k], k - 1);
for (int i = D[n][k] + 1; i<= n; i++)
printf("%d ", S[i]);
putchar('\n');
}
}
\end{verbatim}
\subsection{Balanced Partition}
\begin{verbatim}
enum {DONT_GET, GET};
char **sol, **P;
// return 1 if there is a subset of v0...vi with sum j
// 0 otherwise
int calcP(int i, int j, const vi &v) {
if (i < 0 || j < 0) return 0;
if (P[i][j] != -1) return P[i][j];
if (j == 0) { // trivial case
sol[i][j] = DONT_GET;
return P[i][j] = 1;
}
if (v[i] == j) {
sol[i][j] = GET;
return P[i][j] = 1;
}
int res1 = calcP(i - 1, j, v);
int res2 = calcP(i - 1, j - v[i], v);
if (res1 >= res2)
P[i][j] = res1, sol[i][j] = DONT_GET;
else P[i][j] = res2, sol[i][j] = GET;
return P[i][j];
}
// v is the vector of values
// k is the maximum value in v
// sum is the sum of all elements in v
void balanced_partition(vi &v, int k, int sum) {
P = new char*[v.size()];
sol = new char*[v.size()];
for (int i = 0; i < v.size(); i++) {
P[i] = new char[k * v.size() + 1];
sol[i] = new char[k * v.size() + 1];
for (int j = 0; j < k * v.size() + 1; j++)
P[i][j] = -1, sol[i][j] = DONT_GET;
}
for (int i = 0; i < v.size(); i++)
for (int j = 0; j < v.size() * k + 1; j++)
calcP(i, j, v);
//calcP(v.size() - 1, sum/2, v);
int S = sum / 2;
if (sum & 1 || !P[v.size() - 1][S])
cout << "ERROR" <<endl;
else cout << "SUCCESS" << endl;
}
void free_mem(vi& v) {
for (int i = 0; i < v.size(); i++) {
delete P[i]; delete sol[i];
}
delete[] P;
delete[] sol;
}
// get_solution(v.size() - 1, accumulate(v.begin(), v.end(), 0) / 2,
// v1, v2, v);
void get_solution(int i, int j,
vi &S1, vi &S2, vi &v) {
if (j < 0 || i < 0) return;
if (sol[i][j] == GET) {
S1.push_back(v[i]);
return get_solution(i - 1, j - v[i], S1 ,S2, v);
} else {
S2.push_back(v[i]);
return get_solution(i - 1, j, S1, S2, v);
}
}
\end{verbatim}