Submission #542946

#TimeUsernameProblemLanguageResultExecution timeMemory
542946collodelGondola (IOI14_gondola)C++17
65 / 100
23 ms4364 KiB
#include "gondola.h" #include <vector> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; int valid(int n, int inputSeq[]) { int last = -1, invertions = 0; // trova l'ultimo elemento valido for(int i = n-1; i >= 0; --i) { if(inputSeq[i] <= n) { last = inputSeq[i]; break; } } // conta le inversioni unordered_map<int, int> occ; for(int i = 0; i < n; ++i) { occ[inputSeq[i]]++; if(inputSeq[i] <= n) { invertions += (last > inputSeq[i]); last = inputSeq[i]; } } if(invertions > 1) return false; // controlla non ci siano duplicati1 return all_of(occ.begin(), occ.end(), [](pair<const int, int>& x){return x.second == 1;}); } //---------------------- int replacement(int n, int gondolaSeq[], int replacementSeq[]) { // if(!valid(n, gondolaSeq)) {} int* mx = max_element(gondolaSeq, gondolaSeq + n); int k = *mx - n; // setta tutto a -1 for(int i = 0; i < k; ++i) { replacementSeq[i] = -1; } // trova la posizione dell'elemento 1 int pos = 0; for(int i = 0; i < n; ++i) { if(gondolaSeq[i] <= n) { pos = (n + i - gondolaSeq[i] + 1) % n; break; } } // imposta a partire da gondolaSeq for(int i = 0; i < n; ++i) { if(gondolaSeq[i] > n) { replacementSeq[gondolaSeq[i] - n - 1] = (n + i - pos) % n + 1; } } int curr_mx = (mx - gondolaSeq + n - pos) % n + 1; for(int i = 0; i < k; ++i) { if(replacementSeq[i] == -1) { replacementSeq[i] = curr_mx; curr_mx = n + i + 1; } } replacementSeq[*mx - n - 1] = curr_mx; return k; } //---------------------- using ll = long long; const ll mod = 1e9 + 9ll; ll mod_pow(ll base, ll exp) { ll ans = 1; while(exp > 0) { if(exp & 1) ans = (ans * base) % mod; exp >>= 1; base = (base * base) % mod; } return ans; } int countReplacement(int n, int inputSeq[]) { if(!valid(n, inputSeq)) return 0; ll ans = 1; vector<int> s; for(int i = 0; i < n; ++i) { if(inputSeq[i] > n) s.push_back(inputSeq[i]); } sort(s.begin(), s.end()); int k = s.size(); if(k == 0) return 1; ans *= mod_pow(k, s[0] - n - 1); ans %= mod; for(int i = 1; i < k; ++i) { ans *= mod_pow(k - i, s[i] - s[i-1] - 1); ans %= mod; } 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...