Submission #583637

# Submission time Handle Problem Language Result Execution time Memory
583637 2022-06-25T19:51:00 Z MilosMilutinovic Domino (COCI15_domino) C++14
100 / 160
4000 ms 273356 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;
    }
  }
  for (int s = 0; s < (1 << R); s++) {
    if (__builtin_popcount(s) >= k) {
      for (int i = 0; i < R; i++) {
        for (int j = 0; j <= k; j++) {
          if (s >> i & 1) {
            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;
    }
    for (int i = 0; i < R; i++) {
      for (int p = 0; p < k; p++) {
        if (s >> i & 1) {
          continue;
        }
        bool ok = true;
        for (int j = 0; j < R; j++) {
          if ((s >> j & 1) == 0) {
            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) {
          f[s | (1 << i)][p + 1] = max(f[s | (1 << i)][p], f[s][p] + b[L + i][0]);
        }
      }
    }
    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]);
    }
  }
  for (int s = 0; s < (1 << R); s++) {
    for (int j = 0; j <= k; j++) {
      for (int i = 0; i < k; i++) {
        if (s >> i & 1) {
          f[s][j] = max(f[s][j], f[s ^ (1 << i)][j]);
        }
      }
      if (j > 0) {
        f[s][j] = max(f[s][j], f[s][j - 1]);
      }
    }
  }
  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)]);
  }
  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 1298 ms 155020 KB Output is correct
2 Correct 1279 ms 155048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1303 ms 148216 KB Output is correct
2 Correct 1304 ms 148220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2894 ms 257792 KB Output is correct
2 Correct 2508 ms 257796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1406 ms 148064 KB Output is correct
2 Correct 1401 ms 148172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2281 ms 209896 KB Output is correct
2 Correct 2196 ms 209920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1945 ms 175580 KB Output is correct
2 Correct 1905 ms 175484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3386 ms 257796 KB Output is correct
2 Correct 3151 ms 269516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2287 ms 148220 KB Output is correct
2 Correct 2216 ms 148220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3858 ms 257788 KB Output is correct
2 Correct 3614 ms 273356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3487 ms 148364 KB Output is correct
2 Incorrect 2933 ms 148424 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 468 KB Output is correct
2 Correct 5 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4078 ms 257792 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4074 ms 148308 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4081 ms 175600 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 882 ms 37240 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4089 ms 257876 KB Time limit exceeded
2 Halted 0 ms 0 KB -