Submission #105110

# Submission time Handle Problem Language Result Execution time Memory
105110 2019-04-10T14:56:01 Z WLZ Gondola (IOI14_gondola) C++17
100 / 100
124 ms 6268 KB
#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;

const long long MOD = (long long) 1e9 + 9;

int valid(int n, int inputSeq[]) {
  int pos = -1;
  set<int> used;
  for (int i = 0; i < n; i++) {
    if (used.count(inputSeq[i])) {
      return 0;
    }
    used.insert(inputSeq[i]);
    if (inputSeq[i] <= n) {
      pos = i;
    }
  }
  if (pos == -1) {
    return 1;
  }
  int cur = inputSeq[pos] - pos;
  if (cur < 1) {
    cur = n + cur;
  }
  for (int i = 0; i < n; i++) {
    if (inputSeq[i] <= n && inputSeq[i] != cur) {
      return 0;
    }
    cur++;
    if (cur > n) {
      cur = 1;
    }
  }
  return 1;
}

//----------------------

int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
  int mx = *max_element(gondolaSeq, gondolaSeq + n);
  vector<int> pos(mx + 1, -1);
  int st = -1, tmp = -1;
  for (int i = 0; i < n; i++) {
    pos[gondolaSeq[i]] = i;
    if (gondolaSeq[i] > n) {
      if (st == -1 || gondolaSeq[i] > gondolaSeq[st]) {
        st = i;
      }
    } else {
      tmp = i;
    }
  }
  vector<int> a(n);
  if (tmp == -1) {
    a[0] = 1;
  } else {
    a[0] = gondolaSeq[tmp] - tmp;
    if (a[0] < 1) {
      a[0] = n + a[0];
    }
  }
  for (int i = 1; i < n; i++) {
    a[i] = a[i - 1] + 1;
    if (a[i] > n) {
      a[i] = 1;
    }
  }
  int l = 0;
  for (int i = n + 1; i <= mx; i++) {
    if (pos[i] == -1) {
      replacementSeq[l++] = a[st];
      a[st] = i;
    } else {
      replacementSeq[l++] = a[pos[i]];
      a[pos[i]] = i;
    }
  }
  return l;
}

//----------------------

template<typename T>
T modpow(T b, T p) {
  if (p == 0) {
    return 1;
  }
  T ans = modpow(b * b % MOD, p / 2);
  if (p % 2 == 1) {
    ans = (ans * b) % MOD;
  }
  return ans;
}

int countReplacement(int n, int inputSeq[]) {
  if (!valid(n, inputSeq)) {
    return 0;
  }
  int pos = -1, cnt = 0;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (inputSeq[i] <= n) {
      pos = i;
    } else {
      cnt++;
      v.push_back(inputSeq[i]);
    }
  }
  sort(v.begin(), v.end());
  long long ans = 1;
  int prev = n;
  for (auto& x : v) {
    ans = (ans * modpow((long long) cnt, (long long) (x - prev - 1))) % MOD;
    cnt--;
    prev = x;
  }
  if (pos == -1) {
    ans = (ans * (long long) n) % MOD;
  }
  return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 256 KB Output is correct
6 Correct 22 ms 2424 KB Output is correct
7 Correct 24 ms 1144 KB Output is correct
8 Correct 34 ms 4344 KB Output is correct
9 Correct 14 ms 1664 KB Output is correct
10 Correct 43 ms 4984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 24 ms 2432 KB Output is correct
7 Correct 21 ms 1144 KB Output is correct
8 Correct 40 ms 4344 KB Output is correct
9 Correct 13 ms 1664 KB Output is correct
10 Correct 52 ms 4956 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 30 ms 2424 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 100 ms 5088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 3 ms 256 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 256 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 15 ms 1636 KB Output is correct
12 Correct 21 ms 1664 KB Output is correct
13 Correct 13 ms 1536 KB Output is correct
14 Correct 13 ms 1536 KB Output is correct
15 Correct 29 ms 2340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 3 ms 380 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 256 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 3 ms 256 KB Output is correct
9 Correct 80 ms 4700 KB Output is correct
10 Correct 56 ms 3804 KB Output is correct
11 Correct 18 ms 1536 KB Output is correct
12 Correct 23 ms 1920 KB Output is correct
13 Correct 8 ms 688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 3 ms 356 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 88 ms 4600 KB Output is correct
10 Correct 58 ms 3804 KB Output is correct
11 Correct 23 ms 1664 KB Output is correct
12 Correct 22 ms 1920 KB Output is correct
13 Correct 7 ms 768 KB Output is correct
14 Correct 99 ms 5528 KB Output is correct
15 Correct 124 ms 6268 KB Output is correct
16 Correct 14 ms 1396 KB Output is correct
17 Correct 76 ms 4216 KB Output is correct
18 Correct 27 ms 2552 KB Output is correct