Submission #2646832

Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;

#define NDEBUG
#ifdef DEBUG
#include "../cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))


ll solve(int N,int C,vvi& D, vvi& c) {
    // [mod][colors] = cnt
    vvi st(3, vi(C, 0));
    rep(i,N)rep(j,N){
        int mod = (i+j)%3;
        int color = c[i][j]-1;
        ++st[mod][color];
    }
    ll best = 0x7fffffffffffffffLL;
    rep(i,C) rep(j,C) rep(k,C){ //27000
        if (i==j || j==k || k==i) continue;
        ll score = 0;
        rep(from,C){
            score += st[0][from] * D[from][i];
            score += st[1][from] * D[from][j];
            score += st[2][from] * D[from][k];
        }
        // printf("(%d %d %d) score=%lld\n", i,j,k, score);
        best = min(best, score);
    }
    return best;
}

int main() {
    int N,C; cin >>N>>C;
    vvi D(C,vi(C));
    rep(i,C) {
        rep(j,C) cin >> D[i][j];
    }
    vvi c(N,vi(N));
    rep(i,N){
        rep(j,N) cin >> c[i][j];
    }
    cout << solve(N,C,D,c) << endl;
    return 0;
}

Submission Info

Submission Time
Task D - Good Grid
User naoya_t
Language C++14 (GCC 5.4.1)
Score 400
Code Size 1986 Byte
Status
Exec Time 49 ms
Memory 1280 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample_01.txt, sample_02.txt
All 400 / 400 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
01.txt 47 ms 1280 KB
02.txt 37 ms 1024 KB
03.txt 3 ms 256 KB
04.txt 14 ms 512 KB
05.txt 1 ms 256 KB
06.txt 49 ms 1280 KB
07.txt 47 ms 1280 KB
08.txt 48 ms 1280 KB
09.txt 12 ms 512 KB
10.txt 6 ms 384 KB
11.txt 9 ms 384 KB
12.txt 14 ms 640 KB
13.txt 45 ms 1152 KB
14.txt 38 ms 1152 KB
15.txt 47 ms 1280 KB
16.txt 21 ms 640 KB
sample_01.txt 1 ms 256 KB
sample_02.txt 1 ms 256 KB