#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for(int i = 0; i < n; i++) { cin >> a[i]; } int candidate = -1, count = 0; for(int i = 0; i < n; i++) { if(count == 0) { candidate = a[i]; count = 1; } else if(candidate == a[i]) { count++; } else { count--; } } count = 0; for(int i = 0; i < n; i++) { if(a[i] == candidate) { count++; } } if(count > n / 2) { cout<<1; } else { cout<<2; } return 0; }
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 34 35 36 37 38 | #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for(int i = 0; i < n; i++) { cin >> a[i]; } int candidate = -1, count = 0; for(int i = 0; i < n; i++) { if(count == 0) { candidate = a[i]; count = 1; } else if(candidate == a[i]) { count++; } else { count--; } } count = 0; for(int i = 0; i < n; i++) { if(a[i] == candidate) { count++; } } if(count > n / 2) { cout<<1; } else { cout<<2; } return 0; } |