Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- #include <limits>
- #include <stdexcept>
- #include <CGAL/QP_models.h>
- #include <CGAL/QP_functions.h>
- #include <CGAL/Gmpz.h>
- // choose input type (input coefficients must fit)
- typedef int IT;
- // choose exact type for solver (CGAL::Gmpz or CGAL::Gmpq)
- typedef CGAL::Gmpz ET;
- // program and solution types
- typedef CGAL::Quadratic_program<long> Program;
- typedef CGAL::Quadratic_program_solution<ET> Solution;
- using namespace std;
- int floor_to_double(const CGAL::Quotient<ET> &x)
- {
- double a = floor(CGAL::to_double(x));
- while (a > x)
- a -= 1;
- while (a + 1 <= x)
- a += 1;
- return a;
- }
- int ceil_to_double(const CGAL::Quotient<ET> &x)
- {
- double a = ceil(CGAL::to_double(x));
- while (a < x)
- a += 1;
- while (a - 1 >= x)
- a -= 1;
- return a;
- }
- void testcase()
- {
- long n, m, h, w;
- cin >> n >> m >> h >> w;
- vector<long> free_nails_x(n);
- vector<long> free_nails_y(n);
- vector<long> occupied_nails_x(m);
- vector<long> occupied_nails_y(m);
- long x, y;
- for (int i = 0; i < n; ++i)
- {
- cin >> x >> y;
- free_nails_x[i] = x;
- free_nails_y[i] = y;
- }
- for (int i = 0; i < m; ++i)
- {
- cin >> x >> y;
- occupied_nails_x[i] = x;
- occupied_nails_y[i] = y;
- }
- Program lp(CGAL::SMALLER, true, 1, false, 0);
- for (int i = 0; i < n; ++i)
- {
- lp.set_c(i, -2 * (h + w));
- }
- int counter = 0;
- for (int i = 0; i < n; ++i)
- {
- for (int j = i + 1; j < n; ++j)
- {
- long x_dist = 2 * abs(free_nails_x[i] - free_nails_x[j]);
- long y_dist = 2 * abs(free_nails_y[i] - free_nails_y[j]);
- if (y_dist * w > x_dist * h)
- {
- lp.set_a(i, counter, h);
- lp.set_a(j, counter, h);
- lp.set_b(counter, y_dist);
- }
- else
- {
- lp.set_a(i, counter, w);
- lp.set_a(j, counter, w);
- lp.set_b(counter, x_dist);
- }
- counter++;
- }
- }
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < m; ++j)
- {
- long x_dist = 2 * abs(free_nails_x[i] - occupied_nails_x[j]);
- long y_dist = 2 * abs(free_nails_y[i] - occupied_nails_y[j]);
- if (y_dist * w > x_dist * h)
- {
- lp.set_a(i, counter, h);
- lp.set_b(counter, y_dist - h);
- }
- else
- {
- lp.set_a(i, counter, w);
- lp.set_b(counter, x_dist - w);
- }
- counter++;
- }
- }
- Solution s = CGAL::solve_linear_program(lp, ET());
- cout << setprecision(0) << fixed << ceil_to_double(-s.objective_value()) << endl;
- }
- int main(int argc, char const *argv[])
- {
- ios_base::sync_with_stdio(false);
- int t;
- cin >> t;
- while (t--)
- {
- testcase();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement