Submission #292336

# Submission time Handle Problem Language Result Execution time Memory
292336 2020-09-06T20:46:08 Z VodkaInTheJar Gondola (IOI14_gondola) C++14
100 / 100
65 ms 6008 KB
#include <bits/stdc++.h>
#include "gondola.h"
 
using namespace std;

const int maxn = 1e5 + 3; 

int valid(int n, int a[])
{
	set <int> s;
	for (int i = 0; i < n; i++)
	s.insert(a[i]);
	
	if ((int)s.size() < n)
	return 0;
	
	vector <int> v;
	for (int i = 0; i < n; i++)
	if (a[i] <= n)
	v.push_back(i);
	
	for (int i = 1; i < (int)v.size(); i++)
	{
		int val = a[v[0]] + v[i] - v[0];
		if (val > n)
		val -= n; 
		
		if (val != a[v[i]])
		return 0; 
	}
	
	return 1; 
}

int val[maxn];
int replacement(int n, int a[], int ans[])
{
	int pos = -1;
	for (int i = 0; i < n; i++)
	if (a[i] <= n)
	{
		pos = i;
		break;
	}
	
	if (pos == 1)
	for (int i = 0; i < n; i++)
	val[i] = i + 1; 
	
	else 
	{
		for (int i = pos + 1; i < n; i++)
		{
			val[i] = a[pos] + i - pos; 
			if (val[i] > n)
			val[i] -= n;
		}
		
		for (int i = 0; i < pos; i++)
		{
			val[i] = a[pos] + n - pos + i; 
			if (val[i] > n)
			val[i] -= n; 
		}
	}
	
	vector <pair <int, int> > v;
	for (int i = 0; i < n; i++)
	if (a[i] > n)
	v.push_back({a[i], i});
	
	sort (v.begin(), v.end());
	int last = n, len = 0;
	for (auto i: v)
	{
		for (int j = last + 1; j <= i.first; j++)
		{
			ans[len++] = val[i.second]; 
			val[i.second] = j; 
		}
		
		last = i.first; 
	}
	
	return len; 
}

const int mod = 1e9 + 9; 

int fast_pow(int x, int deg)
{
	int ans = 1, y = x;
	while (deg)
	{
		if (deg & 1)
		ans = 1ll * ans * y % mod;
		
		deg >>= 1;
		y = 1ll * y * y % mod; 
	}
	
	return ans;
}

int countReplacement(int n, int a[])
{
	if (!valid(n, a))
	return 0; 
	
	int ans = 1; 
	
	vector <int > v;
	for (int i = 0; i < n; i++)
	if (a[i] > n)
	v.push_back(a[i]);
	
	sort (v.begin(), v.end());
	int last = n;
	for (int i = 0; i < (int)v.size(); i++)
	{
		ans = 1ll * ans * fast_pow((int)v.size() - i, v[i] - last - 1) % mod;
		last = v[i]; 
	}
	
	bool is = false;
	for (int i = 0; i < n; i++)
	if (a[i] <= n)
	{
		is = true;
		break;
	}
	
	//assert(is != false); 
	
	if (!is)
	ans = 1ll * ans * n % mod; 
	
	return ans; 
}

/*
//const int maxn = 1e5 + 3; 

int n; 
int a[maxn]; 
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	cin >> a[i];
	
	cout << countReplacement(n, a) << endl; 
}
*/
# 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 384 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 376 KB Output is correct
2 Correct 1 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 13 ms 2816 KB Output is correct
7 Correct 33 ms 4216 KB Output is correct
8 Correct 27 ms 4988 KB Output is correct
9 Correct 8 ms 1792 KB Output is correct
10 Correct 30 ms 5664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 19 ms 2816 KB Output is correct
7 Correct 33 ms 4216 KB Output is correct
8 Correct 27 ms 4980 KB Output is correct
9 Correct 8 ms 1792 KB Output is correct
10 Correct 31 ms 5632 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 17 ms 2304 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 44 ms 5628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 384 KB Output is correct
5 Correct 1 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 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 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 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 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 12 ms 1408 KB Output is correct
12 Correct 16 ms 1536 KB Output is correct
13 Correct 17 ms 1680 KB Output is correct
14 Correct 12 ms 1536 KB Output is correct
15 Correct 24 ms 2416 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 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 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 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 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 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 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 43 ms 4608 KB Output is correct
10 Correct 37 ms 3968 KB Output is correct
11 Correct 12 ms 1536 KB Output is correct
12 Correct 15 ms 1920 KB Output is correct
13 Correct 4 ms 640 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 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 43 ms 4600 KB Output is correct
10 Correct 33 ms 3960 KB Output is correct
11 Correct 12 ms 1664 KB Output is correct
12 Correct 16 ms 1920 KB Output is correct
13 Correct 4 ms 640 KB Output is correct
14 Correct 55 ms 5368 KB Output is correct
15 Correct 65 ms 6008 KB Output is correct
16 Correct 10 ms 1408 KB Output is correct
17 Correct 39 ms 4224 KB Output is correct
18 Correct 23 ms 2432 KB Output is correct