题目链接:https://vjudge.net/problem/CodeForces-1198A
题目:
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 nn non-negative integers.
If there are exactly KK distinct values in the array, then we need k=⌈log2K⌉k=⌈log2K⌉ bits to store each value. It then takes nknk 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≤rl≤r, and after that all intensity values are changed in the following way: if the intensity value is within the range [l;r][l;r], we don't change it. If it is less than ll, we change it to ll; if it is greater than rr, we change it to rr. 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 II bytes, and the number of changed elements in the array is minimal possible.
We remind you that 11 byte contains 88 bits.
k=⌈log2K⌉k=⌈log2K⌉ is the smallest integer such that K≤2kK≤2k. In particular, if K=1K=1, then k=0k=0.
Input
The first line contains two integers nn and II (1≤n≤4⋅1051≤n≤4⋅105, 1≤I≤1081≤I≤108) — the length of the array and the size of the disk in bytes, respectively.
The next line contains nn integers aiai (0≤ai≤1090≤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=3l=2,r=3. The array becomes 2 2 2 3 3 3, the number of distinct elements is K=2K=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.
题意
给出一个大小为 n 的数组,和一个整数 I ;
大小为 n 的数组,如果数组中不同数值的个数为K,则需要 nk 位来装这些数,
你可以进行一个操作,选择一个数值区间 [ L ,R ] 把大于 R 的数变成 R ,小于 L 的数变成 L ;给出的整数 I 代表字节数,一个字节8个位;问如果要使这个的数组可以被 I 字节装下,至少有几个数会被改变。
思路很简单(需要剪枝),具体看代码。
AC代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<string>
using namespace std;
typedef long long ll;
ll n,k;
const int INF=4e5+5;
map<ll,ll>p;
ll quick(ll a,ll b)//快速幂
{
ll ans=1;
while(b)
{
if(b&1)
{
ans=ans*a;
}
a=a*a;
b=b/2;
}
return ans;
}
struct node{
ll x,d;
}f[INF];
bool cmp(node u,node v)
{
return u.x <v.x ;
}
int main()
{
scanf("%lld%lld",&n,&k);
ll i,j;
ll sum=0;//统计不同数值的个数
for(i=1;i<=n;i++)
{
scanf("%lld",&j);
if(p.count(j)==0 ){
p[j]=1;sum++;
}
else {
p[j]++;//每个值出现的次数
}
}
// double mp=log(sum)/log(2);//值不精确,卡在第109个样例
// if(mp*n<=k*8)
// {
// printf("0");
// return 0;
// }
ll aa=8*k/n;
if(aa>=20)//这样剪枝就AC了。
{
printf("0");
return 0;
}
ll m=quick(2,aa);//计算区间保留的最大长度
ll cnt=0;
map<ll,ll>::iterator it=p.begin();
for(;it!=p.end();++it)
{
f[++cnt].x =it->first;
f[cnt].d =it->second;
}
sort(f+1,f+1+cnt,cmp);//依据x的值的大小进行排序
ll s=0;
//确定了区间长度,枚举每个区间
for(j=1;j<=m;j++)
{
s+=f[j].d ;
}
ll ss=s;
for(i=2,j=m+1;j<=cnt;i++,j++)
{
ss=ss+f[j].d -f[i-1].d ;
s=max(ss,s);
}
printf("%lld",n-s);
return 0;
}