CSES - Datatähti Open 2021 - Results
Submission details
Task:Sorting
Sender:Black_hat
Submission time:2021-01-30 09:17:57 +0200
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#10.01 s1, 2details
#20.01 s2details
#30.01 s1, 2details
#40.01 s1, 2details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:33:8: warning: unused variable 'j' [-Wunused-variable]
   ll i,j,k,l,n;
        ^
input/code.cpp:33:12: warning: unused variable 'l' [-Wunused-variable]
   ll i,j,k,l,n;
            ^

Code

#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store") // to restrict undesirable precision
#pragma GCC optimize ("-fno-defer-pop")// to pop argument of function as soon as it returns
#define all(a) a.begin(),a.end()
#define ll long long int
#define ld long double
ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m)%m; t=(t*t)%m; if(b&1) t=((t%m)*(a%m))%m; return t;}
ll modInverse(ll a, ll m) { return power(a, m-2, m); }
#define ps push_back
#define fs first
#define sc second
#define takeline cin.ignore();
#define iactive cout.flush();
#define N 100005
#define endl "\n"
#define mod 1000000007
//((1.0l)*BIG MULTIPLY MAGIC?)
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//----------------------------------------------
int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	
//	1 6 4 2 3 5
	int t;
	cin>>t;
	while(t--){
		ll i,j,k,l,n;
		cin>>n;
		ll ar[n+1],fl[n+1];
		for(i=1;i<=n;i++){
			cin>>ar[i]; fl[ar[i]]=i;
		}
//		1 2 a 3 b c d
//		1 2 b c a 3 d
		
		for(i=1;i<=n;i++){
			if(i==ar[i]) continue;
			if(fl[i]==i+1){
				if(i+4<=n){
					swap(ar[n-2],ar[i]);
					swap(ar[n-1],ar[i+1]);
					fl[ar[i]]=i; fl[ar[i+1]]=i+1;
					fl[ar[n-1]]=n-1; fl[ar[n-2]]=n-2;
					swap(ar[n-1],ar[i]);
					swap(ar[n],ar[i+1]);
					fl[ar[n-1]]=n-1; fl[ar[i]]=i;
					fl[ar[n]]=n; fl[ar[i+1]]=i+1;
				}
				else break;
			}
			else if(fl[i]==n){
				if(i+4<=n){
					swap(ar[i+1],ar[n-1]);
					swap(ar[i+2],ar[n]);
					fl[ar[i+1]]=i+1; fl[ar[n-1]]=n-1;
					fl[ar[i+2]]=i+1; fl[ar[n]]=n;
					swap(ar[i],ar[i+2]);
					swap(ar[i+1],ar[i+3]);
					fl[ar[i]]=i; fl[ar[i+2]]=i;
					fl[ar[i+1]]=i+1; fl[ar[i+3]]=i+3;
				}
				else break;
			}
			else{
				k=fl[i];
				swap(ar[i],ar[fl[i]]);
				swap(ar[i+1],ar[fl[i]+1]);
				fl[ar[i+1]]=i+1; fl[ar[i]]=i;
				fl[ar[k]]=k; fl[ar[k+1]]=k+1;
			}
//			for(j=1;j<=n;j++) cout<<ar[j]<<" ";
//			cout<<endl;
		}
		for(i=1;i<=n;i++){
			if(i!=ar[i]) break;
		}
		if(i==n+1) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}

Test details

Test 1

Group: 1, 2

Verdict:

input
153
1
1
2
1 2
...

correct output
YES
YES
NO
NO
NO
...

user output
(empty)

Test 2

Group: 2

Verdict:

input
1000
59
35 29 32 50 11 15 9 21 19 45 2...

correct output
YES
NO
YES
NO
YES
...

user output
(empty)

Test 3

Group: 1, 2

Verdict:

input
720
6
1 6 4 5 2 3
6
6 3 2 1 5 4
...

correct output
YES
NO
NO
NO
YES
...

user output
(empty)

Test 4

Group: 1, 2

Verdict:

input
1000
8
7 4 2 8 6 3 5 1
8
3 8 2 7 5 4 6 1
...

correct output
NO
NO
YES
NO
YES
...

user output
(empty)