#include <bits/stdc++.h> #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long; using ld = long double; //#define int ll #define sz(x) ((int)(x).size()) using pii = pair<int,int>; using tii = tuple<int,int,int>; signed main() { cin.tie(0) -> sync_with_stdio(0); int n; cin >> n; deque<int> deq; vector<int> v(n), freq(n + 1); for(auto &x : v) cin >> x, freq[x]++; sort(all(v), [&](auto a, auto b) { return freq[a] < freq[b] || (freq[a] == freq[b] && a < b); }); //for(auto x : v) cerr << x << ' '; for(auto x : v) deq.emplace_back(x); //cerr << '\n'; int T = 0; while(!deq.empty()) { T++; int b = deq.back(), cnt = 0; while(!deq.empty() && deq.back() == b) deq.pop_back(), cnt++; //cerr << cnt << ' '; while(!deq.empty() && cnt > 1) deq.pop_front(), cnt--; //cerr << b << '\n'; } cout << T << '\n'; } /** Anul asta nu se da centroid -- Rugaciunile mele */
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 39 40 41 | #include <bits/stdc++.h> #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long; using ld = long double; //#define int ll #define sz(x) ((int)(x).size()) using pii = pair<int,int>; using tii = tuple<int,int,int>; signed main() { cin.tie(0) -> sync_with_stdio(0); int n; cin >> n; deque<int> deq; vector<int> v(n), freq(n + 1); for(auto &x : v) cin >> x, freq[x]++; sort(all(v), [&](auto a, auto b) { return freq[a] < freq[b] || (freq[a] == freq[b] && a < b); }); //for(auto x : v) cerr << x << ' '; for(auto x : v) deq.emplace_back(x); //cerr << '\n'; int T = 0; while(!deq.empty()) { T++; int b = deq.back(), cnt = 0; while(!deq.empty() && deq.back() == b) deq.pop_back(), cnt++; //cerr << cnt << ' '; while(!deq.empty() && cnt > 1) deq.pop_front(), cnt--; //cerr << b << '\n'; } cout << T << '\n'; } /** Anul asta nu se da centroid -- Rugaciunile mele */ |