Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <bitset>
- #include <vector>
- #include <algorithm>
- #include <map>
- #include <set>
- #include <cassert>
- #pragma GCC optimize ("Ofast")
- #pragma GCC target ("avx2")
- const int N = 257;
- using namespace std;
- int sum[N][N], n, l, w, k, dpup[N], dpleft[N];
- vector<pair<int, int> > d;
- struct GoodRect {
- int X1, Y1, X2, Y2;
- };
- vector<GoodRect> r;
- int Sum(int X1, int Y1, int X2, int Y2) {
- int ans = sum[X2][Y2];
- if (X1 > 0 && X2 > 0) {
- ans += sum[X1 - 1][Y1 - 1];
- }
- if (Y1 > 0) {
- ans -= sum[X2][Y1 - 1];
- }
- if (X1 > 0) {
- ans -= sum[X1 - 1][Y2];
- }
- return ans;
- }
- int Per(GoodRect a) {
- return (a.X2 - a.X1 + 1) * 2 + (a.Y2 - a.Y1 + 1) * 2;
- }
- signed main()
- {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- #else
- std::ios::sync_with_stdio(false);
- cin.tie(0);
- #endif
- cin >> l >> w;
- cin >> n >> k;
- for (int i = 0; i < n; i++) {
- int x, y;
- cin >> x >> y;
- x--;
- y--;
- sum[x][y]++;
- }
- for (int i = 0; i < l; i++) {
- for (int j = 0; j < w; j++) {
- if (sum[i][j] > 0) {
- d.push_back({ i,j });
- }
- if (i == 0) {
- if (j == 0) {
- continue;
- }
- sum[i][j] += sum[i][j - 1];
- continue;
- }
- if (j == 0) {
- sum[i][j] += sum[i - 1][j];
- continue;
- }
- sum[i][j] += sum[i - 1][j];
- sum[i][j] += sum[i][j - 1];
- sum[i][j] -= sum[i - 1][j - 1];
- }
- }
- for (int i = 0; i < N; i++) {
- dpup[i] = 1e9;
- dpleft[i] = 1e9;
- }
- for (int X1 = 0; X1 < l; X1++) {
- for (int X2 = X1; X2 < l; X2++) {
- for (int Y1 = 0; Y1 < w; Y1++) {
- int L = 1, R = w - Y1;
- if (Sum(X1, Y1, X2, w-1) < k) {
- continue;
- }
- if (Sum(X1, Y1, X2, Y1) == k) {
- r.push_back({ X1, Y1, X2,Y1 });
- continue;
- }
- if (Sum(X1, Y1, X2, Y1) == 0) {
- continue;
- }
- if (Sum(X1, Y1, X2, Y1) > k) {
- continue;
- }
- //L -> <k; R -> >=k
- while (R - L > 1) {
- int mid = (L + R) / 2;
- int Y2 = Y1 + mid - 1;
- if (Sum(X1, Y1, X2, Y2) >= k) {
- R = mid;
- }
- else {
- L = mid;
- }
- }
- int Y2 = Y1 + R - 1;
- if (Sum(X1, Y1, X2, Y2) == k) {
- r.push_back({ X1,Y1,X2,Y2 });
- dpleft[X2] = min(dpleft[X2], Per({ X1,Y1,X2,Y2 }));
- dpup[Y1] = min(dpup[Y1], Per({ X1,Y1,X2,Y2 }));
- }
- }
- }
- }
- for (int i = 1; i < N; i++) {
- dpleft[i] = min(dpleft[i], dpleft[i - 1]);
- }
- for (int i = N - 2; i >= 0; i--) {
- dpup[i] = min(dpup[i], dpup[i + 1]);
- }
- int ans = 1e9;
- for (auto rect : r) {
- if (rect.X1 > 0) {
- ans = min(ans, Per(rect) + dpleft[rect.X1 - 1]);
- }
- ans = min(ans, Per(rect) + dpup[rect.Y2 + 1]);
- }
- if (ans == 1e9) {
- cout << "NO";
- return 0;
- }
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement