#include <bits/stdc++.h> #define ll long long #define llu long long unsigned #define ld long double #define fr(i,n) for(int i=0;i<n;i++) #define watch(x) cout<<(#x)<<"=="<<(x)<<endl #define ft first #define sc second #define mp make_pair #define pb push_back #define vi vector<int> #define pii pair<int,int> #define pll pair<ll,ll> #define P 1000000007llu #define N 500005 #define M 1000001 #define LC 262144 using namespace std; int getchar_pos_int() { int n=0, c=getchar(); while('0'<=c&&c<='9') {n=n*10+c-'0'; c=getchar();} return n; } int a[N], n; vi frequencies; void getFrequencies() { int currFreq=1; for(int i=1;i<n;i++) { if(a[i]!=a[i-1]) { frequencies.pb(currFreq); currFreq=1; } else currFreq++; } frequencies.pb(currFreq); } int minSequences() { int res=0; for(int freq: frequencies) { res++, n-=(2*freq-1); if(n<=0) return res; } return res; } int main() { n=getchar_pos_int(); fr(i,n) a[i]=getchar_pos_int(); sort(a,a+n); getFrequencies(); sort(frequencies.begin(),frequencies.end()); reverse(frequencies.begin(),frequencies.end()); printf("%d\n",minSequences()); 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <bits/stdc++.h> #define ll long long #define llu long long unsigned #define ld long double #define fr(i,n) for(int i=0;i<n;i++) #define watch(x) cout<<(#x)<<"=="<<(x)<<endl #define ft first #define sc second #define mp make_pair #define pb push_back #define vi vector<int> #define pii pair<int,int> #define pll pair<ll,ll> #define P 1000000007llu #define N 500005 #define M 1000001 #define LC 262144 using namespace std; int getchar_pos_int() { int n=0, c=getchar(); while('0'<=c&&c<='9') {n=n*10+c-'0'; c=getchar();} return n; } int a[N], n; vi frequencies; void getFrequencies() { int currFreq=1; for(int i=1;i<n;i++) { if(a[i]!=a[i-1]) { frequencies.pb(currFreq); currFreq=1; } else currFreq++; } frequencies.pb(currFreq); } int minSequences() { int res=0; for(int freq: frequencies) { res++, n-=(2*freq-1); if(n<=0) return res; } return res; } int main() { n=getchar_pos_int(); fr(i,n) a[i]=getchar_pos_int(); sort(a,a+n); getFrequencies(); sort(frequencies.begin(),frequencies.end()); reverse(frequencies.begin(),frequencies.end()); printf("%d\n",minSequences()); return 0; } |