来源:CF-1199C
C. MP3
One common way of digitalizing sound is to record sound intensity at particular time moments. For each time moment intensity is recorded as a non-negative integer. Thus we can represent a sound file as an array of n non-negative integers.
If there are exactly K distinct values in the array, then we need k=⌈log2K⌉ bits to store each value. It then takes nk bits to store the whole file.
To reduce the memory consumption we need to apply some compression. One common way is to reduce the number of possible intensity values. We choose two integers l≤r, and after that all intensity values are changed in the following way: if the intensity value is within the range [l;r], we don’t change it. If it is less than l, we change it to l; if it is greater than r, we change it to r. You can see that we lose some low and some high intensities.
Your task is to apply this compression in such a way that the file fits onto a disk of size I bytes, and the number of changed elements in the array is minimal possible.
We remind you that 1 byte contains 8 bits.
k=⌈log2K⌉ is the smallest integer such that K≤2k. In particular, if K=1, then k=0.
Input
The first line contains two integers n and I (1≤n≤4⋅105, 1≤I≤108) — the length of the array and the size of the disk in bytes, respectively.
The next line contains n integers ai (0≤ai≤109) — the array denoting the sound file.
Output
Print a single integer — the minimal possible number of changed elements.
Examples
input
6 1
2 1 2 3 4 3
output
2
input
6 2
2 1 2 3 4 3
output
0
input
6 1
1 1 2 2 3 3
output
2
Note
In the first example we can choose l=2,r=3. The array becomes 2 2 2 3 3 3, the number of distinct elements is K=2, and the sound file fits onto the disk. Only two values are changed.
In the second example the disk is larger, so the initial file fits it and no changes are required.
In the third example we have to change both 1s or both 3s.
个人理解
这个题没有用到任何算法知识。
因为每个数字可能特别大,所以我们先离散化一下。
sort(a,a+n);cnt = unique(a,a+n)-a;
最大允许的不同数字的个数为2^(8*I/n)
,然后分两种情况讨论:
一、最大允许的不同数字个数大于等于已有的不同数字个数,直接打印0;
二、最大允许的不同数字个数小于已有的不同数字个数,那么就从已有的不同数字中取连续的固定长度为最大允许不同数字个数的一段,找包含数字最多的区间,那么这个区间就是需要去掉数字最小的区间。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[400010];
ll n,I;
map<int,int> mp;
ll cnt;
int main(){
scanf("%lld%lld",&n,&I);
for(int i = 0;i<n;i++){
scanf("%d",&a[i]);
mp[a[i]]++;
}
sort(a,a+n);
cnt = unique(a,a+n)-a;
if(8*I/n>=19 || (1LL<<(8*I/n))>=cnt){
puts("0");
}else{
ll l = 0,r = cnt-1;
ll m = 1LL<<(8*I/n);
ll sum = 0LL;
for(int i = 0;i<m;i++){
sum+=mp[a[i]];
}
ll res = n-sum;
for(int i = 0;i+m<=cnt;i++){
res = min(res,n-sum);
sum-=mp[a[i]];
sum+=mp[a[i+m]];
}
printf("%lld\n",res);
}
return 0;
}