Submission #24046495


Source Code Expand

#include<iostream>
#include<set>
#include<vector>
#include<deque>
using namespace std;
int N;
int A[1<<17];
set<int>id[1<<17];
int cnt[1<<17];
vector<int>ans;
set<int>pre;
main()
{
	cin>>N;
	for(int i=0;i<N;i++)
	{
		cin>>A[i];
		A[i]--;
		cnt[A[i]]++;
		pre.insert(i);
	}
	if(N==2)
	{
		cout<<-1<<endl;
		return 0;
	}
	for(int i=0;i<N;i++)id[cnt[i]].insert(i);
	while(!pre.empty())
	{
		int u;
		bool flag=false;
		if(pre.size()==3&&id[1].size()==2)
		{
			int x=*id[1].begin(),y=*++id[1].begin();
			if(A[x]==y&&A[y]==x)flag=true;
		}
		if(flag)
		{
			if(ans.empty()||A[ans.back()]!=*id[1].begin())u=*id[1].begin();
			else u=*++id[1].begin();
		}
		else if(!id[pre.size()-1].empty())u=*id[pre.size()-1].begin();
		else
		{
			if(ans.empty()||A[ans.back()]!=*pre.begin())u=*pre.begin();
			else u=*++pre.begin();
		}
		ans.push_back(u);
		pre.erase(u);
		id[cnt[u]].erase(u);
		if(id[cnt[A[u]]].find(A[u])!=id[cnt[A[u]]].end())
		{
			id[cnt[A[u]]].erase(A[u]);
			cnt[A[u]]--;
			id[cnt[A[u]]].insert(A[u]);
		}
	}
	for(int i=0;i<N;i++)cout<<ans[i]+1<<(i+1==N?"\n":" ");
}

Submission Info

Submission Time
Task D - Arrangement
User kotatsugame
Language C++ (GCC 9.2.1)
Score 800
Code Size 1077 Byte
Status AC
Exec Time 134 ms
Memory 19964 KiB

Compile Error

./Main.cpp:12:6: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
   12 | main()
      |      ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 3
AC × 50
Set Name Test Cases
Sample sample_01, sample_02, sample_03
All hand2_01, hand2_02, hand2_03, hand2_04, hand_01, hand_02, hand_03, hand_04, handmaid_01, max_01, max_02, max_03, max_04, max_05, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, random_10, random_11, random_12, random_13, random_14, random_15, sample_01, sample_02, sample_03, small_01, small_02, small_03, small_04, small_05, special2_01, special2_02, special2_03, special2_04, special2_05, special2_06, special2_07, special2_08, special2_09, special2_10, special_01, special_02, special_03
Case Name Status Exec Time Memory
hand2_01 AC 11 ms 9752 KiB
hand2_02 AC 13 ms 9664 KiB
hand2_03 AC 94 ms 19392 KiB
hand2_04 AC 94 ms 19272 KiB
hand_01 AC 12 ms 9540 KiB
hand_02 AC 9 ms 9664 KiB
hand_03 AC 134 ms 19964 KiB
hand_04 AC 126 ms 19916 KiB
handmaid_01 AC 12 ms 9720 KiB
max_01 AC 99 ms 19820 KiB
max_02 AC 97 ms 19788 KiB
max_03 AC 97 ms 19744 KiB
max_04 AC 99 ms 19856 KiB
max_05 AC 93 ms 19532 KiB
random_01 AC 13 ms 9660 KiB
random_02 AC 14 ms 9712 KiB
random_03 AC 8 ms 9544 KiB
random_04 AC 8 ms 9656 KiB
random_05 AC 10 ms 9680 KiB
random_06 AC 10 ms 9796 KiB
random_07 AC 10 ms 9792 KiB
random_08 AC 8 ms 9552 KiB
random_09 AC 10 ms 9628 KiB
random_10 AC 10 ms 9664 KiB
random_11 AC 8 ms 9724 KiB
random_12 AC 9 ms 9804 KiB
random_13 AC 9 ms 9512 KiB
random_14 AC 16 ms 9572 KiB
random_15 AC 12 ms 9680 KiB
sample_01 AC 8 ms 9540 KiB
sample_02 AC 9 ms 9620 KiB
sample_03 AC 9 ms 9572 KiB
small_01 AC 8 ms 9556 KiB
small_02 AC 8 ms 9664 KiB
small_03 AC 12 ms 9568 KiB
small_04 AC 10 ms 9712 KiB
small_05 AC 8 ms 9556 KiB
special2_01 AC 90 ms 19560 KiB
special2_02 AC 92 ms 19500 KiB
special2_03 AC 91 ms 19484 KiB
special2_04 AC 94 ms 19272 KiB
special2_05 AC 91 ms 19324 KiB
special2_06 AC 90 ms 19824 KiB
special2_07 AC 91 ms 19704 KiB
special2_08 AC 84 ms 19700 KiB
special2_09 AC 92 ms 19808 KiB
special2_10 AC 90 ms 19820 KiB
special_01 AC 10 ms 9668 KiB
special_02 AC 15 ms 10260 KiB
special_03 AC 95 ms 19772 KiB