C. MP3

题目

outputstandard 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⌉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
inputCopy
6 1
2 1 2 3 4 3
outputCopy
2
inputCopy
6 2
2 1 2 3 4 3
outputCopy
0
inputCopy
6 1
1 1 2 2 3 3
outputCopy
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.

/*一*/
#include<stdio.h>
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
ll a[1000010],b[1000010],sum[1000010];
int main()
{
    int i,n;
    ll I,ans;
    scanf("%d %d",&n,&i);
    i=min(8*i/n,20);
    I=pow(2,i);//n一共4*1e5,所以I不会超过20,
    for(i=1;i<=n;i++)
    {
        scanf("%I64d",&a[i]);
        b[i]=a[i];
    }
    memset(sum,0,sizeof(sum));
    sort(a+1,a+1+n);//有序数组用unique函数
    int len=unique(a+1,a+1+n)-a-1;//要减去数组首地址
    for(i=1;i<=n;i++)
    {
        ll x=lower_bound(a+1,a+len+1,b[i])-a;//离散化,表示下标为i的元素在原数组中第几小
        sum[x]++;//去重排序后的a[i]对应有sum[i]个
    }
    for(i=1;i<=len;i++)
    {
        sum[i]+=sum[i-1];
    }
    if(I>=len)
        printf("0\n");
    else
    {
        ans=inf;
        for(i=1;i<=len;i++)
        {
            int l=i+I-1;
            ans=min(ans,n-(sum[l]-sum[i-1]));
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

%lld相当于%I64d


   /*  二*/
    //没有离散化。给出现的数排了序,每个不同的数用一个a[i]表示,再用一个b[i]表示这个数出现了几次(i是同一个i)
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int inf=0x3f3f3f3f;
    const int N=1e6;
    ll a[N],b[N];
    int main()
    {
        priority_queue<int,vector<int>, greater<int> >q;//相当于一个排了序的数组
        int n,I,i,len;
        ll k,x,ans;
        cin>>n>>I;
        I=min(8*I/n,20);
        k=pow(2,I);
        for(i=1; i<=n; i++)
        {
            cin>>x;
            q.push(x);
        }
        len=1;
        a[1]=q.top();
        b[1]=1;
        q.pop();
        while(!q.empty())
        {
            if(q.top()==a[len])
            {
                b[len]++;
                q.pop();
            }
            else
            {
                a[++len]=q.top();
                q.pop();
                b[len]=1;
            }
        }
        if(k>=len)
            cout<<0<<endl;
        else
        {
            for(i=1;i<=len;i++)
            {
                b[i]+=b[i-1];
            }
            ans=inf;
            for(i=1; i<=len; i++)
            {
                ans=min(ans,n-(b[i+k-1]-b[i-1]));
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值