Submission #583717

# Submission time Handle Problem Language Result Execution time Memory
583717 2022-06-26T06:52:38 Z MilosMilutinovic Domino (COCI15_domino) C++14
160 / 160
3826 ms 273360 KB
/**
 *    author:  wxhtzdy
 *    created: 24.06.2022 12:30:23
**/              
#include <bits/stdc++.h>
 
using namespace std;
 
int f[1 << 23][9];
 
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());
  while ((int) b.size() > 50) {
    b.pop_back();
  }                   
  int sz = (int) b.size();
  int R = min(22, sz / 2);                                      
  int L = sz - R;
  for (int s = 0; s < (1 << R); s++) {
    for (int j = 0; j <= k; j++) {
      f[s][j] = 0;
    }
  }
  vector<int> bb(R);
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < R; j++) {
      if (i == j) {
        continue;
      }        
      if (b[L + i][1] == b[L + j][1]) {
        bb[i] |= (1 << j);
      }
      if (b[L + i][1] == b[L + j][2]) {
        bb[i] |= (1 << j);
      }
      if (b[L + i][2] == b[L + j][1]) {
        bb[i] |= (1 << j);
      }
      if (b[L + i][2] == b[L + j][2]) {
        bb[i] |= (1 << j);
      }
    }
  }
  for (int s = 0; s < (1 << R); s++) {
    if (__builtin_popcount(s) >= k) {
      for (int i = 0; i < R; i++) {
        if (!(s >> i & 1)) {
          continue;
        }
        for (int j = 0; j <= k; j++) {
          f[s][j] = max(f[s][j], f[s ^ (1 << i)][j]);
        }
      }
      for (int p = 1; p <= k; p++) {
        f[s][p] = max(f[s][p], f[s][p - 1]);
      }
      continue;
    }
    int sub = 0;
    for (int i = 0; i < R; i++) {
      if (s >> i & 1) {
        sub |= bb[i];
      }
    }
    for (int i = 0; i < R; i++) {
      if (s >> i & 1) {
        continue;
      }
      if (sub >> i & 1) {
        continue;
      }
      for (int p = 0; p < k; p++) {
        f[s | (1 << i)][p + 1] = max(f[s | (1 << i)][p], f[s][p] + b[L + i][0]);
      }
    }
    for (int i = 0; i < R; i++) {
      if (!(s >> i & 1)) {
        continue;
      }
      for (int p = 0; p < k; p++) {
        f[s][p] = max(f[s][p], f[s ^ (1 << i)][p]);
      }
    }
    for (int p = 1; p <= k; p++) {
      f[s][p] = max(f[s][p], f[s][p - 1]);
    }
  }
  int ans = 0;    
  for (int s = 0; s < (1 << R); s++) {
    for (int j = 0; j <= k; j++) {
      ans = max(ans, f[s][j]);
    }
  }
  vector<int> bad(L);
  for (int i = 0; i < L; i++) {
    for (int j = 0; j < L; j++) {
      if (i == j) {
        continue;
      }   
      if (b[i][1] == b[j][1]) {
        bad[i] |= (1 << j);
        continue;
      }
      if (b[i][1] == b[j][2]) {
        bad[i] |= (1 << j);
        continue;
      }
      if (b[i][2] == b[j][1]) {
        bad[i] |= (1 << j);
        continue;
      }
      if (b[i][2] == b[j][2]) {
        bad[i] |= (1 << j);
        continue;
      }
    }
  }
  vector<int> oth(L);
  for (int i = 0; i < L; i++) {
    for (int j = 0; j < R; j++) {
      if (b[i][1] == b[L + j][1]) {
        oth[i] |= (1 << j);
        continue;
      }
      if (b[i][1] == b[L + j][2]) {
        oth[i] |= (1 << j);
        continue;
      }
      if (b[i][2] == b[L + j][1]) {
        oth[i] |= (1 << j);
        continue;
      }
      if (b[i][2] == b[L + j][2]) {
        oth[i] |= (1 << j);
        continue;
      }
    }
  }
  for (int s = 0; s < (1 << L); s++) {
    if (__builtin_popcount(s) > k) {
      continue;  
    }
    int t = (1 << R) - 1;
    bool ok = true;       
    int ft = 0;
    for (int i = 0; i < L; i++) {
      if (s >> i & 1) {
        ft += b[i][0];
        if (bad[i] & s) {
          ok = false;
          break;
        }
      }
    }
    if (!ok) {
      continue;
    }
    int q = 0;
    for (int i = 0; i < L; i++) {
      if (s >> i & 1) {
        q |= oth[i];
      }
    }
    t -= q;
    ans = max(ans, ft + f[t][k - __builtin_popcount(s)]);
  }
  long long 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 Correct 1161 ms 154984 KB Output is correct
2 Correct 1124 ms 154928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1173 ms 148216 KB Output is correct
2 Correct 1163 ms 148220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2922 ms 257796 KB Output is correct
2 Correct 2469 ms 257748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1176 ms 148060 KB Output is correct
2 Correct 1335 ms 148056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2166 ms 209896 KB Output is correct
2 Correct 2104 ms 209924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1679 ms 175576 KB Output is correct
2 Correct 1644 ms 175596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3022 ms 257796 KB Output is correct
2 Correct 2782 ms 257876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1414 ms 148224 KB Output is correct
2 Correct 1381 ms 148160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3250 ms 257796 KB Output is correct
2 Correct 3826 ms 257752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1784 ms 148400 KB Output is correct
2 Correct 1620 ms 148376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3555 ms 257796 KB Output is correct
2 Correct 3041 ms 257824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2028 ms 148236 KB Output is correct
2 Correct 1543 ms 148252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2467 ms 175580 KB Output is correct
2 Correct 1879 ms 175580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 208 ms 37236 KB Output is correct
2 Correct 193 ms 37240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3693 ms 257792 KB Output is correct
2 Correct 2952 ms 273360 KB Output is correct