Submission #469585

#TimeUsernameProblemLanguageResultExecution timeMemory
469585Soumya1Gondola (IOI14_gondola)C++17
60 / 100
49 ms4784 KiB
#include "gondola.h" #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int valid(int n, int a[]) { bool ok = true; map<int, bool> mp; for (int i = 0; i < n; i++) { ok &= (!mp[a[i]]); mp[a[i]] = true; } int pos = -1, val = -1; for (int i = 0; i < n; i++) { if (a[i] <= n) { val = a[i]; pos = i; break; } } val++; if (val > n) val = 1; for (int i = pos + 1; i < n; i++) { if (a[i] <= n) { ok &= (a[i] == val); } val++; if (val > n) val = 1; } for (int i = 0; i < pos; i++) { if (a[i] <= n) { ok &= (a[i] == val); } val++; if (val > n) val = 1; } return (int) ok; } int replacement(int n, int a[], int ans[]) { int pos = -1, val = -1; for (int i = 0; i < n; i++) { if (a[i] <= n) { pos = i; val = a[i]; break; } } vector<pair<int, int>> v; for (int i = 0; i < n; i++) { if (a[i] > n) v.push_back({a[i], i}); } if (pos != -1) { for (int i = pos; i < n; i++) { a[i] = val++; if (val > n) val = 1; } for (int i = 0; i < pos; i++) { a[i] = val++; if (val > n) val = 1; } } else { for (int i = 0; i < n; i++) a[i] = i + 1; } int ptr = 0; sort(v.begin(), v.end()); int cur = n + 1; for (int i = 0; i < v.size(); i++) { while (cur <= v[i].first) { ans[ptr++] = a[v[i].second]; a[v[i].second] = cur++; } } return ptr; } int bigmod(int a, int b) { int res = 1; while (b > 0) { if (b & 1) res = (1LL * res * a) % mod; a = (1LL * a * a) % mod, b >>= 1; } return res; } int countReplacement(int n, int a[]) { if (!valid(n, a)) return 0; int pos = -1, val = -1; for (int i = 0; i < n; i++) { if (a[i] <= n) { pos = i; val = a[i]; break; } } sort(a, a + n); int ans = 1; int taken = 0; int nn = n; for (int i = 0; i < n; i++) { if (a[i] > n) { ans = (1LL * ans * bigmod(nn, a[i] - n - 1)) % mod; taken++; } nn--; } if (pos == -1) { for (int i = 1; i <= n; i++) { ans = (1LL * ans * i) % mod; } } return ans; }

Compilation message (stderr)

gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:66:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |   for (int i = 0; i < v.size(); i++) {
      |                   ~~^~~~~~~~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:84:17: warning: variable 'val' set but not used [-Wunused-but-set-variable]
   84 |   int pos = -1, val = -1;
      |                 ^~~
#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...