結果

問題 No.1345 Beautiful BINGO
ユーザー sakasu1sakasu1
提出日時 2021-01-23 14:50:12
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 438 ms / 2,000 ms
コード長 1,272 bytes
コンパイル時間 2,016 ms
コンパイル使用メモリ 172,564 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-30 19:41:26
合計ジャッジ時間 10,288 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main(){
  int i,j,k,n,m;
  cin >> n >> m;
  int a[17][17];
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      cin >> a[i][j];
    }
  }
  int ans=1000000000;
  for(i=0;i<(1<<(n+2));i++){
    int b[17][17];
    for(j=0;j<n;j++){
      for(k=0;k<n;k++){
        b[j][k]=0;
      }
    }
    int sum=0,count=0;
    for(j=0;j<n;j++){
      if(!(i&(1<<j))){
        continue;
      }
      count++;
      for(k=0;k<n;k++){
        sum+=a[j][k],b[j][k]=1;
      }
    }
    if(i&(1<<n)){
      count++;
      for(j=0;j<n;j++){
        if(b[j][j]==1){
          continue;
        }
        sum+=a[j][j],b[j][j]=1;
      }
    }
    if(i&(1<<(n+1))){
      count++;
      for(j=0;j<n;j++){
        if(b[j][n-j-1]==1){
          continue;
        }
        sum+=a[j][n-j-1],b[j][n-j-1]=1;
      }
    }
    if(count>=m){
      ans=min(ans,sum);
      continue;
    }
    vector<int> x;
    for(j=0;j<n;j++){
      int sum1=0;
      for(k=0;k<n;k++){
        if(b[k][j]==0){
          sum1+=a[k][j],b[k][j]=1;
        }
      }
      x.push_back(sum1);
    }
    sort(x.begin(),x.end());
    for(j=0;j<m-count;j++){
      sum+=x[j];
    }
    ans=min(ans,sum);
  }
  cout << ans << endl;
}
0