-
Notifications
You must be signed in to change notification settings - Fork 2
/
zad7_ms.cpp
195 lines (170 loc) · 4.19 KB
/
zad7_ms.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <time.h>
using namespace std;
int brut(const vector<int> &x);
std::vector<int> generate_random(int NumberCount, int minimum, int maximum);
int **make_2d(int x, int y);
int calc_sum(int *sums, int starting, int ending)
{
int outside = 0;
if (starting != 0)
outside = sums[starting - 1];
int length = sums[ending] - outside;
return length;
}
int calc_glue(int l, int r, int **dp, int *sums)
{
int currMin = INT32_MAX;
for (int i = l + 1; i <= r; i++)
{
int current = dp[l][i - 1] + dp[i][r] + max(calc_sum(sums, l, i - 1), calc_sum(sums, i, r));
if (current < currMin)
{
currMin = current;
}
}
return currMin;
}
int *calc_prefix_sum(const vector<int> &x)
{
int n = (int)x.size();
int *sums = (int *)malloc((size_t)n * sizeof(int));
if (n == 0)
return sums;
sums[0] = x[0];
for (int i = 1; i < n; i++)
{
sums[i] = sums[i - 1] + x[i];
}
return sums;
}
// T(n) = O(n^3)
// M(n) = O(n^2)
int klej(const vector<int> &x)
{
int n = (int)x.size();
int *sums = calc_prefix_sum(x);
int **dp = make_2d(n, n);
// c = r - l
for (int c = 1; c < n; c++)
{
for (int l = 0; l + c < n; l++)
{
int r = l + c;
dp[l][r] = calc_glue(l, r, dp, sums);
}
}
int ans = dp[0][n - 1];
return ans;
}
int main()
{
int n;
cin >> n;
while (true)
{
vector<int> stick = generate_random(n,0, 100);
int glue = klej(stick);
int brutAns = brut(stick);
cout << glue << " " << brutAns << (brutAns == glue ? " correct" : " wrong") << endl;
stick.clear();
}
return 0;
}
std::vector<int> generate_random(int NumberCount, int minimum, int maximum)
{
std::random_device rd;
std::mt19937 gen(rd()); // these can be global and/or static, depending on how you use random elsewhere
std::vector<int> values(NumberCount);
std::uniform_int_distribution<> dis(minimum, maximum);
std::generate(values.begin(), values.end(), [&]()
{ return dis(gen); });
return values;
}
int **make_2d(int x, int y)
{
int **ret = (int **)malloc((size_t)y * (sizeof(int *)));
for (int i = 0; i < y; i++)
{
ret[i] = (int *)malloc((size_t)x * sizeof(int));
for (int j = 0; j < x; j++)
{
ret[i][j] = 0;
}
}
return ret;
}
// brut \/
typedef struct c *Stick;
struct c
{
int glue;
int l;
int r;
int dl;
};
Stick new_stick(int glue, int l, int r, int dl)
{
Stick st = (Stick)malloc(sizeof(c));
st->glue = glue;
st->l = l;
st->r = r;
st->dl = dl;
return st;
}
Stick new_stick(Stick a, Stick b)
{
int dl = a->dl + b->dl;
Stick bigger = new_stick(a->glue + b->glue + max(a->dl, b->dl), min(a->l, b->l), max(a->r, b->r), dl);
return bigger;
}
bool compare(pair<pair<Stick *, Stick *>, int> a, pair<pair<Stick *, Stick *>, int> b)
{
return a.second < b.second;
}
// T(n) = O(n! * (n-1)log(n-1))
int brut(const vector<int> &x)
{
int n = (int)x.size();
vector<int> c(n - 1);
for (int i = 0; i < n - 1; i++)
{
c[i] = i;
}
int min = INT32_MAX;
do
{
vector<Stick> sticks(n);
for (int i = 0; i < n; i++)
{
sticks[i] = new_stick(0, i, i, x[i]);
}
vector<pair<pair<Stick *, Stick *>, int>> order(n - 1);
for (int i = 0; i < n - 1; i++)
{
order[i] = make_pair(make_pair(&sticks[i], &sticks[i + 1]), c[i]);
}
sort(order.begin(), order.end(), compare);
for (int i = 0; i < n - 1; i++)
{
Stick *a = order[i].first.first;
Stick *b = order[i].first.second;
Stick bigger = new_stick(*a, *b);
free(*a);
free(*b);
sticks[bigger->l] = bigger;
sticks[bigger->r] = bigger;
}
if (sticks[0]->glue < min)
{
min = sticks[0]->glue;
}
free(sticks[0]);
sticks.clear();
} while (next_permutation(c.begin(), c.end()));
return min;
}
// brut /\