205D - Little Elephant and Cards - CodeForces Solution


binary search brute force sortings *1500

Please click on ads to support us..

C++ Code:

/*
ID: laithal2
TASK: beads
PROG: beads
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
#define ll      long long
#define ull     unsigned long long
#define _for(i, n)      for(ll i = 0; i < n; i++)
#define _forv(i, a, n)      for(ll i = a; i < n; i++)
#define _forvIncr(i, a, n, k)      for(ll i = a; i < n; i+=k)
#define _forvRev(i, a, n)      for(ll i = a; i >= n; i--)
#define _for1(i, n)         for(ll i = 1; i <= n; i++)
#define vbe(vec)    vec.begin(), vec.end()
#define uniqueVec(vec) vec.resize(distance(vec.begin(), unique(vbe(vec))));
#define mp(f,s)     make_pair(f, s)
#define pb(e) push_back(e)
#define cond_cout(cond) if(cond) puts("YES"); else puts("NO");
//#define endl '\n'
const int MOD = 1e9 + 7;
const int maxN = 1e6 + 1;

inline void f()
{
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
}


map<ll, ll> mpf, mpb;
int main()
{
    f();
    ll n;
    cin >> n;
    _for(i, n){
        int x, y;
        cin >> x >> y;
        if(x == y){
            mpf[x]++;
        }
        else{
            mpf[x]++;
            mpb[y]++;
        }
    }
    ll ans = 1e9;
    int h = ceil(n/2.0);
    for(auto p : mpf){
        if(p.second + mpb[p.first] >= h){
            ans = min(ans, max(0ll, h - p.second));
        }
    }
    if(ans == 1e9){
        for(auto p : mpb){
            if(p.second >= h) ans = h;
        }
    }
    if(ans == 1e9) ans = -1;
    cout << ans << endl;
    return 0;
}
/*

*/


Comments

Submit
0 Comments
More Questions

202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments