Codeforces 1198A MP3


传送门:MP3


MP3

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个数,告诉你一个可以存8*I位的磁盘,一组数需要存多少位取决于他数组中不同数到的个数K,
由k=log2K,k * n位这组数所需位数。
问你使这组数可以被该磁盘存下的,你所需要改变最少多少个数才行?


题解:
题目说实话不难。就是按他的意思模拟一下就行了,但是要剪枝!!!当给你的位数很多,同时n也相对较少,即8*I/n如果比20大的话,根本不需要考虑,一定输出0!!!!(因为n最大4 * 105)。
我就是这个地方没出来,在第46个样例一直过不去。。



AC代码:

 	#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=1e6+10;
ll n,l;
ll a[N];
ll num[N];
ll inv(ll a,ll n)//快速幂
{
  ll ans=1,base=a;
  while(n)
  {
    if(n&1)
    {
      ans=ans*base;
    }
    base=base*base;
    n>>=1;
  }
  return ans;
}
int main()
{
  map<ll,ll> m;//存每个数有多少个,计算变动量时加减即可
  vector<ll> v;
  scanf("%I64d%I64d",&n,&l);
  for(int i=0;i<n;i++)
  {
    scanf("%I64d",&a[i]);
  }
  sort(a,a+n);
  l=8*l;
  ll k=l/n;//k的最大值
  if(k>=20)//手动剪枝,没有加这个我一直wa,因为小k太大算不了大K
  {
    printf("0\n");
    return 0;
  }
  ll K=inv(2,k);//数值最多可以有K给不同的值
  m[a[0]]++;
  v.push_back(a[0]);
  for(int i=1;i<n;i++)
  {
    if(a[i]!=a[i-1])
    {
      v.push_back(a[i]);
    }
    m[a[i]]++;
  }
  ll diff=v.size();//数组里不同的数的个数
  if(diff<=K)//如果数组里最大的不同的数的个数小于K
  {
    printf("0\n");
    return 0;
  }
  else
  {
    int l=0,r=K-1;
    ll sum=0;
    for(int i=l;i<=r;i++)
    {
      sum+=m[v[i]];
    }
    ll minn=n-sum;
    l++;
    r++;
    while(r<diff)//类似于一个滑动窗口
    {
      sum=sum-m[v[l-1]]+m[v[r]];
      ll temp=n-sum;
      minn=min(minn,temp);
      l++;
      r++;
    }
    printf("%I64d\n",minn);
  }
  return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值