• 奇数码问题


    #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;
    }
    
  • 相关阅读:
    c# 键值数据保存XML文件
    c# 封装 Request操作类
    c# 获取客户端IP
    c#封装DBHelper类
    c# Cache 使用实例
    c#cookie读取写入操作
    c# Session写入读取操作
    ABAP-HTTP支持
    WDA-文档-基础篇/进阶篇/讨论篇
    UI5-文档-4.38-Accessibility
  • 原文地址:https://www.cnblogs.com/jeseesmith/p/16034790.html
Copyright © 2020-2023  润新知