-
Notifications
You must be signed in to change notification settings - Fork 0
/
suez.cpp
309 lines (252 loc) · 8.59 KB
/
suez.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
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
//
// suez.cpp
// Algorithms Lab
//
// Created by Jonas Gessner
// Copyright © 2019 Jonas Gessner. All rights reserved.
//
#include <iostream>
#include <CGAL/QP_models.h>
#include <CGAL/QP_functions.h>
#include <CGAL/Gmpz.h>
#include <algorithm>
// choose input type (input coefficients must fit)
typedef int IT;
// choose exact type for solver (CGAL::Gmpz or CGAL::Gmpq)
typedef CGAL::Gmpq ET;
// program and solution types
typedef CGAL::Quadratic_program<IT> Program;
typedef CGAL::Quadratic_program_solution<ET> Solution;
struct Point
{
int x;
int y;
int index;
Point(const int _x, const int _y, const int _index) : x(_x), y(_y), index(_index){};
public:
bool pointIsBoundedInXByPoint(const Point &other, const CGAL::Gmpq &whRatio) const
{
int xDelta = abs(other.x - x);
int yDelta = abs(other.y - y);
if (yDelta == 0)
{
return true;
}
CGAL::Gmpq whRatioDiff = CGAL::Gmpq(xDelta) / CGAL::Gmpq(yDelta);
if (whRatioDiff >= whRatio)
{
return true;
}
else
{
return false;
}
}
bool pointIsBoundedInYByPoint(const Point &other, const CGAL::Gmpq &whRatio) const
{
int xDelta = abs(other.x - x);
int yDelta = abs(other.y - y);
if (yDelta == 0)
{
return xDelta == 0;
}
CGAL::Gmpq whRatioDiff = CGAL::Gmpq(xDelta) / CGAL::Gmpq(yDelta);
if (whRatioDiff <= whRatio)
{
return true;
}
else
{
return false;
}
}
};
bool pointXAscending(const Point &lhs, const Point &rhs)
{
return lhs.x < rhs.x;
}
bool pointYAscending(const Point &lhs, const Point &rhs)
{
return lhs.y < rhs.y;
}
using namespace std;
double ceil_to_double(const ET &x)
{
double a = std::ceil(CGAL::to_double(x));
while (a < x)
a += 1;
while (a - 1 >= x)
a -= 1;
return a;
}
static void runTestCase()
{
int n, m, h, w;
cin >> n >> m >> h >> w;
const CGAL::Gmpq whRatio = CGAL::Gmpq(w) / CGAL::Gmpq(h);
Program lp(CGAL::SMALLER, true, 1, false, 0);
lp.set_c0(0);
// variables: scale factor for each new poster
// target function: sum of all areas (simply sum of all variables, since they all multiply the same area (w*h) that factor can be ignored)
// constraints: for each new poster:
// find closest x nail to the left
// find closest x nail to the right
// find closest y nail to the top
// find closest y nail to the bottom:
// a * (w / 2) + a_other * (w / 2) <= x - x_old
// a * w + a_other * w <= 2*(x - x_old)
// bzw
// a * w <= 2 * (x - x_old) - w
// etc
vector<Point> newPoints;
newPoints.reserve(n);
for (int i = 0; i < n; i++)
{ // new posters
int x, y;
cin >> x >> y;
lp.set_c(i, -(2 * w + 2 * h));
newPoints.push_back(Point(x, y, i));
}
vector<Point> oldPointsXSorted;
oldPointsXSorted.reserve(m);
vector<Point> oldPointsYSorted;
oldPointsYSorted.reserve(m);
for (int i = 0; i < m; i++)
{ // old posters
int x, y;
cin >> x >> y;
oldPointsXSorted.push_back(Point(x, y, 0));
oldPointsYSorted.push_back(Point(x, y, 0));
}
sort(oldPointsXSorted.begin(), oldPointsXSorted.end(), pointXAscending);
sort(oldPointsYSorted.begin(), oldPointsYSorted.end(), pointYAscending);
// Binary search does little to improve running time. Pretty useless. Simple for loop will also do the trick.
const auto selectSuccessor = [&](const vector<Point> &xHaystack, const vector<Point> &yHaystack, const Point &point, bool y) -> const Point * {
const vector<Point> &haystack = y ? yHaystack : xHaystack;
int bound = upper_bound(haystack.begin(), haystack.end(), point, (y ? pointYAscending : pointXAscending)) - haystack.begin();
while (bound < haystack.size())
{
const Point &found = haystack.at(bound);
if (y ? point.pointIsBoundedInYByPoint(found, whRatio) : point.pointIsBoundedInXByPoint(found, whRatio))
{
return &found;
}
else
{
bound++;
}
}
return NULL;
};
const auto selectPredecessor = [&](const vector<Point> &xHaystack, const vector<Point> &yHaystack, const Point &point, bool y) -> const Point * {
const vector<Point> &haystack = y ? yHaystack : xHaystack;
int bound = lower_bound(haystack.begin(), haystack.end(), point, (y ? pointYAscending : pointXAscending)) - haystack.begin();
while (bound > 0 && bound <= haystack.size())
{
const Point &found = haystack.at(bound - 1);
if (y ? point.pointIsBoundedInYByPoint(found, whRatio) : point.pointIsBoundedInXByPoint(found, whRatio))
{
return &found;
}
else
{
bound--;
}
}
return NULL;
};
const auto selectBoundingPoints = [&](const vector<Point> &xHaystack, const vector<Point> &yHaystack, const Point &point) -> pair<Point *, Point *> {
const Point *xPredecessor = selectPredecessor(xHaystack, yHaystack, point, false);
const Point *yPredecessor = selectPredecessor(xHaystack, yHaystack, point, true);
const Point *xSuccessor = selectSuccessor(xHaystack, yHaystack, point, false);
const Point *ySuccessor = selectSuccessor(xHaystack, yHaystack, point, true);
Point *xBound = NULL;
Point *yBound = NULL;
const auto useXBound = [&](Point *p) {
if (p == NULL)
{
return;
}
if (xBound == NULL || abs(p->x - point.x) < abs(xBound->x - point.x))
{
xBound = p;
}
};
const auto useYBound = [&](Point *p) {
if (p == NULL)
{
return;
}
if (yBound == NULL || abs(p->y - point.y) < abs(yBound->y - point.y))
{
yBound = p;
}
};
useXBound((Point *)xPredecessor);
useXBound((Point *)xSuccessor);
useYBound((Point *)yPredecessor);
useYBound((Point *)ySuccessor);
return {xBound, yBound};
};
int constraint = 0;
for (int i = 0; i < n; i++)
{
const Point &newPoint = newPoints.at(i);
const pair<Point *, Point *> oldBounds = selectBoundingPoints(oldPointsXSorted, oldPointsYSorted, newPoint);
if (oldBounds.first != NULL)
{
const Point oldXBound = *oldBounds.first;
lp.set_a(newPoint.index, constraint, w);
lp.set_b(constraint, 2 * abs(oldXBound.x - newPoint.x) - w);
constraint++;
}
if (oldBounds.second != NULL)
{
const Point oldYBound = *oldBounds.second;
lp.set_a(newPoint.index, constraint, h);
lp.set_b(constraint, 2 * abs(oldYBound.y - newPoint.y) - h);
constraint++;
}
const auto useXBound = [&](const Point &p) {
lp.set_a(newPoint.index, constraint, w);
lp.set_a(p.index, constraint, w);
lp.set_b(constraint, 2 * abs(p.x - newPoint.x));
constraint++;
};
const auto useYBound = [&](const Point &p) {
lp.set_a(newPoint.index, constraint, h);
lp.set_a(p.index, constraint, h);
lp.set_b(constraint, 2 * abs(p.y - newPoint.y));
constraint++;
};
for (int j = i + 1; j < n; j++)
{
const Point &check = newPoints.at(j);
if (check.index == newPoint.index)
{
continue;
}
if (newPoint.pointIsBoundedInXByPoint(check, whRatio))
{
useXBound(check);
}
else
{
useYBound(check);
}
}
}
Solution s = CGAL::solve_linear_program(lp, ET());
cout << (long long)ceil_to_double(-s.objective_value_numerator() / s.objective_value_denominator()) << "\n";
}
int main(int argc, const char *argv[])
{
ios_base::sync_with_stdio(false);
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
runTestCase();
}
return 0;
}