#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define PB push_back
typedef long long ll;
typedef long double ld;
#define REP(x,y) for(int x=0;x<(y);x++)
#define ROF(x,y) for(int x=(y);x>0;x--)
#define FOR(x,z,y) for(int x=(z);x<(y);x++)
#define INT(x) int x;scanf("%d",&x)
#define LL(x) ll x;scanf("%lld",&x)
#define CZ(x) char x;scanf(" %c",&x)
typedef pair<int,int> pii;
typedef pair<int,pii> piii;
unordered_map<int,int> mp;
vector<int> vec;
int main(){
INT(n);
REP(i,n){
INT(x);
mp[x]++;
}
for (const auto &s : mp) vec.PB(s.second);
sort(vec.begin(), vec.end(), greater<int>());
int res=1,ptr=0,b_ptr=vec.size()-1;
while (1){
if(vec[ptr]>n/2){
printf("%d\n",res);
return 0;
}
int size = vec[ptr]-1;
n-=vec[ptr++]*2-1;
res++;
while (1){
if(size<=vec[b_ptr]){
vec[b_ptr]-=size;
break;
}
size-=vec[b_ptr--];
}
}
}
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 | #include<bits/stdc++.h> using namespace std; #define f first #define s second #define PB push_back typedef long long ll; typedef long double ld; #define REP(x,y) for(int x=0;x<(y);x++) #define ROF(x,y) for(int x=(y);x>0;x--) #define FOR(x,z,y) for(int x=(z);x<(y);x++) #define INT(x) int x;scanf("%d",&x) #define LL(x) ll x;scanf("%lld",&x) #define CZ(x) char x;scanf(" %c",&x) typedef pair<int,int> pii; typedef pair<int,pii> piii; unordered_map<int,int> mp; vector<int> vec; int main(){ INT(n); REP(i,n){ INT(x); mp[x]++; } for (const auto &s : mp) vec.PB(s.second); sort(vec.begin(), vec.end(), greater<int>()); int res=1,ptr=0,b_ptr=vec.size()-1; while (1){ if(vec[ptr]>n/2){ printf("%d\n",res); return 0; } int size = vec[ptr]-1; n-=vec[ptr++]*2-1; res++; while (1){ if(size<=vec[b_ptr]){ vec[b_ptr]-=size; break; } size-=vec[b_ptr--]; } } } |
English