結果

問題 No.5026 Spectrum Router
コンテスト
ユーザー fepic_
提出日時 2026-11-23 23:11:42
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1,980 ms / 2,000 ms
+ 268µs
コード長 8,337 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,923 ms
コンパイル使用メモリ 293,828 KB
実行使用メモリ 6,400 KB
スコア 1,428,479
最終ジャッジ日時 2026-11-23 23:19:11
合計ジャッジ時間 108,409 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge6_0
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'pll pointer(int)':
main.cpp:145:1: warning: control reaches end of non-void function [-Wreturn-type]
  145 | }
      | ^
main.cpp: In function 'S dir(int)':
main.cpp:154:1: warning: control reaches end of non-void function [-Wreturn-type]
  154 | }
      | ^
main.cpp: In function 'int id(int, int)':
main.cpp:168:1: warning: control reaches end of non-void function [-Wreturn-type]
  168 | }
      | ^
main.cpp: In function 'void solve()':
main.cpp:307:29: warning: 'temp' may be used uninitialized [-Wmaybe-uninitialized]
  307 |         if(Randouble() < exp((now_score - current_score) / temp)){
      |                          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:241:25: note: 'temp' was declared here
  241 |         double elapsed, temp;
      |                         ^~~~

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
namespace {
	const short N = 100;
	const short INF = 30'000;
	const short SZ = 8;
	unsigned int seed = 869'120;
	unsigned int Rand() { seed ^= seed << 13; seed ^= seed >> 17; seed ^= seed << 5; return (seed & 2147483647); }
	double Randouble() { return Rand() / 2147483648.0; }
	double GetTime() { return 1.0 * clock() / CLOCKS_PER_SEC; }
};
#define vvv(name, d, h, w, type, init)                    \
    std::vector<std::vector<std::vector<type>>> name((d), \
                                                     std::vector<std::vector<type>>((h), std::vector<type>((w), (init))))
#define all(v) v.begin(),v.end()
vector<int> f1 = {0, -1, 0, 1}, f2 = {-1, 0, 1, 0};  
#define PB push_back
#define mannhattan(x1, x2, y1, y2) (abs(x1 - x2) + abs(y1 - y2)) // マンハッタン距離 = |x1-x2|+|y1-y2|
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
# pragma GCC target("avx2")
# pragma GCC optimize("O3")
# pragma GCC optimize("unroll-loops")
#define vv(name, h, w, type, init) \
    std::vector<std::vector<type>> name((h), std::vector<type>((w), (init)))

    #define vc_cout(v)                   \
    do                               \
    {                                \
        int nn = v.size();            \
        for (int i = 0; i < nn; i++) \
        {                            \
            cout << v[i] << " ";     \
        }                            \
        cout << endl;                \
    } while (0)

template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
namespace inversion_impl {
    using S = long long;
    S op(S a, S b) { return a + b; }
    S e() { return 0; }
}
/**
 * @brief 転倒数(Inversion Number)を求める関数
 * 
 * @tparam T 配列の要素型
 * @param q 0 ~ n-1 の順列(または値の範囲が 0 <= x < n である要素数nの配列)
 * @return long long 転倒数
 */
template <typename T>
long long inversion_permutation(const std::vector<T>& q) {
    int n = static_cast<int>(q.size());
    if (n <= 1) return 0;

    // 区間和を管理する Point Add Range Sum セグメント木
    atcoder::segtree<inversion_impl::S, inversion_impl::op, inversion_impl::e> seg(n);
    
    long long inversions = 0;

    for (int i = 0; i < n; ++i) {
        
        int val = static_cast<int>(q[i]);
      //  cerr<<i<<" "<<val+1<<" "<<n<<endl;
        
        // val より厳密に大きい値(val + 1 から n - 1)の出現個数を加算
        inversions += seg.prod(val + 1, n);
        
        // val の出現カウントを +1 (初期値 0 に 1 を設定)
        seg.set(val, seg.get(val) + 1);
    }

    return inversions;
}

//転倒数
//適当にやればでる

template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
bool bfs_est(int xx1, int yy1, int hhh, int www)
{
    return (0 <= xx1 && xx1 < hhh && 0 <= yy1 && www > yy1);
}
using pll = pair<int, int>;
#define en cerr << "------------" << endl
namespace Timer{
    /*
    @FplusFplusF
    */
    chrono::steady_clock::time_point program_start,start;
    void program_start_snap(){//int mainのすぐそばに置く
        program_start=start=chrono::steady_clock::now();
        //Timer::program_start_snap();とおく
    }
    void snap(){//時間を、今にリセット
        start=chrono::steady_clock::now();
    }
    int get_ms(){//snap,program_start_snapのどっちかを読んでからの経過時間
        auto now=chrono::steady_clock::now();
        int ms=chrono::duration_cast<chrono::milliseconds>(now-start).count();
        return ms;
    }
    int get_ms_all_program(){//program_start_snap() を呼んでからのプログラム全体の経過時間
        auto now=chrono::steady_clock::now();
        int ms=chrono::duration_cast<chrono::milliseconds>(now-program_start).count();
        return ms;
    }
}

mt19937 mt;
uint32_t rand_int(uint32_t r){  //[0,r)
    assert(r!=0);
    return ((uint64_t)mt()*r)>>32;
}
int rand_int(int l,int r){  //[l,r)
    assert(l<r);
    return l+rand_int(r-l);
}
    int n = 64;
        int m = 256;
pll pointer(int i){
    if(0<=i&&i<n)return {-1,i};
    if(n<=i&&i<2*n)return {i-n,n};
    if(2*n<=i&&i<3*n)return {n,3*n-1-i};
    if(3*n<=i&&i<4*n)return {4*n-1-i,-1};
}
struct S{
    int x,y;
};
S dir(int i){
    if(0<=i&&i<n)return {1,0};
    if(n<=i&&i<2*n)return {0,-1};
    if(2*n<=i&&i<3*n)return {-1,0};
    if(3*n<=i&&i<4*n)return {0,1}; 
}
int id(int x, int y){
    if(x == -1){
        return y;
    }
    if(y == n){
        return n + x;
    }
    if(x == n){
        return 2 * n + (n - 1 - y);
    }
    if(y == -1){
        return 3 * n + (n - 1 - x);
    }
}
struct state{
    int s[64][64];

    void init(){
        rep(i,n){
            rep(j,n){
                s[i][j] = 0;
            }
        }
    }
};

long long score(vector<long long> &ss){
    int cnt = 0;
    rep(i,m){
        cnt += (i+1) * ss[i];
    }
    return cnt;
}
void solve(){

    Timer::program_start_snap();
    	
    cin>>n;

    vector<int> a(m);
    rep(i,m){
        cin>>a[i];
        a[i]--;
    }

    state s;
    s.init();
    vvv(dp,n,n,4,int,0);

    vector<long long>b(m);
    rep(i,m){
        //地点iからスタート
        auto [x,y] = pointer(i);
        S dire = dir(i);
        int _ = 0;
        while(1){
            // cout<<x<<" "<<y<<endl;
            if(!bfs_est(x,y,n,n) && (_++)){
                //盤面外に出た時
                b[id(x,y)] = a[i]; 
                break;
            }

            x += dire.x;
            y += dire.y;

            if(s.s[x][y] == 1){
                int tmp1 = dire.x,tmp2 = dire.y;
                dire.x = tmp2*-1;
                dire.y = tmp1*-1;
            }
            if(s.s[x][y] == 2){
                int tmp1 = dire.x,tmp2 = dire.y;
                dire.x = tmp2;
                dire.y = tmp1;
            }
        }
    }

    cerr<<score(b)<<endl;

    const double TL = 1975;
      vector<long long> save_b;
	int loops = 0;
	int cnt1 = 0, cnt2 = 0;
    double start_time = Timer::get_ms_all_program();
	double elapsed, temp;

    int current_score = score(b);
    int loop = 0;
    int max_score = current_score;
    vector<int> is(m,-1);
    while(1){
                int x1 = rand_int(0,n);
        int y1 = rand_int(0,n);
        int now = s.s[x1][y1];

        int henka = rand_int(0,3);
        if(now == henka){
           continue;
        }
		loops += 1;
		if ((loops & 511) == 0) {
			elapsed = (Timer::get_ms_all_program() - start_time) / TL;
			temp = 5 / pow(5.0, elapsed);
			if (elapsed > 1.0) break;
// 
            
		}
        
        //cerr<<current_score<<" "<<max_score<<endl;


        //change
        s.s[x1][y1] = henka;
      save_b = b;

    rep(i,m){
        if(is[i] == loops)continue;
        //地点iからスタート
        auto [x,y] = pointer(i);
        S dire = dir(i);
        int _ = 0;
        while(1){
            // cout<<x<<" "<<y<<endl;
            if(!bfs_est(x,y,n,n) && (_++)){
                //盤面外に出た時
                b[id(x,y)] = a[i]; 
                b[i] = a[id(x,y)];
                is[id(x,y)] = loops;
                is[i] = loops;
                break;
            }

            x += dire.x;
            y += dire.y;

            if(s.s[x][y] == 1){
                int tmp1 = dire.x,tmp2 = dire.y;
                dire.x = tmp2*-1;
                dire.y = tmp1*-1;
            }
            if(s.s[x][y] == 2){
                int tmp1 = dire.x,tmp2 = dire.y;
                dire.x = tmp2;
                dire.y = tmp1;
            }
        }
    }

        int now_score = score(b);
        chmax(max_score,now_score);
        if(Randouble() < exp((now_score - current_score) / temp)){
            current_score = now_score;

            
            
            //採用
        }
        else{
            b = save_b;
            s.s[x1][y1] = now;
        }
    }

    rep(i,n){
        rep(j,n){
            cout<<s.s[i][j];
        }
        cout<<endl;
    }

cerr<<current_score<<" " <<max_score<<endl; 
cerr<<loops<<endl;
}

int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);

    solve();
}

0