結果
| 問題 | No.5026 Spectrum Router |
| コンテスト | |
| ユーザー |
炭素(breso)
|
| 提出日時 | 2026-11-23 22:53:39 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 1,891 ms / 2,000 ms |
| + 437µs | |
| コード長 | 11,340 bytes |
| 記録 | |
| コンパイル時間 | 3,863 ms |
| コンパイル使用メモリ | 386,628 KB |
| 実行使用メモリ | 9,904 KB |
| スコア | 1,212,737 |
| 最終ジャッジ日時 | 2026-11-23 23:00:48 |
| 合計ジャッジ時間 | 104,576 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge6_0 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
コンパイルメッセージ
main.cpp: In function 'void solve()':
main.cpp:255:9: warning: 'a' may be used uninitialized [-Wmaybe-uninitialized]
255 | if(a==b || a==c || a==d || b==c || b==d || c==d) continue;
| ^~
main.cpp:217:13: note: 'a' was declared here
217 | int a,b,c,d;
| ^
main.cpp:255:9: warning: 'b' may be used uninitialized [-Wmaybe-uninitialized]
255 | if(a==b || a==c || a==d || b==c || b==d || c==d) continue;
| ^~
main.cpp:217:15: note: 'b' was declared here
217 | int a,b,c,d;
| ^
main.cpp:255:17: warning: 'c' may be used uninitialized [-Wmaybe-uninitialized]
255 | if(a==b || a==c || a==d || b==c || b==d || c==d) continue;
| ~~~~~^~~~~~~
main.cpp:217:17: note: 'c' was declared here
217 | int a,b,c,d;
| ^
main.cpp:255:25: warning: 'd' may be used uninitialized [-Wmaybe-uninitialized]
255 | if(a==b || a==c || a==d || b==c || b==d || c==d) continue;
| ~~~~~~~~~~~~~^~~~~~~
main.cpp:217:19: note: 'd' was declared here
217 | int a,b,c,d;
| ^
ソースコード
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#include <atcoder/fenwicktree>
using namespace atcoder;
using ll = long long;
using vecint = std::vector<int>;
using vecll = std::vector<long long>;
using vecstr = std::vector<string>;
using vecbool = std::vector<bool>;
using vecdou = std::vector<double>;
using vecpl = std::vector<pair<ll,ll>>;
using vec2d = std::vector<vecint>;
using pl = pair<long long,long long>;
using pint = pair<int,int>;
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define rep1(i,n) for (int i = 1; i <= (int)(n); i++)
#define all(a) (a).begin(), (a).end()
#define INF 1000000000
#define inr(l,x,r) ((l) <= (x) && (x) < (r))
class TimeKeeperDouble {
private:
std::chrono::high_resolution_clock::time_point start_time_;
double time_threshold_;
double now_time_ = 0;
public:
// 時間制限をミリ秒単位で指定してインスタンスをつくる。
TimeKeeperDouble(const double time_threshold)
: start_time_(std::chrono::high_resolution_clock::now()),
time_threshold_(time_threshold) {}
// 経過時間をnow_time_に格納する。
void setNowTime() {
auto diff = std::chrono::high_resolution_clock::now() - this->start_time_;
this->now_time_ =
std::chrono::duration_cast<std::chrono::microseconds>(diff).count() *
1e-3; // ms
}
void changeTimeThreshold(const double time_threshold) {
this->time_threshold_ = time_threshold;
}
// 経過時間をnow_time_に取得する。
double getNowTime() const { return this->now_time_; }
// インスタンス生成した時から指定した時間制限を超過したか判定する。
bool isTimeOver() const { return now_time_ >= time_threshold_; }
};
#include <bits/stdc++.h>
using namespace std;
class Random {
private:
uint32_t x2_;
uint32_t x3_;
uint64_t c_x1_;
static uint64_t splitmix64(uint64_t& x) {
x += 0x9e3779b97f4a7c15ULL;
uint64_t z = x;
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
return z ^ (z >> 31);
}
public:
// seedを指定して初期化
Random(const uint64_t seed = 0) { setSeed(seed); }
inline void setSeed(const uint64_t seed = 0) {
if (seed == 0) {
x2_ = 12345U;
x3_ = 0xcafef00dU;
c_x1_ = (0xd15ea5e5ULL << 32) | 23456ULL;
return;
}
uint64_t x = seed;
x2_ = static_cast<uint32_t>(splitmix64(x));
x3_ = static_cast<uint32_t>(splitmix64(x));
c_x1_ = splitmix64(x);
}
inline uint32_t next() {
const uint64_t x = static_cast<uint64_t>(x3_) * 3487286589ULL;
const uint32_t ret =
(x3_ ^ x2_) +
(static_cast<uint32_t>(c_x1_) ^ static_cast<uint32_t>(x >> 32));
x3_ = x2_;
x2_ = static_cast<uint32_t>(c_x1_);
c_x1_ = x + (c_x1_ >> 32);
return ret;
}
inline uint64_t next64() {
return (static_cast<uint64_t>(next()) << 32) | next();
}
inline double nextf() {
const uint64_t bits = 0x3ff0000000000000ULL |
(static_cast<uint64_t>(next()) << 20);
double ret;
memcpy(&ret, &bits, sizeof(ret));
return ret - 1.0;
}
inline size_t get(const size_t n) {
assert(0 < n && n <= static_cast<size_t>(UINT32_MAX));
return (static_cast<uint64_t>(next()) * n) >> 32;
}
inline size_t range(const size_t a, const size_t b) {
assert(a < b);
return get(b - a) + a;
}
inline size_t range_skip(const size_t a, const size_t b, const size_t skip) {
assert(a <= skip && skip < b);
const size_t n = range(a, b - 1);
return n + (skip <= n);
}
inline int64_t rangei(const int64_t a, const int64_t b) {
assert(a < b);
return static_cast<int64_t>(get(static_cast<size_t>(b - a))) + a;
}
template <typename T>
inline void shuffle(vector<T>& a) {
for (size_t i = a.size(); i > 1; --i) {
swap(a[i - 1], a[get(i)]);
}
}
// 0以上m未満の整数の範囲の乱数
inline int nextInt(const int m) {
assert(0 < m);
return static_cast<int>(get(static_cast<size_t>(m)));
}
// 0以上1.0未満の実数の範囲の乱数
inline double nextDouble() { return nextf(); }
// 0以上1.0未満の実数の範囲の乱数のlog。焼きなまし法で使いやすい。
inline double nextLog() { return log(nextDouble()); }
inline uint32_t nextUInt32() { return next(); }
};
Random rnd(1019);
const int N=64;
vecint A(4*N);
void input() {
int tmp;
cin >> tmp;
for (int i=0; i<4*N; i++) {
cin >> A[i];
}
}
void solve() {
vector<vector<int>> U(N,vecint(N)), D(N,vecint(N)), L(N,vecint(N)), R(N,vecint(N));
vector<int> B(4*N);
rep(i,N) {
U[0][i] = i;
D[N-1][i] = 3*N-1-i;
L[i][0] = 4*N-1-i;
R[i][N-1] = N+i;
}
vector<vector<int>> ans(N,vecint(N,0));
rep(i,N) {
rep(j,N-1) {
L[i][j+1] = L[i][j];
U[j+1][i] = U[j][i];
}
for (int j=N-1; j>0; j--) {
R[i][j-1] = R[i][j];
D[j-1][i] = D[j][i];
}
}
rep(i,N) {
B[i] = A[D[0][i]];
B[N+i] = A[L[i][N-1]];
B[2*N+i] = A[U[N-1][N-1-i]];
B[3*N+i] = A[R[N-1-i][0]];
}
ll cost = 0;
fenwick_tree<int> ft(4*N+1);
// Bの転倒数
for(int i=0;i<4*N;i++){
cost += i-ft.sum(0,B[i]);
ft.add(B[i],1);
}
double T0 = 50.0;
double T1 = 0.1;
TimeKeeperDouble tk(1900.0);
auto best_ans = ans;
int best_cost = cost;
int itr = 1;
double T = T0;
while (itr%256 || !tk.isTimeOver()) {
++itr;
if(itr%256==0) {
tk.setNowTime();
T = T0 * pow(T1/T0,tk.getNowTime()/1900.0);
}
int x = rnd.nextInt(N);
int y = rnd.nextInt(N);
int dt = rnd.nextInt(2) + 1;
int original = ans[x][y];
int new_type = (original+dt)%3;
int a,b,c,d;
if(original == 0){
if(new_type == 1){
a = U[x][y];
b = D[x][y];
c = R[x][y];
d = L[x][y];
}else if(new_type == 2){
a = U[x][y];
b = D[x][y];
c = L[x][y];
d = R[x][y];
}
}else if(original == 1){
if(new_type == 0){
a = U[x][y];
b = L[x][y];
c = R[x][y];
d = D[x][y];
}else if(new_type == 2){
a = U[x][y];
b = L[x][y];
c = D[x][y];
d = R[x][y];
}
}else if(original == 2){
if(new_type == 0){
a = U[x][y];
b = R[x][y];
c = L[x][y];
d = D[x][y];
}else if(new_type == 1){
a = U[x][y];
b = R[x][y];
c = D[x][y];
d = L[x][y];
}
}
if(a==b || a==c || a==d || b==c || b==d || c==d) continue;
int cost_diff = 0;
if(a>c) swap(a,c);
if(b>d) swap(b,d);
for(int i=a+1;i<c;i++){
if(B[a]<B[i])cost_diff++;
else cost_diff--;
if(B[c]<B[i])cost_diff--;
else cost_diff++;
}
swap(B[a],B[c]);
for(int i=b+1;i<d;i++){
if(B[b]<B[i])cost_diff++;
else cost_diff--;
if(B[d]<B[i])cost_diff--;
else cost_diff++;
}
swap(B[a],B[c]);
if(B[a]<B[b])cost_diff++;
else cost_diff--;
if(B[c]<B[d])cost_diff++;
else cost_diff--;
if(cost_diff<0 || rnd.nextDouble()<exp(-cost_diff/T)){
// int UU = U[x][y];
// int DD = D[x][y];
// int LL = L[x][y];
// int RR = R[x][y];
// if(original == 0){
// }else if(original == 1){
// }else if(original == 2){
// }
ans[x][y] = new_type;
cost += cost_diff;
function<void(int,int,char)> update = [&](int xx, int yy, char cc) {
if(cc=='U') {
if(xx == 0) return;
int C;
if(ans[xx][yy] == 0) C = D[xx][yy];
else if(ans[xx][yy] == 1) C = L[xx][yy];
else C = R[xx][yy];
D[xx-1][yy] = C;
if(ans[xx-1][yy] == 0)update(xx-1,yy,'U');
else if(ans[xx-1][yy] == 1)update(xx-1,yy,'R');
else update(xx-1,yy,'L');
}else if(cc=='D') {
if(xx == N-1) return;
int C;
if(ans[xx][yy] == 0) C = U[xx][yy];
else if(ans[xx][yy] == 1) C = R[xx][yy];
else C = L[xx][yy];
U[xx+1][yy] = C;
if(ans[xx+1][yy] == 0)update(xx+1,yy,'D');
else if(ans[xx+1][yy] == 1)update(xx+1,yy,'L');
else update(xx+1,yy,'R');
}else if(cc=='L') {
if(yy == 0) return;
int C;
if(ans[xx][yy] == 0) C = R[xx][yy];
else if(ans[xx][yy] == 1) C = U[xx][yy];
else C = D[xx][yy];
R[xx][yy-1] = C;
if(ans[xx][yy-1] == 0)update(xx,yy-1,'L');
else if(ans[xx][yy-1] == 1)update(xx,yy-1,'D');
else update(xx,yy-1,'U');
}else if(cc=='R') {
if(yy == N-1) return;
int C;
if(ans[xx][yy] == 0) C = L[xx][yy];
else if(ans[xx][yy] == 1) C = D[xx][yy];
else C = U[xx][yy];
L[xx][yy+1] = C;
if(ans[xx][yy+1] == 0)update(xx,yy+1,'R');
else if(ans[xx][yy+1] == 1)update(xx,yy+1,'U');
else update(xx,yy+1,'D');
}
};
update(x,y,'U');
update(x,y,'D');
update(x,y,'L');
update(x,y,'R');
swap(B[a],B[c]);
swap(B[b],B[d]);
fenwick_tree<int> ft(4*N+1);
cost = 0;
for(int i=0;i<4*N;i++){
cost += i-ft.sum(0,B[i]);
ft.add(B[i],1);
}
if(cost < best_cost){
best_cost = cost;
best_ans = ans;
}
// rep(i,N){
// rep(j,N) {
// cerr << ans[i][j];
// }
// cerr << '\n';
// }
// cerr << "cost: " << cost << endl;
// cerr << "itr: " << itr << endl;
}
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
cout << best_ans[i][j];
}
cout << '\n';
}
cerr << "cost: " << best_cost << endl;
cerr << "itr: " << itr << endl;
}
int main() {
input();
solve();
}
炭素(breso)