Advertisement
erek1e

IOI '14 P4 - Gondola (Standard I/O)

Jul 14th, 2023
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. const int BASE = 1e9 + 9;
  9. int add(int x, int y) {
  10.     x += y;
  11.     if (x >= BASE) return x-BASE;
  12.     return x;
  13. }
  14. int mult(int x, int y) {
  15.     return (long long)x*y % BASE;
  16. }
  17. int bin_pow(int x, int y) {
  18.     int res = 1;
  19.     while (y) {
  20.         if (y & 1) res = mult(res, x);
  21.         y >>= 1;
  22.         x = mult(x, x);
  23.     }
  24.     return res;
  25. }
  26.  
  27. bool valid(int n, const vector<int> &a) {
  28.     int dif = -1;
  29.     set<int> seen;
  30.     for (int i = 0; i < n; ++i) {
  31.         if (a[i] <= n) {
  32.             int curDif = (a[i] - i + n) % n;
  33.             if (dif == -1) dif = curDif;
  34.             else if (dif != curDif) return false;
  35.         }
  36.         if (seen.count(a[i])) return false;
  37.         seen.insert(a[i]);
  38.     }
  39.     return true;
  40. }
  41. vector<int> replacement(int n, const vector<int> &a) {
  42.     int dif = 1, mx = 0;
  43.     for (int i = 0; i < n; ++i) {
  44.         if (a[i] <= n) dif = (a[i] - i + n) % n;
  45.         if (a[i] > a[mx]) mx = i;
  46.     }
  47.     vector<int> b(n); // current array
  48.     for (int i = 0; i < n; ++i) b[i] = (dif + i + n-1) % n + 1;
  49.  
  50.     vector<int> pos(1+a[mx], -1);
  51.     for (int i = 0; i < n; ++i) pos[a[i]] = i;
  52.  
  53.     vector<int> replace;
  54.     for (int i = n+1; i <= a[mx]; ++i) {
  55.         if (pos[i] == -1) replace.push_back(b[mx]), b[mx] = i;
  56.         else replace.push_back(b[pos[i]]), b[pos[i]] = i;
  57.     }
  58.     return replace;
  59. }
  60. int countReplacement(int n, const vector<int> &a) {
  61.     if (!valid(n, a)) return 0;
  62.     vector<int> b = a;
  63.     sort(b.begin(), b.end());
  64.     int ways = 1;
  65.     for (int i = 0; i < n; ++i) {
  66.         if (b[i] <= n) continue;
  67.         int prev = (i ? b[i-1] : 0);
  68.         int larger = n-i;
  69.         // consider options for values between a[i] and a[i+1]
  70.         int replacements = b[i]-1 - max(prev, n);
  71.         ways = mult(ways, bin_pow(larger, replacements));
  72.     }
  73.     if (b[0] > n) ways = mult(ways, n);
  74.     return ways;
  75. }
  76.  
  77. int main() {
  78.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  79.     int T, n; cin >> T >> n;
  80.     vector<int> a(n);
  81.     for (int &x : a) cin >> x;
  82.    
  83.     if (1 <= T && T <= 3) { // check if gondola sequence
  84.         cout << valid(n, a) << endl;
  85.     } else if (4 <= T && T <= 6) { // find replacement sequence
  86.         vector<int> v = replacement(n, a);
  87.         cout << v.size();
  88.         for (int x : v) cout << ' ' << x;
  89.         cout << endl;
  90.     } else { // 7 <= T && T <= 10, count replacement sequences
  91.         cout << countReplacement(n, a) << endl;
  92.     }
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement