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 分几种情况,两个和都为偶数,至少有一个为奇数。 下面是代码:#include<iostream> #include<set> #include<map> #include<vector> #include<queue> #include<cmath> #include<climits> #include<cstdio> #include<string> #include<cstring> #include<algorithm> typedef long long LL; using namespace std; const int MAX=1000; int N; int upper[MAX],low[MAX]; int odd1,even1,odd2,even2; int sum1,sum2; int main() { while(cin>>N) { sum1=sum2=odd1=even1=odd2=even2=0; for(int i=1; i<=N; i++) { cin>>upper[i]>>low[i]; sum1+=upper[i]; sum2+=low[i]; if(upper[i]%2) odd1++; else even1++; if(low[i]%2) odd2++; else even2++; } if(sum1%2==0&&sum2%2==0) { cout<<0<<endl; continue; } if(sum1%2==1||sum2%2==1) { int i; for(i=1; i<=N; i++) if((sum1-upper[i]+low[i])%2==0&&(sum2-low[i]+upper[i])%2==0) { cout<<1<<endl; break; } if(i>N) cout<<-1<<endl; continue; } } return 0; }