CSES - Datatähti Open 2021 - Results
Submission details
Task:Sorting
Sender:nhho
Submission time:2021-01-31 07:47:55 +0200
Language:C++11
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED36
#2ACCEPTED64
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#2ACCEPTED0.24 s2details
#3ACCEPTED0.17 s1, 2details
#4ACCEPTED0.23 s1, 2details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &t);
  ~~~~~^~~~~~~~~~
input/code.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
   ~~~~~^~~~~~~~~~
input/code.cpp:32:37: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   for (int i = 1; i <= n; i++) scanf("%d", p + i);
                                ~~~~~^~~~~~~~~~~~~

Code

#include <bits/stdc++.h>

using namespace std;

int t, n, p[105];
set<vector<int>> s;

bool go(int a) {
	if (!s.insert(vector<int>(p + 1, p + a + 1)).second) return 0;
	bool ok = 1;
	for (int i = 1; i <= a; i++)
		if (p[i] != i) {
			ok = 0;
			break;
		}
	if (ok) return 1;
	for (int i = 1; i < a; i++)
		for (int j = i + 2; j < a; j++) {
			swap(p[i], p[j]);
			swap(p[i + 1], p[j + 1]);
			if (go(a)) return 1;
			swap(p[i], p[j]);
			swap(p[i + 1], p[j + 1]);
		}
	return 0;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) scanf("%d", p + i);
		for (int j = n ; j >= 6; j--) {
			if (p[j] == j) continue;
			if (p[1] == j) {
				swap(p[1], p[j - 1]);
				swap(p[2], p[j]);
			}
			if (p[j - 1] == j) {
				swap(p[j - 1], p[j - 3]);
				swap(p[j], p[j - 2]);
				swap(p[j - 3], p[j]);
				swap(p[j - 4], p[j - 1]);
				continue;
			}
			for (int k = 2; k <= j - 2; k++)
				if (p[k] == j) {
					swap(p[k - 1], p[j - 1]);
					swap(p[k], p[j]);
					break;
				}
		}
		s.clear();
		puts(go(min(n, 6)) ? "YES" : "NO");
	}
}

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

input
153
1
1
2
1 2
...

correct output
YES
YES
NO
NO
NO
...

user output
YES
YES
NO
NO
NO
...

Test 2

Group: 2

Verdict: ACCEPTED

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

correct output
YES
NO
YES
NO
YES
...

user output
YES
NO
YES
NO
YES
...

Test 3

Group: 1, 2

Verdict: ACCEPTED

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
YES
NO
NO
NO
YES
...

Test 4

Group: 1, 2

Verdict: ACCEPTED

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
NO
NO
YES
NO
YES
...