Submission #589149

#TimeUsernameProblemLanguageResultExecution timeMemory
589149fuad27Gondola (IOI14_gondola)C++17
75 / 100
1046 ms2328 KiB
#include "gondola.h"
#include<bits/stdc++.h>
using namespace std;
#define prev(i)	((i-1+n)%n)
typedef long long ll;
typedef long double ld;
const ll inf = 1e18;
const ll mod = 1e9+9;
#define pii		pair<int,int>
#define pll		pair<ll,ll>
#define vi		vector<int>
#define vll		vector<ll>
#define rep(i, a, b)	for(int i = (a);i<(b);i++)
#define repn(i, a, b)	for(int i = (a);i>=(b);i--)
#define ss		second
#define ff		first
#define mkp		make_pair
#define pb		push_back

#define all(x)		(x).begin(), (x).end()

int valid(int n, int a[])
{
	int cnt = a[0]-1;
	for(int i = 1;i<n-cnt;i++) {
		if(a[i] != a[i-1]+1) {
			return 0;
		}
	}
	for(int i = n-cnt+1;i<n;i++) {
		if(a[i]!=a[i-1]+1)return 0;	
	}
	if(cnt==0)return 1;
	if(a[0] != a[n-1]+1)return 0;
	return 1;
	return -1;
}
 
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
	int cnt=0;
	for(int i = 0;i<n;i++) {
		if(gondolaSeq[i]>n)cnt++;	 
	}
	if(cnt == 0)return 0;
	else if(cnt == n) {
		vector<pair<int,int>> res;
		res.push_back(mkp(n,n));
		for(int i = 0;i<n;i++) {
			res.push_back(mkp(gondolaSeq[i], i+1));
		}
		sort(res.begin(), res.end());
		vector<int> ans;
		for(int i=1;i<(int)res.size();i++) {
			ans.push_back(res[i].second);
			for(int j = res[i-1].first;j<res[i].first-1;j++) {
				ans.push_back(j+1);
			}
		}
		for(int i=0;i<(int)ans.size();i++) {
			replacementSeq[i]=ans[i];
		}
		return (int)ans.size();
	}
	else {
		vector<pair<int,int>> res;
		res.push_back(make_pair(n,n));
		for(int i = 0;i<n;i++) {
			if(gondolaSeq[i] > n and gondolaSeq[prev(i)]<=n) {
				int tmp = gondolaSeq[i];
				gondolaSeq[i] = (gondolaSeq[prev(i)]%n)+1;
				res.push_back(make_pair(tmp, gondolaSeq[i]));
			}
		}
		for(int i = 0;i<n;i++) {
			if(gondolaSeq[i] > n and gondolaSeq[prev(i)]<=n) {
				int tmp = gondolaSeq[i];
				gondolaSeq[i] = (gondolaSeq[prev(i)]%n)+1;
				res.push_back(make_pair(tmp, gondolaSeq[i]));
			}
		}
		sort(res.begin(), res.end());
		vector<int> ans;
		for(int i=1;i<(int)res.size();i++) {
			ans.push_back(res[i].second);
			for(int j = res[i-1].first;j<res[i].first-1;j++) {
				ans.push_back(j+1);
			}
		}
		for(int i=0;i<(int)ans.size();i++) {
			replacementSeq[i]=ans[i];
		}
		return (int)ans.size();
	}
	return 0;
}
 
 
int countReplacement(int n, int inputSeq[])
{
	int mx = 0;
	rep(i, 0, n)mx = max(mx, inputSeq[i]);
	ll ans=1;
	rep(i, n+1, mx+1) {
		ll cnt = 0;
		bool check=false;
		rep(j, 0, n) {
			if(inputSeq[j] == i)check=true;
			else if(inputSeq[j] > i)cnt++;
		}
		if(check)cnt=1;
		ans*=cnt;
		ans%=mod;
	}
	int gondolaSeq[n];
	rep(i, 0, n)gondolaSeq[i]=inputSeq[i];
	vector<pair<int,int>> res;
	res.push_back(make_pair(n,n));
	for(int i = 0;i<n;i++) {
		if(gondolaSeq[i] > n and gondolaSeq[prev(i)]<=n) {
			int tmp = gondolaSeq[i];
			gondolaSeq[i] = (gondolaSeq[prev(i)]%n)+1;
			res.push_back(make_pair(tmp, gondolaSeq[i]));
		}
	}
	for(int i = 0;i<n;i++) {
		if(gondolaSeq[i] > n and gondolaSeq[prev(i)]<=n) {
			int tmp = gondolaSeq[i];
			gondolaSeq[i] = (gondolaSeq[prev(i)]%n)+1;
			res.push_back(make_pair(tmp, gondolaSeq[i]));
		}
	}
	if(!valid(n, gondolaSeq))return 0;
	return ans;
}
#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...