ZCMU-1312: Domino

I've been busy with professional changes these days, I haven't updated much, and I haven't written much about the topic. Take a moment to spare today and look at a number thesis of odd and even numbers.

Article Directory

topic


Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 187 Solved: 78
[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

2
4 2
6 4
1
2 3
3
1 4
2 3
4 4

Sample Output

0
-1
1

idea

This question is to give you an n number, and then there are 2n sets of data, the first n are stored in the a array, and the last n are stored in the b array, asking you whether you can reach the sum of the two sets of data by exchanging the upper and lower sets of data The result is an even number, if it can be output, it takes a few seconds (one second for one exchange), and -1 is output if it is not.

Let's first discuss the result of adding odd and even numbers.
Even number + even number = even
number Odd number + odd number = even
number Odd number + even number = odd number/even number

Then if the a and b arrays both abstract the data and treat them as random occurrences of even and odd numbers, this question becomes more concrete.

According to the first and second clauses of the formula, we can ignore the influence of pairs of even and odd numbers on the answer, that is, after excluding those numbers, the following situations are left

1. Even number of odd numbers, obviously can form even number of even number pairs (two pairs of pairs), then directly output 0 (no need to exchange)
2. Both upper and lower odd numbers are odd numbers, then if we want to satisfy the meaning of the question, we need to eliminate one How can the number and the exchange below meet the conditions? Let’s look at formula 3. If a[i]+b[i] is an odd number, it will obviously be an odd number and an even number. Then only need to exchange, so that the above group can satisfy the addition and the even number (even number Odd pair + an even number), then

3. Other conditions can not be satisfied, we directly output -1

AC code

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
 
    int n,i;
    int a[105],b[105];
    while(cin>>n){
    
    
           int fa=0,fb=0;
           for(i=0;i<n;i++){
    
    
               cin>>a[i]>>b[i];
               if(a[i]&1) fa++;
               if(b[i]&1) fb++;
           }
           if(fa%2==0&&fb%2==0){
    
    
               cout<<0;
           }
           else if((fa&1)&&(fb%2==1)){
    
    
               bool f=false;
               for(i=0;i<n;i++){
    
    
                   if((a[i]+b[i])&1) f=1;
                   if(f) break;
               }
               if(f) cout<<1;
               else cout<<-1;
           }
           else{
    
    
               cout<<-1;
           }
           cout<<endl;
    }
      return 0;
      }

to sum up

Recently, there are fewer topics to write, so I need time to write!

Guess you like

Origin blog.csdn.net/DAVID3A/article/details/114945316
Recommended