結果
| 問題 | No.5022 XOR Printer |
| コンテスト | |
| ユーザー |
Shun_PI
|
| 提出日時 | 2025-07-26 17:45:07 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,668 ms / 2,000 ms |
| コード長 | 6,239 bytes |
| コンパイル時間 | 3,425 ms |
| コンパイル使用メモリ | 241,796 KB |
| 実行使用メモリ | 7,716 KB |
| スコア | 5,127,414,671 |
| 最終ジャッジ日時 | 2025-07-26 17:46:39 |
| 合計ジャッジ時間 | 90,576 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
ソースコード
#include <string>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
constexpr int MOD = 1000000007;
vector<lint> RH_B = {1532834020, 1388622299};
vector<lint> RH_M = {2147482409, 2147478017};
constexpr int INF = 2147483647;
void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";}
template<class T>void chmax(T &a, const T &b) { if (a<b) a=b; }
template<class T>void chmin(T &a, const T &b) { if (b<a) a=b; }
vector<int> dx = {-1, 0, 1, 0};
vector<int> dy = {0, -1, 0, 1};
vector<char> dc = {'U', 'L', 'D', 'R'};
constexpr int TURN_LIMIT = 1000;
mt19937 mt(0);
void move(int &x, int &y, int tx, int ty, vector<char> &ans) {
if (x < tx) {
REP(i, tx - x) ans.push_back('D');
} else {
REP(i, x - tx) ans.push_back('U');
}
if (y < ty) {
REP(i, ty - y) ans.push_back('R');
} else {
REP(i, y - ty) ans.push_back('L');
}
x = tx;
y = ty;
}
void copy(int x, int y, int &s, vector<vector<int>> &A, vector<char> &ans) {
if(ans.size() < TURN_LIMIT) s ^= A[x][y];
ans.push_back('C');
}
void write(int x, int y, int &s, vector<vector<int>> &A, vector<char> &ans) {
if(ans.size() < TURN_LIMIT) A[x][y] ^= s;
ans.push_back('W');
}
bool check(int x, int y, int k, int MIN_BIT, vector<vector<int>> &A) {
return (A[x][y]>>k&1) == 0;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N, T;
cin >> N >> T;
vector<vector<int>> A(N, vector<int>(N));
REP(i, N) {
REP(j, N) {
cin >> A[i][j];
}
}
//sを1XXXXXXに初期化した後、k-2ビット目にそろえるようにしていくとできる
int x = 0;
int y = 0;
int s = 0;
vector<char> ans;
int MIN_BIT = 12;
vector<P> last_list;
IFOR(k, MIN_BIT, 20) {
if(ans.size() >= TURN_LIMIT - 30) break; //ターン数制限に引っかかったら終了
//sについてそのビットを立てる
int min_dist = INF;
int tx = -1, ty = -1;
REP(i, N) REP(j, N) {
if((A[i][j]>>k&1) == (s>>k&1)) continue;
bool ok = true;
//そのビットより上のビットが全て立っているか確認
FOR(l, k+1, 20) {
if((A[i][j]>>l&1) == 0) {
ok = false;
break;
}
}
if(!ok) continue;
if (abs(x - i) + abs(y - j) < min_dist) {
min_dist = abs(x - i) + abs(y - j);
tx = i;
ty = j;
}
}
move(x, y, tx, ty, ans);
copy(x, y, s, A, ans);
//変えないといけないマスの一覧を得る
vector<P> poslist;
poslist.push_back(P(x, y));
REP(i, N) REP(j, N) {
if(check(i, j, k, MIN_BIT, A)) {
poslist.push_back(P(i, j));
}
}
poslist.push_back(P(-1, -1));
//TSPを2-opt焼きなましで解く
int best_score = 0;
REP(i, poslist.size()-1) if(poslist[i+1].first != -1) best_score += abs(poslist[i].first - poslist[i+1].first) + abs(poslist[i].second - poslist[i+1].second);
int best_score_2 = best_score;
vector<P> best_list = poslist;
int loop = 0;
float start_temp = 0.0, end_temp = -1.0;
float temp = start_temp;
float time;
auto start_sa_time = chrono::system_clock::now();
int TIME_LIMIT = 200;
while(true) {
if(loop%100000 == 0) {
time = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now() - start_sa_time).count();
if(time > TIME_LIMIT) break;
cerr << k << " " << loop << " " << best_score << "\n";
temp = start_temp + (end_temp - start_temp) * time / (float)TIME_LIMIT;
}
loop++;
int i = mt() % (poslist.size() - 2) + 1;
int j = mt() % (poslist.size() - 2) + 1;
if(abs(i-j) < 2) continue; //隣接しているものは交換しない
if(i > j) swap(i, j);
int score = best_score;
score -= abs(poslist[i-1].first - poslist[i].first) + abs(poslist[i-1].second - poslist[i].second);
if(poslist[j+1].first != -1) score -= abs(poslist[j].first - poslist[j+1].first) + abs(poslist[j].second - poslist[j+1].second);
score += abs(poslist[i-1].first - poslist[j].first) + abs(poslist[i-1].second - poslist[j].second);
if(poslist[j+1].first != -1) score += abs(poslist[i].first - poslist[j+1].first) + abs(poslist[i].second - poslist[j+1].second);
int diff = best_score - score;
float prob = exp(diff*pow(0.1, temp));
if(diff >= 0 || prob*(float)INF > (mt()%INF)) {
//交換する
vector<P> new_list = poslist;
reverse(new_list.begin() + i, new_list.begin() + j + 1);
poslist = vector<P>(new_list);
best_score = score;
if(best_score < best_score_2) {
best_score_2 = best_score;
best_list = poslist;
}
}
}
int last_x = -1, last_y = -1;
best_list.pop_back();
if(k < 19) {
last_x = best_list.back().first;
last_y = best_list.back().second;
best_list.pop_back(); //最後の要素は移動しない
}
FOR(i, 1, best_list.size()) {
if(ans.size() >= TURN_LIMIT - 30) break; //ターン数制限に引っかかったら終了
int tx = best_list[i].first;
int ty = best_list[i].second;
move(x, y, tx, ty, ans);
write(x, y, s, A, ans);
}
if(last_x != -1) {
move(x, y, last_x, last_y, ans);
copy(x, y, s, A, ans);
last_list.push_back(P(last_x, last_y));
}
}
//残った数の小さいものをなんとかする
REP(i, last_list.size()) {
int tx = last_list[i].first;
int ty = last_list[i].second;
move(x, y, tx, ty, ans);
write(x, y, s, A, ans);
copy(x, y, s, A, ans);
write(x, y, s, A, ans);
break;
}
REP(i, min((int)ans.size(), TURN_LIMIT)) {
cout << ans[i] << "\n";
}
int score = 0;
REP(i, N) {
REP(j, N) {
score += A[i][j];
}
}
cerr << score << "\n";
}
Shun_PI