http://codeforces.com/problemset/problem/1199/C
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.
题目大意:你可以任意选定
l
,
r
l,r
l,r,使得小于
l
l
l的元素都变为
l
l
l,大于
r
r
r的元素都变为
r
r
r,设变化后的序列中有
K
K
K个不同的元素,设
k
=
⌈
log
2
K
⌉
k=\left \lceil \log_{2}K \right \rceil
k=⌈log2K⌉,满足
k
∗
n
<
=
8
∗
I
k*n<=8*I
k∗n<=8∗I,求满足该条件最少需要改变几个元素。
思路:这题写法挺多的。考虑对原序列离散化、排序、去重,假设选取 a [ i ] 、 a [ j ] a[i]、a[j] a[i]、a[j]两个元素,那么此时有 j − i + 1 = K j-i+1=K j−i+1=K,设 Z = I ∗ 8 / n Z=I*8/n Z=I∗8/n,化简可得: j < = i + 2 Z − 1 j<=i+2^{Z}-1 j<=i+2Z−1,因此枚举 i i i就得到了 j j j,此时只需要计算位于 [ a [ i ] , a [ j ] ] [a[i],a[j]] [a[i],a[j]]内的元素个数即可。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=4e5+5;
int n,I;
int a[maxn],b[maxn];
int sum[maxn];
int main()
{
scanf("%d%d",&n,&I);
I*=8,I/=n;
I=min(I,20);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),b[i]=a[i];
sort(b+1,b+1+n);
int len=unique(b+1,b+1+n)-b-1;
for(int i=1;i<=n;i++)
{
a[i]=lower_bound(b+1,b+1+len,a[i])-b;
sum[a[i]]++;
}
for(int i=1;i<=len;i++)
sum[i]+=sum[i-1];
sort(a+1,a+1+n);
unique(a+1,a+1+n);
int dis=1<<I;
int ans=1e9;
if(dis>=len)
ans=0;
for(int i=1;i+dis-1<=len;i++)
{
int j=i+dis-1;
ans=min(ans,sum[len]-sum[a[j]]+sum[a[i]-1]);
}
printf("%d\n",ans);
return 0;
}