Submission #655521

#TimeUsernameProblemLanguageResultExecution timeMemory
655521jophyyjhGondola (IOI14_gondola)C++14
100 / 100
51 ms6352 KiB
/** * Quite simple, though a little bit tedious? Just note that the gondola seq can have * extremely large values in [S10]. * * Time Complexity: O(n * log(n)) (Full solution) * Implementation 1 (Math, implementation) */ #include <bits/stdc++.h> #include "gondola.h" typedef long long ll; typedef std::vector<int> vec; const ll MOD = 1e9 + 9; ll pow(ll a, int b) { ll res = 1; while (b > 0) { if (b % 2 == 1) res = res * a % MOD; a = a * a % MOD, b /= 2; } return res; } vec cyc_shift(int n, int input_seq[]) { int original = -1, original_pos = -1; for (int k = 0; k < n && original == -1; k++) { if (input_seq[k] <= n) original = input_seq[k], original_pos = k; } vec shift(n); int delta = (original != -1 ? original_pos + 1 - original : 0); for (int k = 0; k < n; k++) shift[k] = input_seq[(k + delta + n) % n]; return shift; } int valid(int n, int input_seq[]) { vec shift = cyc_shift(n, input_seq); bool ok = true; std::set<int> appeared; for (int k = 0; k < n && ok; k++) { if (shift[k] <= n) { ok &= (shift[k] == k + 1); } else { ok &= (appeared.find(shift[k]) == appeared.end()); appeared.emplace(shift[k]); } } return ok; } int replacement(int n, int gondola_seq[], int replacement_seq[]) { assert(valid(n, gondola_seq)); vec shift = cyc_shift(n, gondola_seq); int repl_len = 0; vec count(n + 1, 0); memset(replacement_seq, -1, sizeof(int) * 250001); for (int k = 0; k < n; k++) { if (shift[k] > n) { repl_len = std::max(repl_len, shift[k] - n); replacement_seq[shift[k] - n - 1] = k + 1; } } for (int k = repl_len - 1, last = -1; k >= 0; k--) { if (replacement_seq[k] == -1) { replacement_seq[k] = replacement_seq[last]; replacement_seq[last] = n + k + 1; } last = k; } return repl_len; } int countReplacement(int n, int input_seq[]) { if (!valid(n, input_seq)) return 0; vec shift = cyc_shift(n, input_seq); vec top; int max_val = 0; for (int k = 0; k < n; k++) { if (shift[k] > n) top.push_back(shift[k]); max_val = std::max(max_val, shift[k]); } std::sort(top.begin(), top.end()); ll comb = 1; for (int i = 0, m = top.size(), prev = n + 1; i < m; i++) comb *= pow(m - i, top[i] - prev), comb %= MOD, prev = top[i] + 1; bool fixed_cyc = false; for (int k = 0; k < n && !fixed_cyc; k++) fixed_cyc |= (shift[k] == k + 1); if (!fixed_cyc) comb *= ll(n), comb %= MOD; return int(comb); }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...