Submission #316970

# Submission time Handle Problem Language Result Execution time Memory
316970 2020-10-28T18:41:31 Z VROOM_VARUN Gondola (IOI14_gondola) C++14
100 / 100
74 ms 6008 KB
/*
ID: varunra2
LANG: C++
TASK: gondola
*/

#include <bits/stdc++.h>
#include "gondola.h"
using namespace std;

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000009
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions

int n;

bool good(int val) { return val >= 1 and val <= n; }

VI get(int *vals) {
  VI ret;
  for (int i = 0; i < n; i++) {
    if (good(vals[i])) ret.PB(i);
  }
  return ret;
}

VI brokeget(int *vals) { VI ret; }

bool distinct(int *inputSeq) {
  MPII mp;
  for (int i = 0; i < n; i++) {
    mp[inputSeq[i]]++;
    if (mp[inputSeq[i]] >= 2) return false;
  }
  return true;
}

int valid(int N, int *inputSeq) {
  n = N;
  VI inds = get(inputSeq);
  if (!distinct(inputSeq)) return 0;
  if (sz(inds) == 0) return 1;
  for (int i = inds[0], val = inputSeq[inds[0]], cnt = 0; cnt < n;
       cnt++, val = val % n + 1, i = (i + 1) % n) {
    if (!good(inputSeq[i])) continue;
    if (inputSeq[i] != val) return 0;
  }
  return 1;
}

VII genVals(int n, int *gondolaSeq) {
  VII vals;
  for (int i = 0; i < n; i++) {
    if (!good(gondolaSeq[i])) {
      vals.PB(MP(gondolaSeq[i], i));
    }
  }
  return vals;
}

int replacement(int N, int *gondolaSeq, int *replacementSeq) {
  assert(valid(n, gondolaSeq));
  n = N;
  VII vals = genVals(n, gondolaSeq);
  if (vals.empty()) return 0;
  // debug(vals);
  sort(all(vals));
  int lst = n + 1;
  int cnt = 0;
  auto x = get(gondolaSeq);
  VI label(n);
  if (x.empty()) {
    for (int i = 0; i < n; i++) label[i] = i + 1;
  } else {
    for (int i = x[0], val = gondolaSeq[x[0]], cnt = 0; cnt < n;
         cnt++, val = val % n + 1, i = (i + 1) % n) {
      label[i] = val;
    }
  }
  // debug(label);
  for (int i = 0; i < sz(vals); i++) {
    while (label[vals[i].y] != vals[i].x) {
      replacementSeq[cnt] = label[vals[i].y];
      label[vals[i].y] = lst;
      lst++;
      cnt++;
    }
  }
  return cnt;
}

ll binpow(ll a, ll b) {
  if (b == 0) return 1ll;
  if (b == 1) return a;
  ll mid = binpow(a, b / 2);
  ll mult = (mid * mid) % MOD;
  if (b % 2) return (mult * a) % MOD;
  return mult;
}

int countReplacement(int N, int *inputSeq) {
  n = N;
  if (!valid(n, inputSeq)) return 0;
  ll ret = 1;
  VII vals = genVals(n, inputSeq);
  if (vals.empty()) return 1;
  sort(all(vals));
  // debug(vals);
  int lst = n;
  int cnt = sz(vals);
  if(cnt == n) ret = n;
  for (int i = 0; i < sz(vals); i++) {
    int len = vals[i].x - lst - 1;
    ret *= binpow(cnt, len);
    ret %= MOD;
    lst = vals[i].x;
    cnt--;
  }
  return ret;
}

void test1() {
  int n;
  cin >> n;
  int gondolaSeq[n];
  int replacementSeq[50];
  for (int i = 0; i < n; i++) {
    cin >> gondolaSeq[i];
  }
  int val = replacement(n, gondolaSeq, replacementSeq);
  cout << val << '\n';
  for (int i = 0; i < val; i++) {
    cout << replacementSeq[i] << " ";
  }
  if (val) cout << '\n';
}

void test2() {
  int n;
  cin >> n;
  int gondolaSeq[n];
  for (int i = 0; i < n; i++) {
    cin >> gondolaSeq[i];
  }
  cout << countReplacement(n, gondolaSeq) << '\n';
}

// int main() {
// #ifndef ONLINE_JUDGE
//   freopen("gondola.in", "r", stdin);
//   freopen("gondola.out", "w", stdout);
// #endif
//   cin.sync_with_stdio(0);
//   cin.tie(0);

//   int type;
//   cin >> type;

//   if (type == 1) {
//     test1();
//   } else
//     test2();

//   return 0;
// }

Compilation message

gondola.cpp: In function 'VI brokeget(int*)':
gondola.cpp:68:34: warning: no return statement in function returning non-void [-Wreturn-type]
   68 | VI brokeget(int *vals) { VI ret; }
      |                                  ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 18 ms 2428 KB Output is correct
7 Correct 14 ms 1276 KB Output is correct
8 Correct 33 ms 4208 KB Output is correct
9 Correct 10 ms 1536 KB Output is correct
10 Correct 38 ms 4848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 16 ms 2428 KB Output is correct
7 Correct 15 ms 1276 KB Output is correct
8 Correct 32 ms 4208 KB Output is correct
9 Correct 10 ms 1536 KB Output is correct
10 Correct 39 ms 4844 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 21 ms 2048 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 52 ms 4976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 11 ms 640 KB Output is correct
12 Correct 13 ms 640 KB Output is correct
13 Correct 18 ms 1288 KB Output is correct
14 Correct 11 ms 640 KB Output is correct
15 Correct 26 ms 2328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 52 ms 4600 KB Output is correct
10 Correct 42 ms 3840 KB Output is correct
11 Correct 15 ms 1536 KB Output is correct
12 Correct 19 ms 1920 KB Output is correct
13 Correct 4 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 53 ms 4600 KB Output is correct
10 Correct 42 ms 3836 KB Output is correct
11 Correct 15 ms 1536 KB Output is correct
12 Correct 19 ms 1920 KB Output is correct
13 Correct 5 ms 640 KB Output is correct
14 Correct 64 ms 5368 KB Output is correct
15 Correct 74 ms 6008 KB Output is correct
16 Correct 12 ms 1408 KB Output is correct
17 Correct 48 ms 4216 KB Output is correct
18 Correct 25 ms 2432 KB Output is correct