이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#ifndef local
#include"prison.h"
#endif
using namespace std;
const int M=20;
vector<vector<int>> ans;
int trans[13][3],cnt;
void dfs(int x,int lev,int l,int r,int tl,int tr,int d)
{
fflush(stdout);
ans[x][0]=d;
for(int i=tl; i<=l; ++i)
ans[x][i]=-d-1;
for(int i=r; i<=tr; ++i)
ans[x][i]=-(d^1)-1;
fflush(stdout);
if(l+1>=r) return ;
++l,--r;
int A=(r-l+1)/3+((r-l+1)%3>0),
B=(r-l+1)/3+((r-l+1)%3>1);
int l0=l+A,r0=l+A+B-1;
if(r<=l+3)
l0=l+2,r0=r;
for(int i=l; i<l0; ++i)
{
if(!trans[lev][0]) trans[lev][0]=++cnt;
ans[x][i]=trans[lev][0];
}
for(int i=l0; i<=r0; ++i)
{
if(!trans[lev][1]) trans[lev][1]=++cnt;
ans[x][i]=trans[lev][1];
}
for(int i=r0+1; i<=r; ++i)
{
if(!trans[lev][2]) trans[lev][2]=++cnt;
ans[x][i]=trans[lev][2];
}
if(l<l0) dfs(trans[lev][0],lev+1,l,l0-1,l-1,r+1,d^1);
if(l0<=r0) dfs(trans[lev][1],lev+1,l0,r0,l-1,r+1,d^1);
if(r0<r) dfs(trans[lev][2],lev+1,r0+1,r,l-1,r+1,d^1);
return ;
}
vector<vector<int>> devise_strategy(int N)
{
vector<int> cur(N+1);
for(int i=0; i<=M; ++i) ans.push_back(cur);
dfs(0,0,1,N,1,N,0),assert(cnt<=M);
cerr<<cnt<<endl;
return ans;
}
#ifdef local
static constexpr int kNumPrisoners = 500;
static void invalid_strategy(std::string message) {
printf("%s\n", message.c_str());
exit(0);
}
int main() {
int N;
assert(1 == scanf("%d", &N));
std::vector<std::vector<int>> strategy = devise_strategy(N);
if (strategy.size() == 0) {
invalid_strategy("s is an empty array");
}
int x = strategy.size() - 1;
for (int i = 0; i <= x; ++i) {
if (static_cast<int>(strategy[i].size()) != N + 1) {
invalid_strategy("s[i] contains incorrect length");
}
if (strategy[i][0] < 0 || strategy[i][0] > 1) {
invalid_strategy("First element of s[i] is non-binary");
}
for (int j = 1; j <= N; ++j) {
if (strategy[i][j] < -2 || strategy[i][j] > x) {
invalid_strategy("s[i][j] contains incorrect value");
}
}
}
FILE *log_file = fopen("log.txt","w");
int A, B;
while (1 == scanf("%d", &A) && A != -1) {
assert(1 == scanf("%d", &B));
bool answer = false;
int whiteboard = 0;
for (int i = 0; i < kNumPrisoners && !answer; ++i) {
int check = strategy[whiteboard][0];
whiteboard = strategy[whiteboard][check == 0 ? A : B];
if (whiteboard < 0) {
answer = true;
printf("%c\n", "BA"[whiteboard + 2]);
} else {
if (i > 0) {
fprintf(log_file, " ");
}
fprintf(log_file, "%d", whiteboard);
}
}
if (!answer) {
printf("X\n");
}
fprintf(log_file, "\n");
fflush(log_file);
}
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |