// ============== main.cpp ============== #pragma GCC optimize("O3") // library url: https://atcoder.jp/contests/intro-heuristics/submissions/80090780 // ============== define.hpp ============== #include // ============== library/timer.hpp ============== #include namespace library{ class Timer { private: std::chrono::steady_clock::time_point start_time; double scale = 1.0; public: Timer() : start_time(std::chrono::steady_clock::now()) {} void reset() { start_time = std::chrono::steady_clock::now(); } void set_scale(double s) { scale = s; } double elapsed() const { auto now = std::chrono::steady_clock::now(); std::chrono::duration diff = now - start_time; return diff.count() * scale; } }; } // ============== End of library/timer.hpp ============== // ============== library/rng.hpp ============== #include #include #include #include #include #include #include #include namespace library { // ============================================================ // utilities // ============================================================ constexpr uint64_t rotl64(uint64_t x, int k) noexcept { return (x << k) | (x >> (64 - k)); } constexpr uint64_t splitmix64_next(uint64_t& x) noexcept { x += 0x9E3779B97F4A7C15ULL; uint64_t z = x; z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL; z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL; return z ^ (z >> 31); } inline uint64_t random_chrono() { uint64_t t = static_cast( std::chrono::high_resolution_clock::now().time_since_epoch().count()); return splitmix64_next(t); } // ============================================================ // base class / concept // ============================================================ struct rng_base_u64 { using result_type = uint64_t; static constexpr result_type min() noexcept { return 0; } static constexpr result_type max() noexcept { return std::numeric_limits::max(); } }; // ============================================================ // xorplus // - 超低品質、超高速乱数 // ============================================================ class xorplus : public rng_base_u64 { private: uint64_t x1 = 0; uint64_t x2 = 0; public: constexpr explicit xorplus(uint64_t seed = 1) noexcept { this->seed(seed); } constexpr void seed(uint64_t seed_val) noexcept { uint64_t x = seed_val; x1 = splitmix64_next(x); x2 = splitmix64_next(x); if (x1 == 0 && x2 == 0) { seed(seed_val + 1); } } constexpr result_type operator()() noexcept { x1 += x2; x2 ^= x1; return x1; } }; // ============================================================ // xoroplus // - 中低品質(?) 、高速 // - xorplusと比べれば大幅に高品質 (randint3テスト、ベルヌーイテストともに良好) // ============================================================ class xoroplus : public rng_base_u64 { private: uint64_t x1 = 0; uint64_t x2 = 0; public: constexpr explicit xoroplus(uint64_t seed = 1) noexcept { this->seed(seed); } constexpr void seed(uint64_t seed_val) noexcept { uint64_t x = seed_val; x1 = splitmix64_next(x); x2 = splitmix64_next(x); if (x1 == 0 && x2 == 0) { seed(seed_val + 1); } } constexpr result_type operator()() noexcept { x1 += rotl64(x2, 17); x2 ^= x1; return x1; } }; // ============================================================ // xoshiro256 // - 高速で品質もそこそこな乱数 // ============================================================ class xoshiro256 : public rng_base_u64 { private: uint64_t s_[4]{}; public: constexpr explicit xoshiro256(uint64_t seed = 1) noexcept { this->seed(seed); } constexpr void seed(uint64_t seed_val) noexcept { uint64_t x = seed_val; s_[0] = splitmix64_next(x); s_[1] = splitmix64_next(x); s_[2] = splitmix64_next(x); s_[3] = splitmix64_next(x); if (s_[0] == 0 && s_[1] == 0 && s_[2] == 0 && s_[3] == 0) { s_[3] = 1; } } constexpr result_type operator()() noexcept { step(); return s_[0]; } constexpr void step() noexcept { const uint64_t t = s_[1] << 17; s_[2] ^= s_[0]; s_[3] ^= s_[1]; s_[1] ^= s_[2]; s_[0] ^= s_[3]; s_[2] ^= t; s_[3] = rotl64(s_[3], 45); } constexpr uint64_t get(int i) const noexcept { return s_[i]; } }; // ============================================================ // xoshiro256++ // - 高品質な乱数(ただしやや遅い) // ============================================================ class xoshiro256pp : public rng_base_u64 { private: uint64_t s_[4]{}; public: constexpr explicit xoshiro256pp(uint64_t seed = 1) noexcept { this->seed(seed); } constexpr void seed(uint64_t seed_val) noexcept { uint64_t x = seed_val; s_[0] = splitmix64_next(x); s_[1] = splitmix64_next(x); s_[2] = splitmix64_next(x); s_[3] = splitmix64_next(x); if (s_[0] == 0 && s_[1] == 0 && s_[2] == 0 && s_[3] == 0) { seed(seed_val + 1); } } constexpr result_type operator()() noexcept { const uint64_t result = rotl64(s_[0] + s_[3], 23) + s_[0]; const uint64_t t = s_[1] << 17; s_[2] ^= s_[0]; s_[3] ^= s_[1]; s_[1] ^= s_[2]; s_[0] ^= s_[3]; s_[2] ^= t; s_[3] = rotl64(s_[3], 45); return result; } }; // ============================================================ // randint helpers // - 64bit RNG 専用 // ============================================================ inline int randint(int r, uint64_t rng_val) { return static_cast((static_cast(rng_val >> 32) * static_cast(r)) >> 32); } inline int randint(int l, int r, uint64_t rng_val) { return l + randint(r - l, rng_val); } inline std::pair randint_pair(int r, uint64_t rng_val) { const uint64_t v = rng_val; return { static_cast(((v >> 32) * static_cast(r)) >> 32), static_cast(((v & 0xFFFFFFFFULL) * static_cast(r)) >> 32) }; } inline std::pair randint_pair(int l, int r, uint64_t rng_val) { const uint64_t v = rng_val; const uint64_t d = static_cast(r - l); return { l + static_cast(((v >> 32) * d) >> 32), l + static_cast(((v & 0xFFFFFFFFULL) * d) >> 32) }; } inline std::tuple randint3(int r, uint64_t rng_val) { const uint64_t v1 = rng_val >> 42; const uint64_t v2 = (rng_val >> 21) & 0x1FFFFFULL; const uint64_t v3 = rng_val & 0x1FFFFFULL; const uint64_t rr = static_cast(r); return { static_cast((v1 * rr) >> 22), static_cast((v2 * rr) >> 21), static_cast((v3 * rr) >> 21) }; } inline std::tuple randint3(int l, int r, uint64_t rng_val) { const uint64_t d = static_cast(r - l); const uint64_t v1 = rng_val >> 42; const uint64_t v2 = (rng_val >> 21) & 0x1FFFFFULL; const uint64_t v3 = rng_val & 0x1FFFFFULL; return { l + static_cast((v1 * d) >> 22), l + static_cast((v2 * d) >> 21), l + static_cast((v3 * d) >> 21) }; } } // namespace library // ============== End of library/rng.hpp ============== using namespace std; #define rep1(a) for (int _ = 0; _ < (a); ++_) #define rep2(i, a) for (int i = 0; i < (a); ++i) #define rep3(i, a, b) for (int i = a; i < (b); ++i) #define rrep1(a) for (int i = (a) - 1; i >= 0; --i) #define rrep2(i, a) for (int i = (a) - 1; i >= 0; --i) #define rrep3(i, a, b) for (int i = (b) - 1; i >= (a); --i) #define overload3(a, b, c, d, ...) d #define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) #define rrep(...) overload3(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__) #define in(i, vec) for (auto i : (vec)) template using pqueue = priority_queue>; // 大きい順 template using pqueue_g = priority_queue, greater>; // 小さい順 using i8=int8_t; using i16=int16_t; using i32=int32_t; using i64=int64_t; using u8=uint8_t; using u16=uint16_t; using u32=uint32_t; using u64=uint64_t; library::Timer timer; uniform_real_distribution rnd(0.0, 1.0); normal_distribution randn(0.0, 1.0); library::xoshiro256 rng(12345); // ============== End of define.hpp ============== #include // ============== library/sa.hpp ============== #include #include #include #include #include #include namespace library{ template struct SAScheduler { private: using Clock = std::chrono::steady_clock; using Limit = std::conditional_t; inline static double log_table[65536]; Clock::time_point start; double t1, t2, b, temperature; Limit limit; uint64_t itr = 0, freq; bool update() { double progress; if constexpr (TimeBased) { double elapsed = std::chrono::duration(Clock::now() - start).count(); if (elapsed >= limit) return false; progress = elapsed / limit; } else { if (itr >= limit) return false; progress = limit > 1 ? double(itr) / double(limit - 1) : 0.0; } double t_exp = t1 * std::pow(t2 / t1, progress); double t_lin = t1 + (t2 - t1) * progress; temperature = std::pow(t_exp, b) * std::pow(t_lin, 1.0 - b); return true; } public: // limit_: 時間ベースなら秒数、回数ベースなら反復回数 SAScheduler(double start_temp_, double end_temp_, Limit limit_, double balance_ = 0.5, uint64_t update_freq_ = 256, uint64_t seed = 99999) : t1(start_temp_), t2(end_temp_), b(balance_), temperature(t1), limit(limit_), freq(std::max(uint64_t(1), update_freq_)) { for (int i = 0; i < 65536; ++i) log_table[i] = std::log((1.0 + i) / 65536); std::mt19937_64 shuffle_rng(seed); std::shuffle(log_table, log_table + 65536, shuffle_rng); if constexpr (TimeBased) start = Clock::now(); } struct Sentinel {}; struct Iterator { SAScheduler* s; bool running; uint64_t remaining; double operator*() const { return s->temperature * log_table[s->itr & 65535]; } Iterator& operator++() { ++s->itr; if constexpr (!TimeBased) { if (s->itr >= s->limit) { running = false; return *this; } } bool refresh = (--remaining == 0); if constexpr (!TimeBased) { // 最後の反復では終了温度を使う refresh |= (s->itr == s->limit - 1); } if (refresh) { running = s->update(); remaining = s->freq; } return *this; } bool operator!=(Sentinel) const { return running; } }; Iterator begin() { return {this, update(), freq}; } Sentinel end() const { return {}; } uint64_t iterations() const { return itr; } double current_temperature() const { return temperature; } }; } // namespace library // ============== End of library/sa.hpp ============== constexpr int N=64; constexpr int M=256; array A; void get_input(){ int n; cin>>n;assert(n==N); rep(i,M){ int a; cin>>a;A[i]=a-1; } } using Board = array,N>; // ULDR; int dx[4]={-1,0,1,0}; int dy[4]={0,-1,0,1}; char BOARD_TO_CHAR[4]={'0','2','@','1'}; tuple mid_to_pos(int i){ if(i get_B(const Board& board){ array B; B.fill(-1); rep(i,M){ int a = A[i]; auto [x,y,d] = mid_to_pos(i); while((unsigned)x>6, j=b&63; int sum = (y>>(8*i)&255) + __builtin_popcountll(x[i]&((1ULL< lid[N][N]; array B; void compute_light(int i){ int a = A[i]; auto [x,y,d] = mid_to_pos(i); while((unsigned)x bit(M); int score = 0; rep(i,M){ int b = B[i]; score += bit.sum(0,b); bit.add(b,1); } return score; } void change_board(int x,int y, int c){ board[x][y]=c; auto pre = lid[x][y]; for(int i:pre) compute_light(i); } }; int get_score(const array& B){ // atcoder::fenwick_tree bit(M); BIT256 bit{}; int score = 0; rep(i,M){ int b = B[i]; // score += bit.sum(0,b); // bit.add(b,1); score += bit.sum_add(b); } return score; }; void output(const Board& board){ string out; rep(i,N)rep(j,N){ out+=BOARD_TO_CHAR[board[i][j]]; if(j+1==N)out+='\n'; } cout< sa(1e0,1e-2,1.95,0.8); int itr = 0; const int CM = 5; for(double add:sa){ ++itr; int x = rng()%N, y=rng()%N, c=rng()%CM; if(c!=1 && c!=3)c=0; while(state.board[x][y]==c){ x = rng()%N, y=rng()%N, c=rng()%CM; if(c!=1 && c!=3)c=0; } int pc = state.board[x][y]; state.change_board(x,y,c); int new_score = state.get_score(); if(new_score >= score+add){ score = new_score; if(score > best_score){ best_score = score; best_state = state; } }else{ state.change_board(x,y,pc); } } output(best_state.board); cerr << "best score " << best_score << endl; } // ============== End of main.cpp ==============