在交换过程中,如果总长度为奇数,那么奇数位的1可以移动到偶数位上去(它是循环的!),肯定有解。
如果总长度为偶数,奇数位上的1在交换的过程中只能到奇数位上(偶数位也一样,这有点像国际象棋里的黑象与白象永远不会重叠)。条件变为奇数位上的1的个数与偶数位上的1的个数相差不超过1。
Accepted | 1028 | C++ | 00:00.01 |
不明白时间为什么是00:00.01,唉,人品差没办法!
#include<stdio.h>
bool solve(int n)
{
int arr[30],i,even = 0,odd = 0 ;
for(i = 0; i < n; i++ )
{
scanf("%d",& arr[i]);
if(arr[i]) (i & 1) ? even++: odd++ ;
}
return ((n & 1 == 1) || (even == odd) || (odd - even == 1) || (odd - even == -1 ));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1028.txt","r" ,stdin);
#endif
int t,n;
while(scanf("%d",&t) != EOF)
while(t-- && scanf("%d",&n) != EOF)
solve(n) ? printf("YES/n") : printf("NO/n" );
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0 ;
}
bool solve(int n)
{
int arr[30],i,even = 0,odd = 0 ;
for(i = 0; i < n; i++ )
{
scanf("%d",& arr[i]);
if(arr[i]) (i & 1) ? even++: odd++ ;
}
return ((n & 1 == 1) || (even == odd) || (odd - even == 1) || (odd - even == -1 ));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1028.txt","r" ,stdin);
#endif
int t,n;
while(scanf("%d",&t) != EOF)
while(t-- && scanf("%d",&n) != EOF)
solve(n) ? printf("YES/n") : printf("NO/n" );
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0 ;
}