codeforces 1198A A-MP3 排序+离散+前缀和+快速幂

A. MP3
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
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 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
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=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和l,n代表n个长度的序列,l代表给你的内存大小(单位是byte=8位),然后你要取一个区间范围,在范围之外的全部改成范围的左边界和右边界,使得尽量把给的空间塞满,问你最少改多少个。(使用的内存大小 = nk)

思路:首先,先求出最大长度K。这题的范围很大,1e9,开数组要爆,但是给的n确只有4e5,所以考虑离散化,将数组开到40w来储存数据,其次将这些数据从小到大排序,遍历统计每个值的数量以及整个数列的不同值个数。快速幂和前缀和来优化时间。最后:有个需要注意的点,在打之前要算好K的值可能会爆long long,所以要先处理,算出大于等于n是2的几次方,比较即可。

AC code:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
long long a[500000],b[500000],c[500000];
long long mi(int x)  //快速幂
{
    long long ans=1;
    long long as=2;
    while(x!=0)
    {
        if(x%2)
            ans*=as;
        as*=as;
        x>>=1;
    }
    return ans;
}
int main()
{
    long long n,l;
    cin>>n>>l;
    for(int i=0; i<n; i++)
      cin>>a[i];
    long long len = (l*8)/n;
    long long ss;
    for(int i=0;; i++)  //和原长度比较,防止数据溢出
    {
        if(mi(i)>=n)
        {
            ss = i;
            break;
        }
    }
    if(ss<=len)  printf("0\n");
    else
    {
        long long K = mi(len);
        sort(a,a+n);
        int p=1;
        for(int i=0; i<n-1; i++)  //每种类型多少个
        {
            if(a[i]==a[i+1])
                b[p]++;
            else
            {
                b[p]++;
                p++;
            }
        }
        b[p]++;
        if(p<=K) printf("0\n");
        else
        {
            c[1]=b[1];
            for(int i=2; i<=p; i++)  //求前缀和
            {
                c[i] = b[i]+c[i-1];
            }
            long long ans = -1;
            c[0]=0;
            for(int i=0; i<=p-K; i++)  
            {
                ans = max(ans,c[i+K]-c[i]);
            }
            printf("%lld\n",n-ans);
        }
    }
    return 0;
}

总结:其实这道题不难想,但这网站卡了我半小时界面进不去。。。。小小吐槽一下。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值