Submission #528911

#TimeUsernameProblemLanguageResultExecution timeMemory
528911Alex_tz307Gondola (IOI14_gondola)C++17
55 / 100
1071 ms2284 KiB
#include <bits/stdc++.h>
#include "gondola.h"

using namespace std;

int valid(int n, int a[]) {
  int N = 0;
  for (int i = 0; i < n; ++i) {
    N = max(N, a[i]);
  }
  vector<bool> ap(N + 1);
  int pos = 0;
  for (int i = 0; i < n; ++i) {
    if (ap[a[i]]) {
      return 0;
    }
    ap[a[i]] = true;
    if (a[i] < a[pos]) {
      pos = i;
    }
  }
  if (a[pos] > n) {
    return 1;
  }
  int x = a[pos];
  for (int rep = 0; rep < n; ++rep) {
    if (a[pos] <= n && a[pos] != x) {
      return 0;
    }
    x += 1;
    if (x == n + 1) {
      x = 1;
    }
    pos += 1;
    if (pos == n) {
      pos = 0;
    }
  }
  return 1;
}

void maxSelf(int &x, int y) {
  if (x < y) {
    x = y;
  }
}

int replacement(int n, int a[], int sol[]) {
  vector<int> v(n);
  iota(v.begin(), v.end(), 1);
  for (int i = 0; i < n; ++i) {
    if (a[i] <= n) {
      int x = a[i];
      for (int rep = 0; rep < n; ++rep) {
        v[i] = x;
        x += 1;
        if (x == n + 1) {
          x = 1;
        }
        i += 1;
        if (i == n) {
          i = 0;
        }
      }
      break;
    }
  }
  int N = 0;
  for (int i = 0; i < n; ++i) {
    maxSelf(N, a[i]);
  }
  vector<int> w(N + 1);
  for (int i = 0; i < n; ++i) {
    w[a[i]] = v[i];
  }
  int len = 0, x = n + 1;
  for (int i = 1; i <= N; ++i) {
    if (w[i]) {
      while (w[i] < i) {
        sol[len++] = w[i];
        w[i] = x;
        x += 1;
      }
    }
  }
  return len;
}

const int mod = 1e9 + 9;

void multSelf(int &x, const int &y) {
  x = (int64_t)x * y % mod;
}

int Pow(int x, int n) {
  int ans = 1;
  while (n) {
    if (n & 1) {
      multSelf(ans, x);
    }
    multSelf(x, x);
    n >>= 1;
  }
  return ans;
}

int countReplacement(int n, int a[]) {
  if (!valid(n, a)) {
    return 0;
  }
  vector<int> v{n};
  for (int i = 0; i < n; ++i) {
    if (a[i] > n) {
      v.emplace_back(a[i]);
    }
  }
  int ans = 1, m = v.size();
  for (int i = 1; i < m; ++i) {
    multSelf(ans, Pow(m - i, v[i] - v[i - 1] - 1));
  }
  if (m == n + 1) {
    multSelf(ans, n);
  }
  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...