Submission #136582

# Submission time Handle Problem Language Result Execution time Memory
136582 2019-07-25T15:53:08 Z mosesmayer Gondola (IOI14_gondola) C++17
100 / 100
39 ms 3588 KB
#include "gondola.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ALL(x) x.begin(), x.end()
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> pii;

int valid(int n, int inputSeq[]){
	vi a(inputSeq, inputSeq + n);

	// handle whether all values are unique
	vi b(a.begin(), a.end());
	sort(ALL(b)); b.erase(unique(ALL(b)), b.end());
	if (b.size() != a.size()) return 0;

	// handle min element
	vi::iterator mnit = min_element(a.begin(), a.end());
	int mn = *mnit, pos = mnit - a.begin();
	if (mn >= n) return 1;
	
	// rotate elements so that min is at first position.
	// now index i will be original position of (mn + i - 1)%n + 1;
	rotate(a.begin(), a.begin() + pos, a.end());
	// validity check
	for (int i = 0; i < n; i++){
		if (a[i] > n) continue;
		if (a[i] != ((mn + i - 1) % n + 1)) return 0;
	}
	return 1;
}

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

int replacement(int n, int gondolaSeq[], int replacementSeq[]){
	vi a(gondolaSeq, gondolaSeq + n);
	// get length
	int len = (*max_element(a.begin(), a.end())) - n;

	vi::iterator mnit = min_element(a.begin(), a.end());
	int mn = *mnit, pos = mnit - a.begin();

	if (mn < n){
		rotate(a.begin(), a.begin() + pos, a.end());
	} else {
		pos = 0;
	}
	vector<pii> rep; // to be replaced
	for (int i = 0; i < n; i++){
		int oriNum = mn < n ? ((mn + i - 1) % n) + 1 : i+1;
		if (a[i] > n || a[i] != oriNum){
			rep.eb(a[i], oriNum);
		}
	}
	sort(rep.begin(), rep.end());
	int cur = n+1, idx = 0;
	for (int i = 0; i < rep.size(); i++){
		int prv = rep[i].se;
		while (cur <= rep[i].fi){
			replacementSeq[idx++] = prv;
			prv = cur;
			cur++;
		}
	}
	return len;
}

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

const LL mod = 1e9 + 9;
LL fpow(LL b, LL e, LL m = mod){
	LL r = 1;
	for (; e > 0; e >>= 1){
		if (e&1) (r *= b) %= m;
		(b *= b) %= m;
	}
	return r;
}
int countReplacement(int n, int inputSeq[]){
	if (!valid(n, inputSeq)) return 0;
	vi a(inputSeq, inputSeq + n);

	vi::iterator mnit = min_element(a.begin(), a.end());
	int mn = *mnit, pos = mnit - a.begin();

	rotate(a.begin(), a.begin() + pos, a.end());

	vector<pii> rep;
	LL tot = 1;

	int cur = n;
	for (int i = 0; i < n; i++){
		int oriNum = (mn + i - 1) % n + 1;
		if (a[i] != oriNum)
			rep.eb(a[i], oriNum);
		else cur--;
	}
	sort(rep.begin(), rep.end());
	int prv = n;
	for (pii p : rep){
		(tot *= fpow(cur, p.fi - prv - 1)) %= mod;
		cur--;
		prv = p.fi;
	}
	if (mn > n){
		(tot *= n) %= mod;
	}
	return tot;
}

Compilation message

gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:61:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < rep.size(); i++){
                  ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 10 ms 760 KB Output is correct
7 Correct 20 ms 1400 KB Output is correct
8 Correct 14 ms 1144 KB Output is correct
9 Correct 7 ms 632 KB Output is correct
10 Correct 20 ms 1400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 248 KB Output is correct
6 Correct 10 ms 760 KB Output is correct
7 Correct 20 ms 1400 KB Output is correct
8 Correct 14 ms 1144 KB Output is correct
9 Correct 7 ms 632 KB Output is correct
10 Correct 20 ms 1272 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 10 ms 760 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 22 ms 1400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 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 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 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 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 248 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 12 ms 888 KB Output is correct
12 Correct 14 ms 1016 KB Output is correct
13 Correct 18 ms 1396 KB Output is correct
14 Correct 12 ms 1016 KB Output is correct
15 Correct 23 ms 2404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 3 ms 256 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 27 ms 2192 KB Output is correct
10 Correct 22 ms 1932 KB Output is correct
11 Correct 10 ms 1096 KB Output is correct
12 Correct 12 ms 1060 KB Output is correct
13 Correct 4 ms 636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 252 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 27 ms 2192 KB Output is correct
10 Correct 22 ms 1932 KB Output is correct
11 Correct 10 ms 1096 KB Output is correct
12 Correct 12 ms 1192 KB Output is correct
13 Correct 4 ms 504 KB Output is correct
14 Correct 35 ms 3288 KB Output is correct
15 Correct 39 ms 3588 KB Output is correct
16 Correct 9 ms 1008 KB Output is correct
17 Correct 26 ms 2292 KB Output is correct
18 Correct 16 ms 1620 KB Output is correct