Submission #582630

# Submission time Handle Problem Language Result Execution time Memory
582630 2022-06-24T07:47:05 Z MilosMilutinovic Domino (COCI15_domino) C++14
110 / 160
4000 ms 125648 KB
/**
 *    author:  wxhtzdy
 *    created: 24.06.2022 09:28:57
**/
#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];
    }
  }
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      sum += a[i][j];
    }
  }
  vector<tuple<int, int, int>> b;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i + 1 < n) {
        b.emplace_back(-a[i][j] - a[i + 1][j], i * n + j, (i + 1) * n + j);
      }
      if (j + 1 < n) {
        b.emplace_back(-a[i][j] - a[i][j + 1], i * n + j, i * n + j + 1);
      }
    }
  }
  sort(b.begin(), b.end()); 
  b.resize(min((int) b.size(), 42));
  long long mx = 0, curr = 0;
  vector<int> v;
  vector<int> cnt(n * n);
  bool found = false;
  function<void(int, int)> Gen = [&](int i, int p) {
    if (p + (int) b.size() - i < k || p > k) {
      return;
    }
    if (i == (int) b.size()) {
      found = true;
      mx = max(mx, curr);
      return;
    }
    Gen(i + 1, p);
    if (cnt[get<1>(b[i])] == 0 && cnt[get<2>(b[i])] == 0) {
      curr -= get<0>(b[i]);
      v.push_back(get<1>(b[i]));
      v.push_back(get<2>(b[i]));
      cnt[get<1>(b[i])]++;
      cnt[get<2>(b[i])]++;
      Gen(i + 1, p + 1);
      cnt[get<1>(b[i])]--;
      cnt[get<2>(b[i])]--;
      curr += get<0>(b[i]);
      v.pop_back();
      v.pop_back();
    }
  };
  Gen(0, 0);
  assert(found);
  cout << sum - mx << '\n';                                               
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 82 ms 9016 KB Output is correct
2 Correct 44 ms 8080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1557 ms 125560 KB Output is correct
2 Correct 763 ms 125512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 212 KB Output is correct
2 Correct 4 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 900 ms 107776 KB Output is correct
2 Correct 579 ms 107724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 424 ms 31612 KB Output is correct
2 Correct 214 ms 31576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1725 ms 125556 KB Output is correct
2 Correct 813 ms 125560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 954 ms 468 KB Output is correct
2 Correct 195 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2514 ms 125528 KB Output is correct
2 Correct 929 ms 125648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4051 ms 788 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4048 ms 125556 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4070 ms 596 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4093 ms 31572 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 133 ms 332 KB Output is correct
2 Correct 108 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4054 ms 125564 KB Time limit exceeded
2 Halted 0 ms 0 KB -