結果
| 問題 | No.3857 RGB Grid |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-12-03 16:25:22 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
TLE
不安定
|
| 実行時間 | - |
| コード長 | 6,704 bytes |
| 記録 | |
| コンパイル時間 | 3,731 ms |
| コンパイル使用メモリ | 372,908 KB |
| 実行使用メモリ | 613,240 KB |
| 最終ジャッジ日時 | 2026-12-04 21:18:47 |
| 合計ジャッジ時間 | 53,163 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge6_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 46 TLE * 9 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
static const int LTRUE = -1, LFALSE = -2;
static int floordiv(int a, int b) { // b > 0
int q = a / b;
if ((a % b) != 0 && a < 0) --q;
return q;
}
// 反復版 Tarjan (CSR)。comp は逆トポロジカル順(最初に確定した SCC が 0)
vector<int> tarjan_scc(int n, const vector<int>& start, const vector<int>& to) {
vector<int> index(n, -1), low(n, 0), comp(n, -1), st, ptr(n, 0);
vector<char> onst(n, 0);
vector<int> work;
int idx = 0, nc = 0;
for (int s = 0; s < n; s++) {
if (index[s] != -1) continue;
index[s] = low[s] = idx++;
st.push_back(s); onst[s] = 1;
ptr[s] = start[s];
work.push_back(s);
while (!work.empty()) {
int v = work.back();
if (ptr[v] < start[v + 1]) {
int u = to[ptr[v]++];
if (index[u] == -1) {
index[u] = low[u] = idx++;
st.push_back(u); onst[u] = 1;
ptr[u] = start[u];
work.push_back(u);
} else if (onst[u]) {
low[v] = min(low[v], index[u]);
}
} else {
if (low[v] == index[v]) {
while (true) {
int u = st.back(); st.pop_back();
onst[u] = 0;
comp[u] = nc;
if (u == v) break;
}
nc++;
}
work.pop_back();
if (!work.empty()) {
int p = work.back();
low[p] = min(low[p], low[v]);
}
}
}
}
return comp;
}
bool verify(int N, const vector<int>& A, const vector<int>& S,
const vector<vector<int>>& g) {
for (int i = 0; i < N; i++) {
if (g[i][A[i]] != S[i]) return false;
for (int j = 0; j < N; j++) {
if (i + 1 < N && g[i][j] == g[i + 1][j]) return false;
if (j + 1 < N && g[i][j] == g[i][j + 1]) return false;
}
}
return true;
}
bool solve_2sat(int N, const vector<int>& A, const vector<int>& S,
vector<vector<int>>& g) {
const int INF = 1e9;
const int L = N / 3 + 2; // x_i ∈ [-L, L]
const int M = 2 * L; // 1 つの i あたりの変数数
const int nv = N * M;
const int nn = 2 * nv;
auto lit = [&](int i, int t) -> int { // P(i,t) = (x_i >= t)
if (t <= -L) return LTRUE;
if (t > L) return LFALSE;
return 2 * (i * M + (t + L - 1)); // 真リテラル。偽は ^1
};
for (int c = 0; c < 2; c++) {
vector<int> r(N, 0);
for (int i = 0; i < N; i++)
for (int v = 0; v < 6; v++)
if (v % 3 == S[i] && v % 2 == (i + A[i] + c) % 2) { r[i] = v; break; }
bool unsat = false;
auto generate = [&](auto&& emit) {
auto imp = [&](int a, int b) {
if (a == LFALSE || b == LTRUE) return;
if (a == LTRUE && b == LFALSE) { unsat = true; return; }
if (a == LTRUE) { emit(b ^ 1, b); return; }
if (b == LFALSE) { emit(a, a ^ 1); return; }
emit(a, b);
emit(b ^ 1, a ^ 1);
};
// 1) 単調性 P(i,t+1) -> P(i,t)
for (int i = 0; i < N; i++)
for (int t = -L + 1; t < L; t++)
imp(lit(i, t + 1), lit(i, t));
// 2) x_0 = 0
for (int t = -L + 1; t <= L; t++) {
if (t <= 0) imp(LTRUE, lit(0, t));
else imp(lit(0, t), LFALSE);
}
// 3) 差分制約 x_i - x_k <= w : P(i,t) -> P(k,t-w)
for (int k = 0; k < N; k++) {
for (int i = 0; i < N; i++) {
if (i == k) continue;
int D = abs(i - k) + abs(A[i] - A[k]);
int w = floordiv(D - r[i] + r[k], 6);
for (int t = -L + 1 + w; t <= L; t++)
imp(lit(i, t), lit(k, t - w));
}
}
};
// 1 回目: 次数を数える
vector<int> start(nn + 1, 0);
generate([&](int u, int) { start[u + 1]++; });
if (unsat) continue;
for (int i = 0; i < nn; i++) start[i + 1] += start[i];
// 2 回目: 埋める
vector<int> to(start[nn]);
{
vector<int> pos(start.begin(), start.end() - 1);
generate([&](int u, int v) { to[pos[u]++] = v; });
}
vector<int> comp = tarjan_scc(nn, start, to);
bool ok = true;
for (int v = 0; v < nv; v++)
if (comp[2 * v] == comp[2 * v + 1]) { ok = false; break; }
if (!ok) continue;
// 割当: comp[真] < comp[偽] なら真。x_i = 真となる最大の t
vector<int> H(N);
for (int i = 0; i < N; i++) {
int xi = -L;
for (int t = -L + 1; t <= L; t++) {
int v = i * M + (t + L - 1);
if (comp[2 * v] < comp[2 * v + 1]) xi = t;
}
H[i] = r[i] + 6 * xi;
}
// 復元: L1 距離変換
vector<vector<int>> h(N, vector<int>(N, INF));
for (int i = 0; i < N; i++) h[i][A[i]] = H[i];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
if (i) h[i][j] = min(h[i][j], h[i - 1][j] + 1);
if (j) h[i][j] = min(h[i][j], h[i][j - 1] + 1);
}
for (int i = N - 1; i >= 0; i--)
for (int j = N - 1; j >= 0; j--) {
if (i < N - 1) h[i][j] = min(h[i][j], h[i + 1][j] + 1);
if (j < N - 1) h[i][j] = min(h[i][j], h[i][j + 1] + 1);
}
g.assign(N, vector<int>(N));
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
g[i][j] = ((h[i][j] % 3) + 3) % 3;
//assert(verify(N, A, S, g));
return true;
}
return false;
}
int main() {
int N;
scanf("%d", &N);
vector<int> A(N), S(N);
for (int i = 0; i < N; i++) { scanf("%d", &A[i]); A[i]--; }
static char buf[1024];
scanf("%s", buf);
const string cs = "RGB";
for (int i = 0; i < N; i++) S[i] = (int)cs.find(buf[i]);
vector<vector<int>> g;
if (!solve_2sat(N, A, S, g)) {
puts("No");
return 0;
}
puts("Yes");
for (int i = 0; i < N; i++) {
string row(N, ' ');
for (int j = 0; j < N; j++) row[j] = cs[g[i][j]];
puts(row.c_str());
}
return 0;
}