Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <assert.h>
- #include <unordered_map>
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- typedef vector < long long > vll;
- typedef pair < long long, long long > pll;
- typedef pair < int, int > pii;
- typedef vector < int > vii;
- #define csl ios_base::sync_with_stdio(false); cin.tie(NULL)
- #define l(x) (((x) << 1) | 1)
- #define r(x) ((l(x)) + 1)
- #define mp make_pair
- #define fst first
- #define snd second
- ll t, n, u, v, m, q, r, ql, qr, k, l, s, x, y, w;
- const int N = 300;
- const long long mod = 1e9 + 7;
- const long long INF = 1LL << 57LL;
- const bool JUDGE = false;
- ll _n;
- int g[N][N];
- int gt[N][N];
- int pre[N][N];
- ll per[N];
- inline int cnt(int xa, int ya, int xb, int yb) {
- int ret = pre[xb][yb] - pre[xa - 1][yb] - pre[xb][ya - 1] + pre[xa - 1][ya - 1];
- return ret;
- }
- int main(){
- csl;
- if (JUDGE) {
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- }
- cin >> n >> m;
- cin >> _n >> k;
- for (int i = 0; i < _n; ++i) {
- cin >> x >> y;
- g[x][y]++;
- gt[y][x]++;
- }
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- pre[i][j] = g[i][j] - pre[i - 1][j - 1] + pre[i - 1][j] + pre[i][j - 1];
- }
- }
- ll ans = INF;
- per[0] = INF;
- for (ll i = 1; i <= n; ++i) {
- per[i] = INF;
- for (ll j = i; j > 0; --j) {
- int l = 1, r = 1;
- int curcnt = cnt(j, l, i, r);
- while (r < n + 1 && l < n + 1) {
- while (r < n + 1 && curcnt < k) {
- r++;
- curcnt = cnt(j, l, i, r);
- }
- if (r == n + 1) continue;
- while (curcnt >= k && l < n + 1) {
- if (curcnt == k) {
- per[i] = min(per[i], 2 * (i - j + 1) + 2 * (r - l + 1));
- }
- l++;
- curcnt = cnt(j, l, i, r);
- }
- }
- ans = min(ans, per[i] + per[j - 1]);
- }
- //cout << per[i] << '\n';
- per[i] = min(per[i], per[i - 1]);
- }
- swap(n, m);
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) g[i][j] = gt[i][j];
- }
- memset(pre, 0, sizeof(pre));
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- pre[i][j] = g[i][j] - pre[i - 1][j - 1] + pre[i - 1][j] + pre[i][j - 1];
- }
- }
- per[0] = INF;
- for (ll i = 1; i <= n; ++i) {
- per[i] = INF;
- for (ll j = i; j > 0; --j) {
- int l = 1, r = 1;
- int curcnt = cnt(j, l, i, r);
- while (r < n + 1 && l < n + 1) {
- while (r < n + 1 && curcnt < k) {
- r++;
- curcnt = cnt(j, l, i, r);
- }
- if (r == n + 1) continue;
- while (curcnt >= k && l < n + 1) {
- if (curcnt == k) {
- per[i] = min(per[i], 2 * (i - j + 1) + 2 * (r - l + 1));
- }
- l++;
- curcnt = cnt(j, l, i, r);
- }
- }
- ans = min(ans, per[i] + per[j - 1]);
- }
- // cout << per[i] << '\n';
- per[i] = min(per[i], per[i - 1]);
- }
- if (ans != INF) cout << ans << '\n';
- else cout << "NO\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement