結果

問題 No.1345 Beautiful BINGO
ユーザー LayCurseLayCurse
提出日時 2021-01-16 13:10:11
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 4,403 bytes
コンパイル時間 2,566 ms
コンパイル使用メモリ 216,880 KB
最終ジャッジ日時 2025-01-17 21:41:54
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
  walloc1d(arr, x2-x1, mem);
  (*arr) -= x1;
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
  sort(a, a+N);
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
int N;
int M;
int A[16][16];
int b[16][16];
int c[16];
int main(){
  int mask;
  wmem = memarr;
  int res = 1073709056;
  int tmp;
  int ok;
  rd(N);
  rd(M);
  {
    int Lj4PdHRW;
    int KL2GvlyY;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      for(KL2GvlyY=(0);KL2GvlyY<(N);KL2GvlyY++){
        rd(A[Lj4PdHRW][KL2GvlyY]);
      }
    }
  }
  for(mask=(0);mask<(1<<(N+2));mask++){
    int i;
    tmp = 0;
    ok = 0;
    for(i=(0);i<(N+2);i++){
      if(((mask) &(1<<(i)))){
        ok++;
      }
    }
    for(i=(0);i<(N);i++){
      int j;
      for(j=(0);j<(N);j++){
        b[i][j] = A[i][j];
      }
    }
    for(i=(0);i<(N);i++){
      if(((mask) &(1<<(i)))){
        int j;
        for(j=(0);j<(N);j++){
          tmp += b[i][j];
          b[i][j] = 0;
        }
      }
    }
    if(((mask) &(1<<(N)))){
      for(i=(0);i<(N);i++){
        tmp += b[i][i];
        b[i][i] = 0;
      }
    }
    if(((mask) &(1<<(N+1)))){
      for(i=(0);i<(N);i++){
        tmp += b[N-1-i][i];
        b[N-1-i][i] = 0;
      }
    }
    for(i=(0);i<(N);i++){
      {
        int jPV_0s1p;
        remove_reference<decltype(b[jPV_0s1p][i])>::type BUotOFBp;
        int Q5rsz4fz = 0;
        if((0) > ((N)-1)){
          BUotOFBp = 0;
        }
        else{
          for(jPV_0s1p = 0; jPV_0s1p <= (N)-1; jPV_0s1p++){
            if(Q5rsz4fz == 0){
              BUotOFBp = b[jPV_0s1p][i];
              Q5rsz4fz = 1;
              continue;
            }
            BUotOFBp += b[jPV_0s1p][i];
          }
        }
        c[i] =BUotOFBp;
      }
    }
    sortA_L(N, c);
    for(i=(0);i<(M-ok);i++){
      tmp += c[i];
    }
    chmin(res, tmp);
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay version 20210103-1 [bug fixed 1]

// --- original code ---
// int N, M, A[16][16], b[16][16], c[16];
// {
//   int res = int_inf, tmp, ok;
//   rd(N,M,A(N,N));
//   rep(mask,1<<(N+2)){
//     tmp = 0;
//     ok = 0;
//     rep(i,N+2) if(BIT_ith(mask,i)) ok++;
//     rep(i,N) rep(j,N) b[i][j] = A[i][j];
//     rep(i,N) if(BIT_ith(mask,i)) rep(j,N) tmp += b[i][j], b[i][j] = 0;
//     if(BIT_ith(mask,N)) rep(i,N) tmp += b[i][i], b[i][i] = 0;
//     if(BIT_ith(mask,N+1)) rep(i,N) tmp += b[N-1-i][i], b[N-1-i][i] = 0;
//     rep(i,N) c[i] = sum[j,0,N](b[j][i]);
//     sortA(N, c);
//     rep(i, M-ok) tmp += c[i];
//     res <?= tmp;
//   }
//   wt(res);
// }
0