Submission #767353

#TimeUsernameProblemLanguageResultExecution timeMemory
767353marvinthangMechanical Doll (IOI18_doll)C++17
100 / 100
77 ms11208 KiB
/************************************* * author: marvinthang * * created: 26.06.2023 22:53:11 * *************************************/ #include "doll.h" #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define left ___left #define right ___right #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define MASK(i) (1LL << (i)) #define BIT(x, i) ((x) >> (i) & 1) #define __builtin_popcount __builtin_popcountll #define ALL(v) (v).begin(), (v).end() #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i) #define REPD(i, n) for (int i = (n); i--; ) #define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; ) #define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u) #define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u) #ifdef LOCAL #include "debug.h" #else #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } #define DB(...) 23 #define db(...) 23 #define debug(...) 23 #endif template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; } template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; } template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; } template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); } template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; } // end of template int N, M; vector <int> A, X, Y; void gen_tree(int cur, int k, int v) { if (k == 1) { X[cur] = 0; Y[cur] = v == 2 ? 0 : -1; return; } int r; if (v <= MASK(k - 1)) { r = 0; } else { r = X.size(); X.push_back(0); Y.push_back(0); gen_tree(r, k - 1, MASK(k - 1)); v -= MASK(k - 1); } int l = X.size(); X.push_back(0); Y.push_back(0); gen_tree(l, k - 1, v); X[cur] = -1 - l; Y[cur] = -1 - r; } void dfs(int u) { if (A.empty()) return; u = -u - 1; swap(X[u], Y[u]); if (!Y[u]) { Y[u] = A.back(); A.pop_back(); dfs(-1); } else dfs(Y[u]); } void create_circuit(int m, vector<int> a) { A = a; M = m; A.push_back(0); reverse(ALL(A)); N = A.size(); int k = 1; while (MASK(k) < N) ++k; X.push_back(0); Y.push_back(0); gen_tree(0, k, N); vector <int> C(M + 1, -1); dfs(-1); answer(C, X, Y); } #ifdef LOCAL #include <cstdio> #include <cstdlib> #include "doll.h" namespace { 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 1"); } if (pos != ___A[k++]) { fclose(file_log); wrong_answer("wrong motion 2"); } pos = IC[pos]; } } fclose(file_log); if (k != ___N) { wrong_answer("wrong motion 3"); } 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() { file("doll"); ___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; } #endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...