【CodeForces - 1199C】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?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
input
Copy
6 1 2 1 2 3 4 3output
Copy
2input
Copy
6 2 2 1 2 3 4 3output
Copy
0input
Copy
6 1 1 1 2 2 3 3output
Copy
2Note
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.
?
題目大意:
?
解題報告:
? 根據給定的I可以求出來可以保留的不同數字的個數,然后枚舉的同時記錄最小值就行了。注意首先要保證算的logg不能爆longlong,還有一點就是判斷logg和LEN的長度關系,因為否則的話下面遍歷的時候就炸了。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 5e5 + 5; ll a[MAX],I,b[MAX]; int sum[MAX]; ll logg; int n; int LEN; int get(ll x) {return lower_bound(b+1,b+LEN+1,x) - b; } ll qpow(ll a,ll b) {ll res = 1;while(b) {if(b&1) res = res*a;b>>=1;a=a*a;}return res; } int main() {cin>>n>>I;I*=8;ll logg = min(I/n,31*1LL);logg = qpow(2,logg);for(int i = 1; i<=n; i++) scanf("%lld",a+i);sort(a+1,a+n+1);for(int i = 1; i<=n; i++) b[i] = a[i];LEN = unique(b+1,b+n+1) - b - 1;for(int i = 1; i<=n; i++) sum[get(a[i])]++;for(int i = 1; i<=LEN; i++) {sum[i] += sum[i-1];}if(logg >= LEN) {printf("0\n");return 0;}int out = 0x3f3f3f3f;for(int i = 1; i<=LEN; i++) {int l = i;int ans = i + logg-1;out = min(out,n - (sum[ans] - sum[i-1]));}printf("%d\n",out);return 0; }?
總結
以上是生活随笔為你收集整理的【CodeForces - 1199C】MP3(思维,离散化)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【蓝桥杯官网试题 - 历届试题】格子刷
- 下一篇: 《戴森球计划》新版本无MOD控制台使用方