CF#576 div2 C

探讨了声音文件数字化及压缩技术,通过调整强度值范围减少内存消耗,目标是在限定磁盘空间内存储文件,同时最小化修改元素数量。文章详细解析了算法实现,包括离散化处理、前缀和计算及最优参数选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://codeforces.com/contest/1199
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.
一个长度为n的数组中不同的数字数为k,需要nlog2(k)(向上取整)的空间储存,总的储存空间大小为8I,有一种操作:设置l和r,将数组中大于r的修改成r,小于l的修改成l,询问最小修改次数,
先离散化一下,得到初始k,如果一开始需要的储存空间小于8*I的话则直接输出0,
不然的话遍历一次离散化后的数组得到前缀和,再遍历一次得到最大能保存的数字,得到答案

#include<stdio.h>
#include<math.h> 
#include<algorithm>
#include<vector>
using namespace std;
int n,I,a[400005],b[400005];
int sum[400005];
int main()
{
	scanf("%d%d",&n,&I);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	} 
	sort(a+1,a+1+n);
	int cnt=1,num=1;
	int last=a[1];
	for(int i=2;i<=n;i++)
	{
		if(a[i]==last)
		{
			cnt++;
		}
		else
		{
			b[num++]=cnt;
			cnt=1;
			last=a[i];
		}
	}
	b[num]=cnt;
	sum[0]=0;
	for(int i=1;i<=num;i++)
	{
        sum[i]=sum[i-1]+b[i];
    }
    int tep=n*ceil(log2(num));
	if(tep<=8*I)
	{
		printf("0\n");
		return 0;
	}
	int k=8*I/n;
	int maxk=pow(2,k);
    int maxleave=sum[maxk-1];
    for(int i=1;i+maxk<=num;i++)
	{
        maxleave=max(maxleave,sum[i+maxk-1]-sum[i-1]);
    }
    printf("%d\n",n-maxleave);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值