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 }

 

转载于:https://www.cnblogs.com/jian1573/archive/2013/01/07/2848620.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值