Advertisement
bibaboba12345

Untitled

Jul 12th, 2022
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <bitset>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <map>
  7. #include <set>
  8. #include <cassert>
  9.  
  10. #pragma GCC optimize ("Ofast")
  11. #pragma GCC target ("avx2")
  12.  
  13. const int N = 257;
  14. using namespace std;
  15. int sum[N][N], n, l, w, k, dpup[N], dpleft[N];
  16. vector<pair<int, int> > d;
  17.  
  18. struct GoodRect {
  19. int X1, Y1, X2, Y2;
  20. };
  21.  
  22. vector<GoodRect> r;
  23.  
  24. int Sum(int X1, int Y1, int X2, int Y2) {
  25. int ans = sum[X2][Y2];
  26. if (X1 > 0 && X2 > 0) {
  27. ans += sum[X1 - 1][Y1 - 1];
  28. }
  29. if (Y1 > 0) {
  30. ans -= sum[X2][Y1 - 1];
  31. }
  32. if (X1 > 0) {
  33. ans -= sum[X1 - 1][Y2];
  34. }
  35. return ans;
  36. }
  37.  
  38. int Per(GoodRect a) {
  39. return (a.X2 - a.X1 + 1) * 2 + (a.Y2 - a.Y1 + 1) * 2;
  40. }
  41.  
  42. signed main()
  43. {
  44. #ifdef _DEBUG
  45. freopen("input.txt", "r", stdin);
  46. #else
  47. std::ios::sync_with_stdio(false);
  48. cin.tie(0);
  49. #endif
  50. cin >> l >> w;
  51. cin >> n >> k;
  52. for (int i = 0; i < n; i++) {
  53. int x, y;
  54. cin >> x >> y;
  55. x--;
  56. y--;
  57. sum[x][y]++;
  58. }
  59. for (int i = 0; i < l; i++) {
  60. for (int j = 0; j < w; j++) {
  61. if (sum[i][j] > 0) {
  62. d.push_back({ i,j });
  63. }
  64. if (i == 0) {
  65. if (j == 0) {
  66. continue;
  67. }
  68. sum[i][j] += sum[i][j - 1];
  69. continue;
  70. }
  71. if (j == 0) {
  72. sum[i][j] += sum[i - 1][j];
  73. continue;
  74. }
  75. sum[i][j] += sum[i - 1][j];
  76. sum[i][j] += sum[i][j - 1];
  77. sum[i][j] -= sum[i - 1][j - 1];
  78. }
  79. }
  80.  
  81. for (int i = 0; i < N; i++) {
  82. dpup[i] = 1e9;
  83. dpleft[i] = 1e9;
  84. }
  85. for (int X1 = 0; X1 < l; X1++) {
  86. for (int X2 = X1; X2 < l; X2++) {
  87. for (int Y1 = 0; Y1 < w; Y1++) {
  88. int L = 1, R = w - Y1;
  89. if (Sum(X1, Y1, X2, w-1) < k) {
  90. continue;
  91. }
  92. if (Sum(X1, Y1, X2, Y1) == k) {
  93. r.push_back({ X1, Y1, X2,Y1 });
  94. continue;
  95. }
  96. if (Sum(X1, Y1, X2, Y1) == 0) {
  97. continue;
  98. }
  99. if (Sum(X1, Y1, X2, Y1) > k) {
  100. continue;
  101. }
  102. //L -> <k; R -> >=k
  103. while (R - L > 1) {
  104. int mid = (L + R) / 2;
  105. int Y2 = Y1 + mid - 1;
  106. if (Sum(X1, Y1, X2, Y2) >= k) {
  107. R = mid;
  108. }
  109. else {
  110. L = mid;
  111. }
  112. }
  113. int Y2 = Y1 + R - 1;
  114. if (Sum(X1, Y1, X2, Y2) == k) {
  115. r.push_back({ X1,Y1,X2,Y2 });
  116. dpleft[X2] = min(dpleft[X2], Per({ X1,Y1,X2,Y2 }));
  117. dpup[Y1] = min(dpup[Y1], Per({ X1,Y1,X2,Y2 }));
  118. }
  119. }
  120. }
  121. }
  122.  
  123. for (int i = 1; i < N; i++) {
  124. dpleft[i] = min(dpleft[i], dpleft[i - 1]);
  125. }
  126. for (int i = N - 2; i >= 0; i--) {
  127. dpup[i] = min(dpup[i], dpup[i + 1]);
  128. }
  129.  
  130. int ans = 1e9;
  131.  
  132. for (auto rect : r) {
  133. if (rect.X1 > 0) {
  134. ans = min(ans, Per(rect) + dpleft[rect.X1 - 1]);
  135. }
  136. ans = min(ans, Per(rect) + dpup[rect.Y2 + 1]);
  137. }
  138.  
  139. if (ans == 1e9) {
  140. cout << "NO";
  141. return 0;
  142. }
  143. cout << ans;
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement