poj1063Flip and Shift解题报告<数学题>

链接:http://poj.org/problem?id=1063

题意:把若干个白球和黑球围成一圈,每个球都可以与与它距离为2的球交换位置,判断能否把黑球和白球分开;

思路:白球和黑球分开最后的结果为把球排成一排后所有白球或者黑球在奇数位上的个数和在偶数位上的个数之差为2以内(不等于2),若总数为奇数,则不管小球的布局如何,一定能使黑白球各自连续。因为在这种情况下,奇位置上的小球通过交换可以移动到偶位置上。而若总数为偶数则需要满足以上要求了,因为为偶数时它们交换相对奇偶性不变.

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 #include <cstring>
 5 #include <cmath>
 6 using namespace std;
 7 
 8 int main( )
 9 {
10     int T, a[35];
11     scanf( "%d", &T );
12     while( T -- ){
13         int N, g=0, o=0;
14         scanf( "%d", &N );
15         
16         for( int i=0; i<N; ++ i ){
17             scanf( "%d", &a[i] );
18             if( a[i] )  
19                 if( i&1 )o++;
20                 else g++;
21         }
22         if( N&1 ){
23             puts( "YES" );
24             continue;
25         }
26         if( abs( g-o )<2 )puts( "YES" );
27         else puts( "NO" );
28     }
29     return 0;
30 }

 

posted @ 2013-01-07 09:33  淡墨æ末央  阅读(216)  评论(0编辑  收藏  举报