MXN Puzzle-Reverse order of "n*m the accessibility of digital problems"

Source: Internet
Author: User

MXN Puzzlesol:

\ (n*m\) the feasibility of a certain situation to reach another situation
It is possible to determine the parity of the number by reverse order.
Let's write the number of this \ (n*m-1\) in a column.

    • Considering the odd behavior, the odd-numbered case is the same as the number of odd parity
      The space movement will not change the number of reverse order, and the upper and lower movement is equivalent to change the number of total (m-1\) in reverse order number of the column, because \ (m-1\) is even, so the number of reverse before and after the odd parity is unchanged.
    • Consider the behavior of odd, listed as an even number of cases (reverse order number + before and after the status of space lines of the difference) parity of the same time can reach
      Moving up and down will change the number of inverse pairs (m-1\) number, and \ (m-1\) is odd, when the top and bottom moved the even line, the parity is not changed, the odd line is moved up and down, parity changes.
    • If the behavior is even, ibid.

      AC CODE:
Source code#include<cstdio> #include <cstring> #include <algorithm>using namespace Std;int read () {    int X=0,f=1;char ch= '; while (ch> ' 9 ' | |    ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch<= ' 9 ' &&ch>= ' 0 ') {x= (x<<3) + (x<<1) + (ch^ ' 0 '); Ch=getchar ();} return x*f;} const int N = + 2;int n,m;int a[n*n],tot;int tr[n*n];int lowbit (int x) {return x&-x;}    void Modify (int p,int k) {for (int i=p;i<=tot;i+=lowbit (i)) {tr[i]+=k;    }}int query (int p) {int ans=0;    for (int i=p;i;i-=lowbit (i)) {ans+=tr[i]; } return ans;    int main () {//Freopen ("Data.in", "R", stdin);//Freopen ("Sol.out", "w", stdout);        while (1) {n=read (); M=read ();        if (!n&&!m) break;        memset (tr,0,sizeof (tr));        Tot=0;int POS;                for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {int x;x=read ();                if (x) a[++tot]=x;            else pos=i; }} int muL=n*m;        int ans=0;            for (int i=tot;i;i--) {ans+=query (a[i]-1);        Modify (a[i],1);        } if (m%2==0) {ans+=n-pos;        } if (ans%2==0) printf ("yes\n");    else printf ("no\n"); } return 0;}

MXN Puzzle-Reverse order the accessibility of the N*M digital problem scenario

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.