1312: Domino
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 135 Solved: 51
[ Submit][ Status][ Web Board]
Description
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.
Input
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.
Output
Print a single number — the minimum required number of seconds. If Valera can't do the task in any time, print - 1.
Sample Input
Sample Output
HINT
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.
【解析】
这道题的话是这个意思如果两列和都是偶数那么我们就不用改动了,如果不是的话,那么我们可以这么想改变的只能在一行行元素中改变,所以如果两列和是一奇一偶的话那肯定是不可以的,如果是两个奇数,并且行元素中有一奇一偶存在的那是可以的只需要改动一次就可以了,我们会发动其实最大只用改一次
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
int sum1,sum2,i,j,n,flag,flag1;
int a[101]={0};
int b[101]={0};
while(~scanf("%d",&n))
{
sum1=0;
sum2=0;
flag=0;
flag1=0;
int a[101]={0};
int b[101]={0};
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i],&b[i]);
sum1=sum1+b[i];
sum2=sum2+a[i];
}
if((sum1%2)==1&&(sum2%2)==1)
{
for(i=0;i<n;i++)
{
flag=1;
if((a[i]%2+b[i]%2)==1)
{
printf("1\n");
flag1=1;
break;
}
}
if(flag1==1)
continue;
}
else if((sum1%2==0)&&(sum2%2==0))
{
printf("0\n");
}
else
{
printf("-1\n");
}
if( flag==1)
{
printf("-1\n");
}
}
return 0;
}