353A - Domino - CodeForces Solution


implementation math *1200

Please click on ads to support us..

Python Code:

number = int(input())
dominoes = []
for i in range(number):
    domino = list(map(int, input().split()))
    dominoes.append(domino)

left = 0
right = 0
for i in range(len(dominoes)):
    left += dominoes[i][0]
    right += dominoes[i][1]

different = 0
for i in range(len(dominoes)):
    if (dominoes[i][0] % 2 == 0 and dominoes[i][1] % 2 == 1) or (dominoes[i][0] % 2 == 1 and dominoes[i][1] % 2 == 0):
        different += 1

if left % 2 == 0 and right % 2 == 0:
    print(0)
elif left % 2 == 1 and right % 2 == 1 and number != 1 and different >= 2:
    print(1)
else:
    print(-1)

C++ Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <any>

using namespace std;

#ifdef LOCAL
#define eprintf(...)                  \
    {                                 \
        fprintf(stderr, __VA_ARGS__); \
        fflush(stderr);               \
    }
#else
#define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template <typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
int MOD = 1e9 + 7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B)
{
    return (ull)rng() % B;
}


#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define print(...) cout << ... << endl;
#define nl "\n"
#define REPL(x, n) for(int x = 0; x < n; ++x)


clock_t startTime;
double getCurrentTime() {
    return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}

void printiv(vector<ll> v) {
    for (int ele : v) {
        cout << ele << " ";
    }
    cout << endl;
}



int main() {
    int n;
    int a,b;
    int lsum=0, rsum=0;
    bool pivot = false;
    cin >> n;
    REPL(i ,n ) {
        cin >> a >> b;
        if ((a % 2 == 1 && b % 2 == 0) || (a % 2 == 0 && b % 2 == 1)) {
            pivot = true;
        }
        lsum += a; rsum += b;
    }

    if (lsum % 2 == 0 && rsum % 2 == 0) {
        cout << 0 << nl;
    } else if((lsum + rsum) % 2 == 0) {
        if(pivot) {
            cout << 1 << nl;
        } else {
            cout << -1 << nl;
        }
    } else {
        cout << -1;
    }



    return 0;
}


Comments

Submit
0 Comments
More Questions

74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations