답안 #52449

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52449 2018-06-26T03:50:47 Z shoemakerjo 곤돌라 (IOI14_gondola) C++14
100 / 100
88 ms 7136 KB
#include <bits/stdc++.h>
#include "gondola.h"
using namespace std;
#define ll long long
#define pii pair<int, int>
const int mod = 1000000009;

int modpow(int base, int p) {
	if (p == 0) return 1;
	if (p & 1) {
		int tmp = modpow(base, p-1);
		return (tmp*1LL*base)%mod;
	}
	int tmp = modpow(base, p/2);
	return (tmp*1LL*tmp)%mod;
}

int valid(int n, int inputSeq[])
{
	int minval = n+1;
	int minind = 0;
	set<int> seen;
	for (int i = 0; i < n; i++) {
		if (inputSeq[i] < minval) {
			minval = inputSeq[i];
			minind = i;
		}
		if (seen.count(inputSeq[i]) > 0) return 0;
		seen.insert(inputSeq[i]);
	}
	if (minval > n) return 1; //i think this is fine then

	int cur = minind;
	int ivals[n];
	int cv = minval;
	//cannot appear after something that is greater than me and still less than n
	int maxv = -1;
	for (int i = 0; i < n; i++) {
		ivals[cur] = cv;
		cur = (cur+1)%n;
		cv = (cv+1)%n;
		if (cv == 0) cv += n;
	}
	for (int i = 0; i < n; i++) {
		if (inputSeq[i] <= n && inputSeq[i] != ivals[i]) {
			return 0;
		}
	}
	return 1; //nothing was bad enough to return 0
}

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

int replacement(int n, int gondolaSeq[], int replacementSeq[])
{	
	//find a valid replacement sequence

	int sz = 0; //size of the answer

	int lowbelow = -1;
	int lowind = -1;
	int maxval = -1;
	map<int, int> ch;
	int maxind = -1;
	int cvals[n];
	for (int i = 0; i < n; i++) {
		if (gondolaSeq[i] <= n) {
			if (gondolaSeq[i] < lowbelow || lowbelow == -1) {
				lowbelow = gondolaSeq[i];
				lowind = i;
			}

		}
		else { //the value is greater than n
			ch[gondolaSeq[i]] = i; //this index must be the one replaced at its final value
		}
		if (gondolaSeq[i] > maxval) {
			maxval = gondolaSeq[i];
			maxind = i;
		}
	}
	//do not use a vector for ans just add stuff to replacementSeq as you go and increment sz	
	int cv;
	if (lowind != -1) cv = gondolaSeq[lowind];
	int ci = lowind;
	if (lowind == -1) {
		ci = 0;
		cv = 1;
	}
	for (int i = 0; i < n; i++) {
		cvals[ci] = cv; //current values
		ci = (ci+1)%n;
		cv = (cv+1)%n;
		if (cv == 0) cv += n;
	}
	
	for (int i = n+1; i <= maxval; i++) {
		if (ch.count(i) != 0) { //this is a final value for something 
			replacementSeq[sz++] = cvals[ch[i]];
			cvals[ch[i]] = i;
		}
		else {
			replacementSeq[sz++] = cvals[maxind];
			cvals[maxind] = i; //becomes the new value
		}
	}

	return sz;
}

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

int countReplacement(int n, int inputSeq[])
{
	if (valid(n, inputSeq) == 0) { //just add this in there
		return 0;
	}
	int ans = 1;
	vector<int> aftn;
	aftn.push_back(n);
	int minv = 1234567890;
	for (int i = 0; i < n; i++) {
		if (inputSeq[i] > n) {
			aftn.push_back(inputSeq[i]);
		}
		minv = min(minv, inputSeq[i]);
	}
	sort(aftn.begin(), aftn.end());
	for (int i = 1; i < aftn.size(); i++) {
		int rep = aftn[i]-aftn[i-1]-1; //how many times we need to repeat this
		int ops = aftn.size() - i;
		//cout << "here: " << aftn[i] << " rep, ops: " << rep << " " << ops << endl;
		ans = (ans * 1LL * modpow(ops, rep))%mod;
	}
	if (minv > n) {
		//we are allowed to replace anything from the first set to get to n+1
		ans = (ans*1LL*n)%mod;
	}

	return ans;
}

Compilation message

gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:37:6: warning: unused variable 'maxv' [-Wunused-variable]
  int maxv = -1;
      ^~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:129:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 1; i < aftn.size(); i++) {
                  ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 488 KB Output is correct
4 Correct 2 ms 540 KB Output is correct
5 Correct 3 ms 540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 540 KB Output is correct
2 Correct 2 ms 540 KB Output is correct
3 Correct 3 ms 540 KB Output is correct
4 Correct 2 ms 540 KB Output is correct
5 Correct 2 ms 552 KB Output is correct
6 Correct 21 ms 2644 KB Output is correct
7 Correct 14 ms 2644 KB Output is correct
8 Correct 35 ms 4588 KB Output is correct
9 Correct 11 ms 4588 KB Output is correct
10 Correct 45 ms 5116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5116 KB Output is correct
2 Correct 2 ms 5116 KB Output is correct
3 Correct 2 ms 5116 KB Output is correct
4 Correct 2 ms 5116 KB Output is correct
5 Correct 2 ms 5116 KB Output is correct
6 Correct 20 ms 5116 KB Output is correct
7 Correct 23 ms 5116 KB Output is correct
8 Correct 38 ms 5116 KB Output is correct
9 Correct 12 ms 5116 KB Output is correct
10 Correct 46 ms 5180 KB Output is correct
11 Correct 2 ms 5180 KB Output is correct
12 Correct 2 ms 5180 KB Output is correct
13 Correct 24 ms 5180 KB Output is correct
14 Correct 2 ms 5180 KB Output is correct
15 Correct 57 ms 5244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5244 KB Output is correct
2 Correct 2 ms 5244 KB Output is correct
3 Correct 2 ms 5244 KB Output is correct
4 Correct 2 ms 5244 KB Output is correct
5 Correct 2 ms 5244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5244 KB Output is correct
2 Correct 2 ms 5244 KB Output is correct
3 Correct 2 ms 5244 KB Output is correct
4 Correct 2 ms 5244 KB Output is correct
5 Correct 2 ms 5244 KB Output is correct
6 Correct 2 ms 5244 KB Output is correct
7 Correct 2 ms 5244 KB Output is correct
8 Correct 3 ms 5244 KB Output is correct
9 Correct 3 ms 5244 KB Output is correct
10 Correct 3 ms 5244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5244 KB Output is correct
2 Correct 2 ms 5244 KB Output is correct
3 Correct 2 ms 5244 KB Output is correct
4 Correct 2 ms 5244 KB Output is correct
5 Correct 2 ms 5244 KB Output is correct
6 Correct 3 ms 5244 KB Output is correct
7 Correct 2 ms 5244 KB Output is correct
8 Correct 2 ms 5244 KB Output is correct
9 Correct 3 ms 5244 KB Output is correct
10 Correct 2 ms 5244 KB Output is correct
11 Correct 13 ms 5244 KB Output is correct
12 Correct 14 ms 5244 KB Output is correct
13 Correct 34 ms 5244 KB Output is correct
14 Correct 12 ms 5244 KB Output is correct
15 Correct 33 ms 5244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5244 KB Output is correct
2 Correct 2 ms 5244 KB Output is correct
3 Correct 2 ms 5244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5244 KB Output is correct
2 Correct 2 ms 5244 KB Output is correct
3 Correct 2 ms 5244 KB Output is correct
4 Correct 2 ms 5244 KB Output is correct
5 Correct 2 ms 5244 KB Output is correct
6 Correct 2 ms 5244 KB Output is correct
7 Correct 2 ms 5244 KB Output is correct
8 Correct 2 ms 5244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5244 KB Output is correct
2 Correct 2 ms 5244 KB Output is correct
3 Correct 2 ms 5244 KB Output is correct
4 Correct 2 ms 5244 KB Output is correct
5 Correct 2 ms 5244 KB Output is correct
6 Correct 2 ms 5244 KB Output is correct
7 Correct 2 ms 5244 KB Output is correct
8 Correct 2 ms 5244 KB Output is correct
9 Correct 68 ms 5244 KB Output is correct
10 Correct 46 ms 5244 KB Output is correct
11 Correct 19 ms 5244 KB Output is correct
12 Correct 21 ms 5244 KB Output is correct
13 Correct 8 ms 5244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5244 KB Output is correct
2 Correct 2 ms 5244 KB Output is correct
3 Correct 2 ms 5244 KB Output is correct
4 Correct 2 ms 5244 KB Output is correct
5 Correct 2 ms 5244 KB Output is correct
6 Correct 2 ms 5244 KB Output is correct
7 Correct 2 ms 5244 KB Output is correct
8 Correct 2 ms 5244 KB Output is correct
9 Correct 60 ms 5244 KB Output is correct
10 Correct 46 ms 5244 KB Output is correct
11 Correct 17 ms 5244 KB Output is correct
12 Correct 21 ms 5244 KB Output is correct
13 Correct 5 ms 5244 KB Output is correct
14 Correct 74 ms 5668 KB Output is correct
15 Correct 88 ms 7136 KB Output is correct
16 Correct 14 ms 7136 KB Output is correct
17 Correct 56 ms 7136 KB Output is correct
18 Correct 36 ms 7136 KB Output is correct