Domino(模拟)

A. Domino
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
2
4 2
6 4
output
0
input
1
2 3
output
-1
input
3
1 4
2 3
4 4
output
1
Note

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.

 

  题意:

  给出N(1到100)对数字,要另左半边的数和右半边的数都为偶数,可以通过某一对数互换位置来满足条件,输出最小的对换次数,若本身已经满足条件则输出0,若无论如何都不可能达到则输出-1。

 

  思路:

  无论左右半边是什么数,总数加出来只有三种情况。1.两边都是偶数 2.两边都是奇数 3.一边是奇数,一边是偶数。情况一则输出0,情况三则输出-1。对于情况二则有两种情况,只要有对数是一个奇数一个偶数的话,那么通过交换1次就能满足条件,输出1;如果不存在这么一对数,则无论如何交换都不能满足条件,输出-1。

  无论如何,输出的结果只有3种可能1,-1,0。

 

   AC:

#include<stdio.h>
int main()
{
	int n,i;
	int le=0,ri=0,di=0;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
	  int a,b;
	  scanf("%d%d",&a,&b);
	  if((a+b)%2) di=1;
	  le+=a;
	  ri+=b;
	}
	if(!(le%2)&&!(ri%2))  printf("%d\n",0);
	else if((le+ri)%2) printf("%d\n",-1);
	else printf("%d\n",di?1:-1);
 	return 0;
}

 

    总结:

    思路不清晰,思考不够深入仔细;

 

 

你可能感兴趣的:(dom)