http://poj.org/problem?id=2893
来自逆序对的强大力量
1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 #include<cstring> 5 #define lowbit(x)(x&-x) 6 using namespace std; 7 int c[1000000],a[1000][1000],maxn,n,m,d[1000000]; 8 void update(int k,int x){for(int i=k;i<=maxn;i+=lowbit(i))c[i]+=x;} 9 int getsum(int x) 10 { 11 int ans=0; 12 for(int i=x;i;i-=lowbit(i))ans+=c[i]; 13 return ans; 14 } 15 int main() 16 { 17 while(1) 18 { 19 20 int cnt=0,num=0; 21 scanf("%d%d",&m,&n); 22 if(m==0&&n==0) break; 23 maxn=m*n; 24 for(register int i=1;i<=maxn;++i) 25 { 26 c[i]=d[i]=0; 27 } 28 int kl; 29 for(int i=1;i<=m;++i) 30 for(int j=1;j<=n;++j) 31 { 32 scanf("%d",&a[i][j]); 33 if(a[i][j]==0){kl=i;continue; } 34 d[++cnt]=a[i][j]; 35 update(d[cnt],1); 36 num+=cnt-getsum(d[cnt]); 37 } 38 if(n%2==1) 39 { 40 if(num%2==0)printf("YES\n"); 41 else printf("NO\n"); 42 } 43 else 44 { 45 if((m-kl+num)%2==0)printf("YES\n"); 46 else printf("NO\n"); 47 } 48 } 49 }