Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <numeric>
  5. #include <limits>
  6. #include <stdexcept>
  7.  
  8. #include <CGAL/QP_models.h>
  9. #include <CGAL/QP_functions.h>
  10. #include <CGAL/Gmpz.h>
  11.  
  12. // choose input type (input coefficients must fit)
  13. typedef int IT;
  14. // choose exact type for solver (CGAL::Gmpz or CGAL::Gmpq)
  15. typedef CGAL::Gmpz ET;
  16.  
  17. // program and solution types
  18. typedef CGAL::Quadratic_program<long> Program;
  19. typedef CGAL::Quadratic_program_solution<ET> Solution;
  20. using namespace std;
  21.  
  22. int floor_to_double(const CGAL::Quotient<ET> &x)
  23. {
  24. double a = floor(CGAL::to_double(x));
  25. while (a > x)
  26. a -= 1;
  27. while (a + 1 <= x)
  28. a += 1;
  29. return a;
  30. }
  31.  
  32. int ceil_to_double(const CGAL::Quotient<ET> &x)
  33. {
  34. double a = ceil(CGAL::to_double(x));
  35. while (a < x)
  36. a += 1;
  37. while (a - 1 >= x)
  38. a -= 1;
  39. return a;
  40. }
  41.  
  42. void testcase()
  43. {
  44. long n, m, h, w;
  45. cin >> n >> m >> h >> w;
  46. vector<long> free_nails_x(n);
  47. vector<long> free_nails_y(n);
  48. vector<long> occupied_nails_x(m);
  49. vector<long> occupied_nails_y(m);
  50.  
  51. long x, y;
  52. for (int i = 0; i < n; ++i)
  53. {
  54. cin >> x >> y;
  55. free_nails_x[i] = x;
  56. free_nails_y[i] = y;
  57. }
  58. for (int i = 0; i < m; ++i)
  59. {
  60. cin >> x >> y;
  61. occupied_nails_x[i] = x;
  62. occupied_nails_y[i] = y;
  63. }
  64.  
  65. Program lp(CGAL::SMALLER, true, 1, false, 0);
  66.  
  67. for (int i = 0; i < n; ++i)
  68. {
  69. lp.set_c(i, -2 * (h + w));
  70. }
  71.  
  72. int counter = 0;
  73.  
  74. for (int i = 0; i < n; ++i)
  75. {
  76. for (int j = i + 1; j < n; ++j)
  77. {
  78.  
  79. long x_dist = 2 * abs(free_nails_x[i] - free_nails_x[j]);
  80. long y_dist = 2 * abs(free_nails_y[i] - free_nails_y[j]);
  81.  
  82. if (y_dist * w > x_dist * h)
  83. {
  84. lp.set_a(i, counter, h);
  85. lp.set_a(j, counter, h);
  86. lp.set_b(counter, y_dist);
  87. }
  88.  
  89. else
  90. {
  91. lp.set_a(i, counter, w);
  92. lp.set_a(j, counter, w);
  93. lp.set_b(counter, x_dist);
  94. }
  95. counter++;
  96. }
  97. }
  98.  
  99. for (int i = 0; i < n; ++i)
  100. {
  101. for (int j = 0; j < m; ++j)
  102. {
  103.  
  104. long x_dist = 2 * abs(free_nails_x[i] - occupied_nails_x[j]);
  105. long y_dist = 2 * abs(free_nails_y[i] - occupied_nails_y[j]);
  106.  
  107. if (y_dist * w > x_dist * h)
  108. {
  109. lp.set_a(i, counter, h);
  110. lp.set_b(counter, y_dist - h);
  111. }
  112.  
  113. else
  114. {
  115. lp.set_a(i, counter, w);
  116. lp.set_b(counter, x_dist - w);
  117. }
  118. counter++;
  119. }
  120. }
  121.  
  122. Solution s = CGAL::solve_linear_program(lp, ET());
  123. cout << setprecision(0) << fixed << ceil_to_double(-s.objective_value()) << endl;
  124. }
  125.  
  126. int main(int argc, char const *argv[])
  127. {
  128. ios_base::sync_with_stdio(false);
  129. int t;
  130. cin >> t;
  131. while (t--)
  132. {
  133. testcase();
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement