File size: 4,741 Bytes
1fd0050 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | // Simulator that captures solution's stderr
#include <bits/stdc++.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
using namespace std;
int main(int argc, char* argv[]) {
int n = 100000, subtask = 3;
if (argc > 1) n = atoi(argv[1]);
if (argc > 2) subtask = atoi(argv[2]);
vector<int> ring(n);
iota(ring.begin(), ring.end(), 1);
mt19937 rng(42);
shuffle(ring.begin(), ring.end(), rng);
vector<int> pos(n+1);
for (int i = 0; i < n; i++) pos[ring[i]] = i;
vector<int> vis(n+2, 0);
int an = 0;
auto flip2 = [&](int u) -> int {
int pu = pos[u] + 1;
if ((vis[pu] ^= 1)) {
an += vis[pu-1] + vis[pu+1];
} else {
an -= vis[pu-1] + vis[pu+1];
}
return an || (vis[1] && vis[n]);
};
int pipe_to_sol[2], pipe_from_sol[2];
pipe(pipe_to_sol);
pipe(pipe_from_sol);
// Pipe for solution's stderr
int pipe_err[2];
pipe(pipe_err);
pid_t pid = fork();
if (pid == 0) {
close(pipe_to_sol[1]);
close(pipe_from_sol[0]);
close(pipe_err[0]);
dup2(pipe_to_sol[0], STDIN_FILENO);
dup2(pipe_from_sol[1], STDOUT_FILENO);
dup2(pipe_err[1], STDERR_FILENO);
close(pipe_to_sol[0]);
close(pipe_from_sol[1]);
close(pipe_err[1]);
execl("./debug_sol", "debug_sol", nullptr);
perror("execl");
_exit(1);
}
close(pipe_to_sol[0]);
close(pipe_from_sol[1]);
close(pipe_err[1]);
// Make stderr pipe non-blocking
fcntl(pipe_err[0], F_SETFL, O_NONBLOCK);
FILE* to_sol = fdopen(pipe_to_sol[1], "w");
FILE* from_sol = fdopen(pipe_from_sol[0], "r");
auto readIntFrom = [&]() -> int {
int c = fgetc(from_sol);
while (c != '-' && (c < '0' || c > '9')) {
if (c == EOF) return -999;
c = fgetc(from_sol);
}
int sgn = 1;
if (c == '-') { sgn = -1; c = fgetc(from_sol); }
int x = 0;
while (c >= '0' && c <= '9') { x = x*10+(c-'0'); c = fgetc(from_sol); }
return x * sgn;
};
int cnt_round = 0, cnt_query = 0;
auto start_time = chrono::steady_clock::now();
fprintf(to_sol, "%d %d\n", subtask, n);
fflush(to_sol);
bool correct = false;
bool error = false;
while (true) {
int N = readIntFrom();
if (N == -999) { fprintf(stderr, "EOF from solution\n"); error = true; break; }
if (N == -1) {
vector<int> ans(n);
for (int i = 0; i < n; i++) {
ans[i] = readIntFrom();
if (ans[i] == -999) { error = true; break; }
}
if (error) break;
int opt = -1;
for (int i = 0; i < n; i++) if (ans[0] == ring[i]) { opt = i; break; }
if (opt == -1) { fprintf(stderr, "Answer vertex not in ring\n"); break; }
bool f1 = true, f2 = true;
for (int i = 1; i < n; i++) {
int op1 = (opt + i) % n;
int op2 = (opt - i + n) % n;
if (ring[op1] != ans[i]) f1 = false;
if (ring[op2] != ans[i]) f2 = false;
if (!f1 && !f2) break;
}
correct = f1 || f2;
break;
}
cnt_round++;
cnt_query += N;
vector<int> ops(N);
for (int i = 0; i < N; i++) {
ops[i] = readIntFrom();
if (ops[i] == -999) { error = true; break; }
}
if (error) break;
for (int i = 0; i < N; i++) {
int res = flip2(ops[i]);
if (i > 0) fputc(' ', to_sol);
fprintf(to_sol, "%d", res);
}
fputc('\n', to_sol);
fflush(to_sol);
}
auto end_time = chrono::steady_clock::now();
double elapsed = chrono::duration<double>(end_time - start_time).count();
fclose(to_sol);
fclose(from_sol);
int status;
waitpid(pid, &status, 0);
// Read solution's stderr
char errbuf[65536];
string sol_stderr;
ssize_t nr;
while ((nr = read(pipe_err[0], errbuf, sizeof(errbuf)-1)) > 0) {
errbuf[nr] = 0;
sol_stderr += errbuf;
}
close(pipe_err[0]);
auto f = [](double x) -> double { return min(max(log2(x), 0.0), 8.0); };
double lambda_val = max(0.0, 1.0 - 0.1 * (f(cnt_round / 18.0) + f(cnt_query / 1.5e7)));
printf("=== Solution Debug Output ===\n%s\n", sol_stderr.c_str());
printf("=== Results ===\n");
printf("Correct: %s\n", correct ? "YES" : "NO");
printf("Rounds: %d\n", cnt_round);
printf("Queries: %d\n", cnt_query);
printf("Time: %.3f s\n", elapsed);
printf("Lambda: %.4f\n", lambda_val);
return correct ? 0 : 1;
}
|