結果

問題 No.5026 Spectrum Router
コンテスト
ユーザー kaede2020
提出日時 2026-11-26 19:58:28
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1,852 ms / 2,000 ms
+ 387µs
コード長 5,915 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,464 ms
コンパイル使用メモリ 357,224 KB
実行使用メモリ 9,836 KB
スコア 1,414,957
最終ジャッジ日時 2026-11-26 20:00:13
合計ジャッジ時間 104,475 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge5_0
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>      // cout, endl, cin
#include <string>        // string, to_string, stoi
#include <vector>        // vector
#include <algorithm>     // min, mai, swap, sort, reverse, lower_bound, upper_bound
#include <utility>       // pair, make_pair
#include <tuple>         // tuple, make_tuple
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <deque>         // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset>        // bitset
#include <cctype>        // isupper, islower, isdigit, toupper, tolower
#include <iomanip>       // fiied,setprecision
#include <limits.h>      // INT_MAi
#include <math.h>        // M_PI
#include <random>        // random_device, mt19937, uniform_int_distribution
#include <regex>         // 正規表現
#include <time.h>        // time
#include <fstream>       // ifstream, ofstream
#include <array>         // array
#include <bit>           // bit
#include <chrono>        // chrono
#include <span>          // span
#include <cmath>         // sqrt, sin, cos, tan, asin, acos, atan, atan2
#include <complex>       // 複素数
// #include <bits/stdc++.h>
using namespace std;

#include <atcoder/all>
using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;

using ll = long long;
using ull = unsigned long long;
using ld = long double; // 80bit 18桁

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const string DIR = "URDL";
const int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy8[8] = {1, 1, 0, -1, -1, -1, 0, 1};

// INFに3以上を掛ける、またはint(INF)に1以上を足すとオーバーフローする
// 4611686016279904255 cf: LLONG_MAi=9223372036854775807
// const ll INF = (1ll << 62) - (1ll << 31) - 1ll;
// using P=pair<ll,ll>;
const int INF = 1001001001;
//-----------------------------------------------------

const int N = 64;
const int M = 256;
vector<int> a(M),b(M);
int calc_score(const vector<string>& out)
{
    rep(i, M) {
        int x, y, dir;
        if (i < N) {
            x = 0;
            y = i;
            dir = 2;  // 下
        } else if (i < 2 * N) {
            x = i - N;
            y = N - 1;
            dir = 3;  // 左
        } else if (i < 3 * N) {
            x = N - 1;
            y = 3 * N - 1 - i;
            dir = 0;  // 上
        } else {
            x = 4 * N - 1 - i;
            y = 0;
            dir = 1;  // 右
        }

        // 方向の順番は U, R, D, L
        constexpr int slash[4] = {1, 0, 3, 2};
        constexpr int backslash[4] = {3, 2, 1, 0};

        while (0 <= x && x < N && 0 <= y && y < N) {
            if (out[x][y] == '1') {
                dir = slash[dir];
            } else if (out[x][y] == '2') {
                dir = backslash[dir];
            }
            x += dx[dir];
            y += dy[dir];
        }

        int exit;
        if (x < 0) {
            exit = y;
        } else if (x >= N) {
            exit = 3 * N - 1 - y;
        } else if (y < 0) {
            exit = 4 * N - 1 - x;
        } else {
            exit = N + x;
        }
        b[exit] = a[i];
    }

    int inversions = 0;
    rep(i, M) {
        for (int j = i + 1; j < M; ++j) {
            if (b[i] > b[j]) ++inversions;
        }
    }
    return M * (M - 1) / 2 - inversions;
}
vector<string> solve()
{
    //cerr << "Starting solve" << endl;
    vector<string> out(N),best_out(N);
    rep(i, N)
    {
        out[i].resize(N);
        best_out[i].resize(N);
        rep(j, N)
        {
            out[i][j] = '0';
            best_out[i][j] = '0';
        }
    }
    vector<bool>tate(N, false), yoko(N, false);
    vector<int>order_tate(N), order_yoko(N);
    rep(i,N){
        order_tate[i] = i;
        order_yoko[i] = i;
    }
    shuffle(order_tate.begin(), order_tate.end(), mt19937(random_device()()));
    shuffle(order_yoko.begin(), order_yoko.end(), mt19937(random_device()()));
    rep(i,N){
        out[order_tate[i]][order_yoko[i]] = '1';
        best_out[order_tate[i]][order_yoko[i]] = '1';
    }
    //山登りでランダムに1と2を変更する
    //山登りでランダムにマスの位置をswapする
    //1を追加する
    //2を追加する
    //1,2を削除する
    int TIME_LIMIT = 1.85*CLOCKS_PER_SEC; // 適当な時間制限を設定
    int best = calc_score(out);
    cerr << "Initial best score: " << best << endl;
    int ti=clock();
    while(clock() - ti < TIME_LIMIT){

        int i = rand() % N;
        int j = rand() % N;
        int old=out[i][j]-'0';
        int type=rand()%3;
        while(type==old) type=rand()%3;
        out[i][j] = '0' + type;

        //cerr << "Trying type " << type << " with positions (" << i << "," << j << ") and (" << i2 << "," << j2 << ")" << endl;

        int score = calc_score(out);
        //cerr << "Current score: " << score << endl;
        if(score >= best){
            cerr << "New best score: " << score << endl;
            best = score;
            best_out = out;
        }else{
            // 元に戻す
            out[i][j] = '0' + old; // 元に戻す
        }
        //cerr<<__LINE__<<"-----"<<endl;
    }
    //cerr<<__LINE__<<"-----"<<out.size()<<endl;
    //for(int i = 0; i < N; i++){
    //    cerr << best_out[i] << endl;
    //}
    cerr<<"best score: " << best << endl;
    return best_out;
}
int main()
{
    int n;
    cin >> n;
    rep(i, M) cin >> a[i];
    vector<string> out = solve();
    //cerr<<__LINE__<<"-----"<<endl;
    rep(i, N) cout << out[i] << endl;
    cerr << "Best score: " << calc_score(out) << endl;
    //rep(i,M)cerr<<"a["<<i<<"]="<<a[i]<<" b["<<i<<"]="<<b[i]<<endl;
    return 0;
}
0