Submission #165028

#TimeUsernameProblemLanguageResultExecution timeMemory
165028kostia244Gondola (IOI14_gondola)C++17
75 / 100
51 ms4888 KiB
#include "gondola.h"
#include<bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<int>;
int valid(int n, int a[]) {
	set<int> x;
	for (int i = 0; i < n; i++)
		x.insert(a[i]);
	if (x.size() != n)
		return 0;
	for (int i = 1; i < n; i++) {
		if (a[i] > n) {
			a[i] = a[i - 1] + 1;
		}
	}
	if(*x.begin()>n)
		a[0]=1;
	for (int i = n; i-- > 1;) {
		if (a[i] > n) {
			a[i] = a[i - 1] == n ? 1 : a[i - 1] + 1;
		}
	}
	int i = 0;
	while (a[i] != 1)
		i++;
	int p = 1;
	for (int j = (i + 1) % n; j != i; j = (j + 1) % n) {
		if (p+1 != a[j])
			return 0;
		p = a[j];
	}
	return 1;
}
int validorz(int n, int a[]) {
	set<int> x;
	for (int i = 0; i < n; i++)
		x.insert(a[i]);
	if (x.size() != n)
		return 0;
	int mn = 1, pp = 0;
	for (int i = 1; i < n; i++) {
		if (a[i] > n) {
			a[i] = a[i - 1] + 1;
		}
		if(a[i]<mn) mn = a[i], pp = i;
	}
	for(int i = 0; i < n; i++) {
		a[pp] = mn;
		mn = (mn%n)+1;
		pp = (pp+1)%n;
	}
	int i = 0;
	while (a[i] != 1)
		i++;
	int p = 1;
	for (int j = (i + 1) % n; j != i; j = (j + 1) % n) {
		if (p+1 != a[j])
			return 0;
		p = a[j];
	}
	return 1;
}

//----------------------
int pos[250250];
int replacement(int n, int a[], int b[]) {
	int mx = 0, p = 0, mn = n, pp = 1;
	bitset<250250> have;
	for(int i = 0; i < n; i++) {
		if(a[i]>n) {
			have.set(a[i]);
			pos[a[i]] = i;
		}
		if(a[i]>mx) mx = a[i], p = i;
		if(a[i]<mn) mn = a[i], pp = i;
	}
	for(int i = 0; i < n; i++) {
		a[pp] = mn;
		mn = (mn%n)+1;
		pp = (pp+1)%n;
	}
	for(int i = 0; i < n; i++)
		if(a[i]>n) exit(-1);
	int j = 0;
	for(int i = n+1; i <= mx; i++) {
		if(have.test(i)) {
			b[j++] = a[pos[i]];
			a[pos[i]] = i;
		} else {
			b[j++] = a[p];
			a[p] = i;
		}
	}
	return max(0, mx-n);
}

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

int ta[300300];
int countReplacement(int n, int a[]) {
	for(int i = 0; i < n; i++) ta[i] = a[i];
	if(!validorz(n, ta)) return 0;
	int mx = 0, p = 0, mn = n, pp = 0;
	bitset<250250> have;
	ll use = 0;
	for(int i = 0; i < n; i++) {
		if(a[i]>n) {
			have.set(a[i]);
			pos[a[i]] = i;
			use++;
		}
		if(a[i]>mx) mx = a[i], p = i;
		if(a[i]<mn) mn = a[i], pp = i;
	}
	for(int i = 0; i < n; i++) {
		a[pp] = mn;
		mn = (mn%n)+1;
		pp = (pp+1)%n;
	}
	for(int i = 0; i < n; i++)
		if(a[i]>n) exit(-1);
	int j = 0;
	ll ans = 1;
	for(int i = n+1; i <= mx; i++) {
		if(have.test(i)) {
//			b[j++] = a[pos[i]];
			a[pos[i]] = i;
			use--;
		} else {
			ans = (ans*use)%1000000009;
//			b[j++] = a[p];
			a[p] = i;
		}
	}
	return ans;
}

Compilation message (stderr)

gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:12:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (x.size() != n)
      ~~~~~~~~~^~~~
gondola.cpp: In function 'int validorz(int, int*)':
gondola.cpp:41:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (x.size() != n)
      ~~~~~~~~~^~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:125:6: warning: unused variable 'j' [-Wunused-variable]
  int j = 0;
      ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...