結果

問題 No.1345 Beautiful BINGO
コンテスト
ユーザー siman
提出日時 2023-04-20 23:33:18
言語 C++17(clang)
(clang++ 22.1.2 + boost 1.89.0)
コンパイル:
clang++ -O2 -lm -std=c++1z -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 1,727 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,554 ms
コンパイル使用メモリ 152,832 KB
実行使用メモリ 7,972 KB
最終ジャッジ日時 2026-05-05 09:10:39
合計ジャッジ時間 8,561 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 61
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:33:12: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   33 |   int A[N][N];
      |            ^
main.cpp:33:12: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:31:7: note: declared here
   31 |   int N, M;
      |       ^
main.cpp:33:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   33 |   int A[N][N];
      |         ^
main.cpp:33:9: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:31:7: note: declared here
   31 |   int N, M;
      |       ^
main.cpp:45:16: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   45 |       int B[N][N];
      |                ^
main.cpp:45:16: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:31:7: note: declared here
   31 |   int N, M;
      |       ^
main.cpp:45:13: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   45 |       int B[N][N];
      |             ^
main.cpp:45:13: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:31:7: note: declared here
   31 |   int N, M;
      |       ^
4 warnings generated.

ソースコード

diff #
raw source code

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

struct Node {
  int y;
  int v;

  Node(int y = -1, int v = -1) {
    this->y = y;
    this->v = v;
  }

  bool operator>(const Node &n) const {
    return v > n.v;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  int A[N][N];

  for (int y = 0; y < N; ++y) {
    for (int x = 0; x < N; ++x) {
      cin >> A[y][x];
    }
  }

  int ans = INT_MAX;
  int L = (1 << N);
  for (int mask = 0; mask < L; ++mask) {
    for (int v = 0; v < 4; ++v) {
      int B[N][N];
      memcpy(B, A, sizeof(A));
      int sum = 0;
      int m = M;

      for (int x = 0; x < N; ++x) {
        if ((mask >> x & 1) == 0) continue;
        --m;

        for (int y = 0; y < N; ++y) {
          sum += B[y][x];
          B[y][x] = 0;
        }
      }

      if (v >> 0 & 1) {
        --m;
        for (int i = 0; i < N; ++i) {
          sum += B[i][i];
          B[i][i] = 0;
        }
      }

      if (v >> 1 & 1) {
        --m;
        for (int i = 0; i < N; ++i) {
          sum += B[i][N - i - 1];
          B[i][N - i - 1] = 0;
        }
      }

      priority_queue <Node, vector<Node>, greater<Node>> pque;

      for (int y = 0; y < N; ++y) {
        int v = 0;
        for (int x = 0; x < N; ++x) {
          v += B[y][x];
        }
        pque.push(Node(y, v));
      }

      for (int i = 0; i < m && not pque.empty(); ++i) {
        Node node = pque.top();
        pque.pop();

        sum += node.v;
      }

      ans = min(ans, sum);
    }
  }

  cout << ans << endl;

  return 0;
}
0