-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path204-B.cpp
33 lines (29 loc) · 801 Bytes
/
204-B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
greedy
difficulty: medium
date: 02/Apr/2020
problem: given the two-sided values of N cards, what is the minimum number of turns in the cards so that at least half of them are the same front?; they are initially facing upwards
by: @brpapa
*/
#include <iostream>
#include <set>
using namespace std;
int main() {
int N; cin >> N;
multiset<int> front, back;
set<int> values;
for (int n = 0; n < N; n++) {
int f, b; cin >> f >> b;
front.insert(f), values.insert(f);
if (b != f) back.insert(b), values.insert(b);
}
int ans = N+1;
for (int v : values) {
int a = front.count(v);
int b = back.count(v);
int t = (N+1)/2;
if (a+b >= t) ans = min(ans, max(0, t-a));
}
cout << (ans == N+1? -1: ans) << endl;
return 0;
}