每日一题第一期答案

#include"iostream"
#include"cstdio"
#include"cmath"
using namespace std;
int n, a[10005], b[10005], ans1, ans2;
bool check1() {
	for (int i = 2; i <= n * 2; i++) {
		if (a[i] < a[i - 1]) {
			return false;
		}
	}
	return true;
}
bool check2() {
	int sum = 0;
	for (int i = 1; i <= n * 2; i++) {
		if (a[i] == b[i]) {
			sum++;
		}
	}
	if (sum == n * 2) {
		return true;
	} else {
		return false;
	}
}
void pd() {
	for (int i = 1; i <= 2 * n; i += 2) {
		swap(a[i], a[i + 1]);
	}
}
void pd2() {
	for (int i = n + 1; i <= n * 2; i++) {
		swap(a[i - n], a[i]);
	}
}
int main() {
	freopen("task.in", "r", stdin);
	freopen("task.out", "w", stdout);
	scanf("%d", &n);
	for (int i = 1; i <= n * 2; i++) {
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	int x = 1, flag = 0;
	while (1) {
		if (check1()) {
			flag = 1;
			break;
		}
		if (x == 1) {
			ans1++;
			pd();
			x = 2;
		} else {
			ans1++;
			pd2();
			x = 1;
		}
		if (check2()) {
			break;
		}
	}
	x = 2;
	for (int i = 1; i <= n * 2; i++) {
		a[i] = b[i];
	}
	while (1) {
		if (check1()) {
			flag = 1;
			break;
		}
		if (x == 1) {
			ans2++;
			pd();
			x = 2;
		} else {
			ans2++;
			pd2();
			x = 1;
		}
		if (check2()) {
			break;
		}
	}
	if (flag == 0) {
		printf("-1");
	} else {
		printf("%d", min(ans1, ans2));
	}

	return 0;
}

你可能感兴趣的:(每日一题答案,c++)