Submission #218870

#TimeUsernameProblemLanguageResultExecution timeMemory
218870Kenzo_1114Gondola (IOI14_gondola)C++17
90 / 100
26 ms3328 KiB
#include "gondola.h" #include<bits/stdc++.h> using namespace std; const int MAXN = 300010; const long long int MOD = 1e9 + 9; int correct[MAXN], pos[MAXN]; bool marc[MAXN], all = false; void calc(int n, int seq[]) { int id = -1; for(int i = 0; i < n; i++) if(seq[i] <= n) { id = i; break; } if(id != -1) correct[id] = seq[id]; else id = 0, correct[id] = 1, all = true; for(int i = id + 1; i < n; i++) { correct[i] = correct[i - 1] + 1; if(correct[i] > n) correct[i] = 1; } for(int i = id - 1; i >= 0; i--) { correct[i] = correct[i + 1] - 1; if(correct[i] < 1) correct[i] = n; } } int valid(int n, int seq[]) { calc(n, seq); int ans = 1; for(int i = 0; i < n; i++) { if(seq[i] <= n && correct[i] != seq[i]) ans = 0; if(seq[i] > n && marc[seq[i]]) ans = 0; marc[seq[i]] = true; } for(int i = 0; i < MAXN; i++) marc[i] = false; return ans; } int replacement(int n, int gondolaSeq[], int replacementSeq[]) { calc(n, gondolaSeq); for(int i = 0; i < MAXN; i++) pos[i] = -1; int mx = n; for(int i = 0; i < n; i++) if(gondolaSeq[i] > n) pos[gondolaSeq[i]] = i, mx = max(mx, gondolaSeq[i]); for(int i = mx - 1; i > n; i--) if(pos[i] == -1) pos[i] = pos[i + 1]; int l = 0; for(int i = n + 1; i <= mx; i++) { replacementSeq[l] = correct[pos[i]]; correct[pos[i]] = i; l++; } return l; } long long int fe(long long int num, long long int exp) { if(exp == 0) return 1; if(exp == 1) return num; long long int aux = fe(num, exp / 2); aux *= aux, aux %= MOD; if(exp % 2) return (aux * num) % MOD; return aux % MOD; } vector<long long int> v; int countReplacement(int n, int inputSeq[]) { if(!valid(n, inputSeq)) return 0; for(int i = 0; i < n; i++) if(inputSeq[i] > n) v.push_back(inputSeq[i]); sort(v.begin(), v.end()); long long int ans = 1; long long int sz = v.size(), last = n; for(int i = 0; i < v.size(); i++) { // printf("num = %lld exp = %lld\n", sz, v[i] - 1- last); ans *= fe(sz, v[i] - 1 - last); ans %= MOD; last = v[i]; sz--; } if(all) ans *= n, ans %= MOD, all = false; v.clear(); return (int) ans; } /* int N, SEQ[MAXN]; int main () { scanf("%d", &N); for(int i = 0; i < N; i++) scanf("%d", &SEQ[i]); printf("valid = %d\n", valid(N, SEQ)); printf("cont = %lld\n", countReplacement(N, SEQ)); } */

Compilation message (stderr)

gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:103:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v.size(); i++)
                 ~~^~~~~~~~~~
#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...