[快速幂]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=log2Kk=⌈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 lrl≤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 }

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
2024-12-30 18:02
程序员牛肉:1.可以标记一下自己的学校是985,有一些hr可能没想到你这个院校是985的。 2.简历所呈现出来的能力还是有点差的,苍穹外卖+黑马点评。这在java技术域里面也就是刚学三四个月的样子,大厂现在招人少,小厂又更加希望你能直接过来干活。就你简历上呈现出来的能力,确实是有点难找,肉眼可见的不懂技术。 第一个项目中:简单的使用redis也算是亮点嘛?使用jwt,threadlocal也算是亮点?你不就是调了几个包嘛?Nginx作为服务器也能写出来,这不是前端的活嘛? 第二个项目中:分布式锁+mq消息队列+Lua队列。真没啥好问的。属于面试官看一眼就阳痿的简历,没有任何想提问的欲望。 我给你建议是好好的挖一挖这个项目吧,其实苍穹外卖和黑马点评这两个项目很不错了,只不过是太烂大街了导致面试官没啥问的兴趣,所以不太推荐写简历上。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务