Submission #620486

#TimeUsernameProblemLanguageResultExecution timeMemory
620486Genius3435Gondola (IOI14_gondola)C++17
100 / 100
71 ms5912 KiB
#include <bits/stdc++.h> using namespace std; #include "gondola.h" constexpr int N = 100005; constexpr int M = 250005; constexpr int MOD = 1000000009; int valid(int n, int a[]) { // Check that values \leq n are in order, and that there are no repeats set<int> contained; for (int i = 0; i < n; ++i) { if (contained.count(a[i])) return 0; contained.insert(a[i]); } int start_index = -1; for (int i = 0; i < n; ++i) { if (a[i] <= n) { int index = (a[i] - (i+1) + n) % n; if (start_index == -1) { start_index = index; } else if (start_index != index) { return 0; } } } return 1; } int replacement(int n, int a[], int b[]) { static int position[M]; auto it = max_element(a, a+n); int m = *it, max_idx = it-a; for (int i = 1; i <= m; ++i) { position[i] = -1; } for (int i = 0; i < n; ++i) { position[a[i]] = i; } int start_index = -1; for (int i = 0; i < n; ++i) { if (a[i] <= n) { start_index = (a[i] - (i+1) + n) % n; break; } } if (start_index == -1) { start_index = 0; } int *currSeq = new int[n]; for (int i = 0; i < n; ++i) { currSeq[i] = (start_index + i) % n + 1; } for (int i = n+1; i <= m; ++i) { if (position[i] == -1) { b[i-n-1] = currSeq[max_idx]; currSeq[max_idx] = i; } else { b[i-n-1] = currSeq[position[i]]; currSeq[position[i]] = i; } } delete[] currSeq; return m-n; } int pow(long long b, int e, int m) { long long p = 1; while (e) { if (e&1) p = p * b % m; e >>= 1, b = b * b % m; } return p; } int countReplacement(int n, int a[]) { if (!valid(n, a)) return 0; std::set<int> contained; for (int i = 0; i < n; ++i) { if (a[i] > n) { contained.insert(a[i]); } } int positions = contained.size(); long long ans = positions<n ? 1 : n; int prv = n; for (const int i : contained) { // All values in the interval (prv, i) have positions spots they could go in ans = ans * pow(positions, i-prv-1, MOD) % MOD; prv = i; positions--; } return ans; }
#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...