sgu 139 Help Needed!

题意:16数码是否有解?

先计算展开成一维后逆序对。如果0在最后一行,那么逆序偶时有解。4*4时(n为偶)0的位置上升一行,逆序对+3或-1(奇偶性变化)。(n为奇时+2或+0,不变)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#define mkp make_pair
using namespace std;
const double EPS=1e-8;
const int SZ=20,INF=0x7FFFFFFF;
typedef long long lon;
int arr[SZ];

int main()
{
    std::ios::sync_with_stdio(0);
    //freopen("d:\\1.txt","r",stdin);
    lon casenum;
    //cin>>casenum;
    //for(lon time=1;time<=casenum;++time)
    {
        int n=16;
        int res=0;
        for(int i=0;i<n;++i)
        {
            cin>>arr[i];
            if(arr[i]==0)res+=i/4+1;
        }
        for(int i=0;i<n;++i)
        {
            for(int j=i+1;j<n;++j)
            {
                if(arr[i]!=0&&arr[j]!=0&&arr[i]>arr[j])++res;
            }
        }
        cout<<(res&1?"NO":"YES")<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/gaudar/p/9771289.html
sgu