-
Notifications
You must be signed in to change notification settings - Fork 8
/
partition3.c
91 lines (79 loc) · 2.44 KB
/
partition3.c
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
/* ----------------------------------------------------------------------- *
*
* Author: Leandro Augusto Lacerda Campos <[email protected]>
*
* Data Structures and Algorithms Specialization,
* by University of California, San Diego,
* and National Research University Higher School of Economics
*
* Course 1: Algorithmic Toolbox
*
* Solution for Partitioning Souvenirs Problem
*
* ----------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
/* maximum number of elements */
#define NEL_MAX 20
/* number of partitions */
#define NPART 3
/* maximum limit value */
#define LIM_MAX ((20 * 30) / NPART)
/* the keyword typeof is not available in programs compiled with -std */
#define typeof __typeof__
/*
* _cmp: compares two values of the same or compatible types.
*/
#define _cmp(x, y, op) ((x) op (y) ? (x) : (y))
/*
* _cmp_once: compares two values of the same or compatible types
* avoiding multiple evaluations of the arguments.
*/
#define _cmp_once(x, y, op) ({ \
typeof(x) _x = (x); \
typeof(y) _y = (y); \
_cmp(_x, _y, op); })
/*
* max: returns the maximum of two values of the same or compatible types.
*/
#define max(x, y) _cmp_once(x, y, >)
/*
* partition3: returns 1 if it is possible to partition vals[0], ...,
* vals[nel-1] into three subsets with equal sums, and 0 otherwise.
*/
int partition3(unsigned int *vals, unsigned int nel)
{
static unsigned int tbl[LIM_MAX + 1][NEL_MAX + 1][NPART + 1] = { 0 };
unsigned int i, v, j;
unsigned int sum, lim, val, tmp;
sum = 0;
for (i = 0; i < nel; i++)
sum += vals[i];
if (sum % NPART != 0)
return 0;
lim = sum / NPART;
for (j = 1; j <= NPART; j++)
for (i = 1; i <= nel; i++)
for (v = 1; v <= lim; v++) {
tbl[v][i][j] = tbl[v][i - 1][j];
if ((val = vals[i - 1]) <= v) {
tmp = tbl[v - val][i - 1][j] + val - (tbl[v][i][j-1]
- tbl[v][i][j - 1]);
tbl[v][i][j] = max(tmp, tbl[v][i][j]);
}
}
for (j = 1; j <= NPART; j++)
if (tbl[lim][nel][j] != lim)
return 0;
return 1;
}
int main()
{
unsigned int nel, vals[NEL_MAX];
unsigned int i;
scanf("%u", &nel);
for (i = 0; i < nel; i++)
scanf("%u", &vals[i]);
printf("%u\n", partition3(vals, nel));
return 0;
}