奇数码问题

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
using ll = long long;

const int N = 5e2 + 10;
int n, m, a[N * N], b[N * N], c[N * N], i, j, k;
ll cnt;
void merge (int a[], int l,int r){
	if(r-l<1){
		return;
		
	}
	int mid = (l+r)>>1;
	merge(a,l,mid);
	merge(a,mid+1,r);
	int i=l,j=mid+1;
	for(int k=l;k<=r;++k){
		if(j>r||i<=mid && a[i]<=a[j]){
			b[k]=a[i++];
		}else{
			cnt+=mid-i+1;
			b[k]=a[j++];
		}
	}
	for(int k=l;k<=r;++k){
		a[k]=b[k];
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	while (cin >> n) {
		int ok = 0, x;
		for (i = 1; i <= n * n; ++i) {
			cin >> x;
			if (0 == x) {
				ok = 1;
			} else {
				a[i - ok] = x;
			}

		}
		ok = 0;
		for (i = 1; i <= n * n; ++i) {
			cin >> x;
			if (0 == x) {
				ok = 1;
			} else {
				c[i - ok] = x;
			}
		}
		cnt = 0;
		memset(b, 0, sizeof b);
		merge(a, 1, n * n);
		ans = cnt;
		memset(b, 0, sizeof b);
		cnt = 0;
		merge(c, 1, n * n);
		if((ans&1)==(cnt&1)){
			puts("TAK");
		}else{
			puts("NIE");
		}
	}
	return 0;
}
posted @ 2022-03-21 15:05  ethon-wang  阅读(28)  评论(0编辑  收藏  举报