Submission #582799

# Submission time Handle Problem Language Result Execution time Memory
582799 2022-06-24T12:50:04 Z MilosMilutinovic Domino (COCI15_domino) C++14
10 / 160
2078 ms 524288 KB
/**
 *    author:  wxhtzdy
 *    created: 24.06.2022 12:30:23
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<vector<int>> a(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }
  vector<array<int, 3>> b;
  auto Id = [&](int i, int j) {
    return i * n + j;
  };
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i + 1 < n) {
        b.push_back({a[i][j] + a[i + 1][j], Id(i, j), Id(i + 1, j)});
      }
      if (j + 1 < n) {
        b.push_back({a[i][j] + a[i][j + 1], Id(i, j), Id(i, j + 1)});
      }
    }
  }
  sort(b.rbegin(), b.rend());
  b.resize(min((int) b.size(), 50));
  int L = (int) b.size() / 2;
  int R = (int) b.size() - L;
  vector<vector<int>> f(1 << R, vector<int>(k + 1));
  for (int s = 0; s < (1 << R); s++) {
    if (__builtin_popcount(s) >= k) {
      for (int i = 0; i < R; i++) {
        if (s >> i & 1) {
          for (int j = 0; j <= k; j++) {
            f[s][j] = max(f[s][j], f[s ^ (1 << i)][j]);
          }
        }
      }
      for (int j = 1; j <= k; j++) {
        f[s][j] = max(f[s][j], f[s][j - 1]);
      }
      continue;
    }
    for (int i = 0; i < R; i++) {
      if (s >> i & 1) {
        continue;
      }
      bool ok = true;
      for (int j = 0; j < R; j++) {
        if (!(s >> j & 1)) {
          continue;
        }
        if (b[L + i][1] == b[L + j][1]) {
          ok = false;
        }
        if (b[L + i][1] == b[L + j][2]) {
          ok = false;
        }
        if (b[L + i][2] == b[L + j][1]) {
          ok = false;
        }
        if (b[L + i][2] == b[L + j][2]) {
          ok = false;
        }
      }
      if (ok) {
        for (int j = 0; j < k; j++) {
          f[s | (1 << i)][j + 1] = max(f[s | (1 << i)][j + 1], f[s][j] + b[L + i][0]);
        }
      }
    }
    for (int j = 1; j <= k; j++) {
      f[s][j] = max(f[s][j], f[s][j - 1]);
    }
  }
  int ans = 0;
  for (int s = 0; s < (1 << L); s++) {
    if (__builtin_popcount(s) > k) {
      continue;
    }
    int t = (1 << R) - 1;
    bool ok = true;
    for (int i = 0; i < L; i++) {
      if (s >> i & 1) {
        for (int j = i + 1; j < L; j++) {
          if (s >> j & 1) {
            if (b[i][1] == b[j][1]) {
              ok = false;
            }
            if (b[i][1] == b[j][2]) {
              ok = false;
            }
            if (b[i][2] == b[j][1]) {
              ok = false;
            }
            if (b[i][2] == b[j][2]) {
              ok = false;
            }
          }
        }
      }
    }
    if (!ok) {
      continue;
    }
    for (int i = 0; i < L; i++) {
      if (s >> i & 1) {
        for (int j = 0; j < R; j++) {
          if (t >> j & 1) {
            bool found = false;
            if (b[i][1] == b[L + j][1]) {
              found = true;
            }
            if (b[i][1] == b[L + j][2]) {
              found = true;
            }
            if (b[i][2] == b[L + j][1]) {
              found = true;
            }
            if (b[i][2] == b[L + j][2]) {
              found = true;
            }
            if (found) {
              t ^= (1 << j);
            }
          }
        }
      }
    }
    int ft = 0;
    for (int i = 0; i < L; i++) {
      if (s >> i & 1) {
        ft += b[i][0];
      }
    }
    ans = max(ans, ft + f[t][k - __builtin_popcount(s)]);
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      sum += a[i][j];
    }
  }
  cout << sum - ans << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 574 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 462 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2078 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 417 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1369 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 817 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2051 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 378 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2029 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 368 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 596 KB Output is correct
2 Correct 3 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 2024 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 352 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 712 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 389 ms 74116 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1949 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -