# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
109469 | nikolapesic2802 | Mechanical Doll (IOI18_doll) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
void answer(std::vector<int> C, std::vector<int> X, std::vector<int> Y);
struct node{
int x,y;
node(){x=-1;y=-1;}
};
const int NMAX=20;
vector<node> graf(NMAX);
vector<int> flipped(NMAX);
int stigao;
void build(int tr,int koliko)
{
if(koliko==0)
return;
graf[tr].x=stigao;
stigao++;
build(stigao-1,koliko-1);
graf[tr].y=stigao;
stigao++;
build(stigao-1,koliko-1);
}
pair<int,int> get(int tr)
{
//printf("Getujem %i %i %i %i\n",tr,flipped[tr],graf[tr].x,graf[tr].y);
if(flipped[tr])
{
flipped[tr]=0;
if(graf[tr].y==-1)
{
return {tr,1};
}
else
return get(graf[tr].y);
}
else
{
flipped[tr]=1;
if(graf[tr].x==-1)
{
return {tr,0};
}
else
return get(graf[tr].x);
}
}
void construct(int i,vector<int> d)
{
//cout << d << i << endl;
if(d.size()==0)
d.pb(0);
if(d.size()==1)
{
graf[i].x=d[0];
return;
}
int sz=1;
while((1<<sz)<(int)d.size())
sz++;
graf[i].x=stigao;
stigao++;
build(stigao-1,sz-1);
for(int j=0;j<(1<<sz);j++)
{
pair<int,int> t=get(graf[i].x);
if(t.s)
{
if(j==(1<<sz)-1)
{
graf[t.f].y=d.back();
continue;
}
if(j>=(int)d.size()-1)
graf[t.f].y=graf[i].x;
else
graf[t.f].y=d[j];
}
else
{
if(j==(1<<sz)-1)
{
graf[t.f].x=d.back();
continue;
}
if(j>=(int)d.size()-1)
graf[t.f].x=graf[i].x;
else
graf[t.f].x=d[j];
}
}
/*for(int i=0;i<NMAX;i++)
{
printf("%i: %i %i\n",i,graf[i].x,graf[i].y);
}*/
}
void create_circuit(int m, vector<int> a)
{
int n=a.size();
vector<vector<int> > ide(m+1);
for(int i=0;i<=m;i++)
graf[i].y=-1;
graf[0].x=a[0];
for(int i=1;i<n;i++)
ide[a[i-1]].pb(a[i]);
ide[a.back()].pb(0);
stigao=m+1;
for(int i=1;i<=m;i++)
construct(i,ide[i]);
vector<int> A,X,Y;
for(int i=0;i<=m;i++)
if(graf[i].x>m)
A.pb(m-graf[i].x);
else
A.pb(graf[i].x);
for(int i=m+1;i<stigao;i++)
{
if(graf[i].x>m)
X.pb(m-graf[i].x);
else
X.pb(graf[i].x);
if(graf[i].y>m)
Y.pb(m-graf[i].y);
else
Y.pb(graf[i].y);
}
answer(A,X,Y);
}
constexpr int P_MAX = 20000000;
constexpr int S_MAX = 400000;
int M, N;
std::vector<int> A;
bool answered;
int S;
std::vector<int> IC, IX, IY;
int read_int() {
int x;
if (scanf("%d", &x) != 1) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
return x;
}
void wrong_answer(const char *MSG) {
printf("Wrong Answer: %s\n", MSG);
exit(0);
}
void simulate() {
if (S > S_MAX) {
char str[50];
sprintf(str, "over %d switches", S_MAX);
wrong_answer(str);
}
for (int i = 0; i <= M; ++i) {
if (!(-S <= IC[i] && IC[i] <= M)) {
wrong_answer("wrong serial number");
}
}
for (int j = 1; j <= S; ++j) {
if (!(-S <= IX[j - 1] && IX[j - 1] <= M)) {
wrong_answer("wrong serial number");
}
if (!(-S <= IY[j - 1] && IY[j - 1] <= M)) {
wrong_answer("wrong serial number");
}
}
int P = 0;
std::vector<bool> state(S + 1, false);
int pos = IC[0];
int k = 0;
FILE *file_log = fopen("log.txt", "w");
fprintf(file_log, "0\n");
for (;;) {
fprintf(file_log, "%d\n", pos);
if (pos < 0) {
if (++P > P_MAX) {
fclose(file_log);
char str[50];
sprintf(str, "over %d inversions", P_MAX);
wrong_answer(str);
}
state[-pos] = !state[-pos];
pos = state[-pos] ? IX[-(1 + pos)] : IY[-(1 + pos)];
} else {
if (pos == 0) {
break;
}
if (k >= N) {
fclose(file_log);
wrong_answer("wrong motion");
}
if (pos != A[k++]) {
fclose(file_log);
wrong_answer("wrong motion");
}
pos = IC[pos];
}
}
fclose(file_log);
if (k != N) {
wrong_answer("wrong motion");
}
for (int j = 1; j <= S; ++j) {
if (state[j]) {
wrong_answer("state 'Y'");
}
}
printf("Accepted: %d %d\n", S, P);
}
// namespace
void answer(std::vector<int> C, std::vector<int> X, std::vector<int> Y) {
if (answered) {
wrong_answer("answered not exactly once");
}
answered = true;
// check if input format is correct
if ((int)C.size() != M + 1) {
wrong_answer("wrong array length");
}
if (X.size() != Y.size()) {
wrong_answer("wrong array length");
}
S = X.size();
IC = C;
IX = X;
IY = Y;
}
int main() {
freopen("in.txt","r",stdin);
M = read_int();
N = read_int();
A.resize(N);
for (int k = 0; k < N; ++k) {
A[k] = read_int();
}
answered = false;
create_circuit(M, A);
if (!answered) {
wrong_answer("answered not exactly once");
}
FILE *file_out = fopen("out.txt", "w");
fprintf(file_out, "%d\n", S);
for (int i = 0; i <= M; ++i) {
fprintf(file_out, "%d\n", IC[i]);
}
for (int j = 1; j <= S; ++j) {
fprintf(file_out, "%d %d\n", IX[j - 1], IY[j - 1]);
}
fclose(file_out);
simulate();
return 0;
}