Submission #102776

#TimeUsernameProblemLanguageResultExecution timeMemory
102776wxh010910Gondola (IOI14_gondola)C++17
100 / 100
31 ms2204 KiB
#include <bits/stdc++.h>
#include "gondola.h"

using namespace std;

const int md = (int) 1e9 + 9;

inline int mul(int x, int y) {
  return (int) ((long long) x * y % md);
}

inline int power(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1) {
      res = mul(res, x);
    }
    x = mul(x, x);
    y >>= 1;
  }
  return res;
}

int valid(int n, int inputSeq[]) {
  vector<int> a(inputSeq, inputSeq + n);
  sort(a.begin(), a.end());
  if (unique(a.begin(), a.end()) != a.end()) {
    return 0;
  }
  int p = min_element(inputSeq, inputSeq + n) - inputSeq;
  for (int i = 0; i < n; ++i) {
    if (inputSeq[(p + i) % n] <= n && inputSeq[(p + i) % n] != inputSeq[p] + i) {
      return 0;
    }
  }
  return 1;
}

int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
  int q = min_element(gondolaSeq, gondolaSeq + n) - gondolaSeq;
  if (gondolaSeq[q] <= n) {
    q = (q - (gondolaSeq[q] - 1) + n) % n;
  }
  int p = max_element(gondolaSeq, gondolaSeq + n) - gondolaSeq;
  int m = gondolaSeq[p];
  vector<int> pos(m + 1, -1);
  for (int i = 0; i < n; ++i) {
    pos[gondolaSeq[i]] = (i - q + n) % n + 1; 
  }
  for (int i = n + 1; i <= m; ++i) {
    if (pos[i] != -1) {
      replacementSeq[i - n - 1] = pos[i];
    } else {
      replacementSeq[i - n - 1] = pos[m];
      pos[m] = i;
    }
  }
  return m - n;
}

int countReplacement(int n, int inputSeq[]) {
  if (!valid(n, inputSeq)) {
    return 0;
  }
  sort(inputSeq, inputSeq + n);
  int ans = 1;
  if (inputSeq[0] > n) {
    ans = n;
  }
  for (int i = 0; i < n; ++i) {
    if (inputSeq[i] > n) {
      ans = mul(ans, power(n - i, inputSeq[i] - max(n, i ? inputSeq[i - 1] : n) - 1));
    }
  }
  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...