[快速幂]Codeforces Round #576 (Div. 2)-C. MP3

C. MP3 time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

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=⌈log2⁡K⌉ 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⌉ is the smallest integer such that K≤2k. In particular, if K=1, then k=0.

Input

The first line contains two integers nn and II (1≤n≤4e5, 1≤I≤1e8) — the length of the array and the size of the disk in bytes, respectively.

The next line contains nn integers aiai (0≤≤ai≤1e9) — the array denoting the sound file.

Output

Print a single integer — the minimal possible number of changed elements.

Examples input Copy
6 1
2 1 2 3 4 3
output Copy
2
input Copy
6 2
2 1 2 3 4 3
output Copy
0
input Copy
6 1
1 1 2 2 3 3
output Copy
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个数,求最少删去多少个数才能满足剩下数的个数的种类小于等于2^(8*I/n),令K是现在数的种类,I是给出的byte,k是由K算出的bit,注意1 byte contains 8 bits,k=log2(K),n*k<=8*I,k<=8*I/n,K<=2^(k)<=2^(8*I/n),所以剩下的数的种类要<=2^(8*I/n)

注意:1 byte contains 8 bits

   K<=4e5 -> ->K在int范围内 -> K<=2^(32)-1 rg<=32,不限制这个的话快速幂那会爆long long

   要删去最少的数目,而用总数减去剩下的就是要删掉的,剩下的个数的种类是need,可用前缀和求出各种情况下need种数的个数

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int amn=4e5+5,inf=0x3f3f3f3f;
 5 int a[amn],b[amn],c[amn];
 6 map<int,int> mp;
 7 ll qp(ll in){   ///快速幂
 8     ll ans=1,t=2;
 9     while(in){
10         if(in&1)ans*=t;
11         in>>=1;
12         t*=t;
13     }
14     return ans;
15 }
16 int main(){
17     int n,I,tp=0;
18     cin>>n>>I;
19     ll rg=8*I/n;    ///k=log2(K),n*k<=8*I,k<=8*I/n
20     if(rg>32)rg=32;/// K<=4e5 -> ->K在int范围内 -> K<=2^(32)-1 rg<=32,不限制这个的话快速幂那会爆long long
21     for(int i=1;i<=n;i++){
22         cin>>a[i];
23         if(!mp[a[i]])       ///如果a[i]没被统计过
24             b[++tp]=a[i];   ///统计数有多少种
25         mp[a[i]]++;         ///统计数的个数
26     }
27     ll need=qp(rg);         ///快速幂计算最后要剩多少个,K<=2^(k)<=2^(8*I/n)
28     if(tp<=need)printf("0\n");
29     else{
30         sort(b+1,b+1+tp);   ///区间中从小到大
31         c[0]=0;
32         for(int i=1;i<=tp;i++){
33             c[i]=c[i-1]+mp[b[i]];   ///数的个数作前缀和
34         }
35         ll ans=inf;
36         for(int i=1;i<=tp-need;i++){
37             ans=min(ans,(ll)n-(c[i+need]-c[i]));    ///要删去最少的数目,而用总数减去剩下的就是要删掉的,剩下的个数的种类是need,可用前缀和求出各种情况下need种数的个数
38         }
39         printf("%lld\n",ans);
40     }
41 }

 

上一篇:【CodeForces - 1199C】MP3(思维,离散化)


下一篇:如何利用python爬虫爬取音乐