Valera has got n domino pieces in a row. Each piece consists of two halves — the upper one and the lower one. Each of the halves contains a number from 1 to 6. Valera loves even integers very much, so he wants the sum of the numbers on the upper halves and the sum of the numbers on the lower halves to be even.
To do that, Valera can rotate the dominoes by 180 degrees. After the rotation the upper and the lower halves swap places. This action takes one second. Help Valera find out the minimum time he must spend rotating dominoes to make his wish come true.
The first line contains integer n (1 ≤ n ≤ 100), denoting the number of dominoes Valera has. Next n lines contain two space-separated integers xi, yi (1 ≤ xi, yi ≤ 6). Number xi is initially written on the upper half of the i-th domino, yi is initially written on the lower half.
Print a single number — the minimum required number of seconds. If Valera can't do the task in any time, print - 1.
2 4 2 6 4
0
1 2 3
-1
3 1 4 2 3 4 4
1
In the first test case the sum of the numbers on the upper halves equals 10 and the sum of the numbers on the lower halves equals 6. Both numbers are even, so Valera doesn't required to do anything.
In the second sample Valera has only one piece of domino. It is written 3 on the one of its halves, therefore one of the sums will always be odd.
In the third case Valera can rotate the first piece, and after that the sum on the upper halves will be equal to 10, and the sum on the lower halves will be equal to 8.
解题说明:此题是要求交换一些行上两个数的位置让前后两列数字之和都是偶数。做法是先统计前后两列的和,如果和都为偶数就不用交换,如何一奇一偶那么无论如何交换都不行,如果是两个奇数则需要找到某一行中两个数字之和为奇数的情况,因为交换了这两个数,前后列的和分别变化1,这样正好得到了两个偶数。分析后发现其实题目中最多只需要交换一行即可。
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
int su=0,sl=0,n,i,j,a[100],b[100],flag=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i],&b[i]);
sl=sl+b[i];
su=su+a[i];
}
if((sl%2)==1&&(su%2)==1)
{
for(i=0;i<n;i++)
{
flag=1;
if((a[i]%2+b[i]%2)==1)
{
printf("1\n");
return 0;
}
}
}
else if((sl%2==0)&&(su%2==0))
{
printf("0\n");
return 0;
}
else
{
printf("-1\n");
}
if( flag==1)
{
printf("-1\n");
}
return 0;
}