[Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案)
题面
给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r。每次操作的代价为改变数的个数。问:要让操作后序列里不同数的个数\(k\)满足$n \lceil \log _2 k\rceil \leq 8I $,操作的最小代价
分析
首先把a离散化,这样[l,r]都在\(10^5\)的级别。枚举l,发现r显然有单调性。操作后不同数的个数随r的增大而减小(考虑极端情况l=r,操作后所有数相同。若\(r_1\)满足条件,那\(r_2<r_1\)也一定满足条件。而操作的最小代价随r增大而减小。所以我们只需要对于每个l找到最大的满足条件的r即可。
考虑判定:我们把a排序,记\(cnt(l,r)\)表示满足\(a_i \in [l,r]\)的\(i\)的个数,m为离散化后的最大值。那么操作的代价为\(cnt(1,l-1)+cnt(r+1,m)\),不同数的个数为\(r-l+1\)(操作完之后最小值变成l,最大值变成r,由于我们离散化过,[l,r]内的数一定是连续的,所有个数为[r-l+1])
那么我们只要预处理前缀和\(sum(v)\)表示整个序列值<=v的个数,离散化后可以\(O(n)\)预处理,\(cnt(l,r)=sum(r)-sum(l-1)\)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define INF 0x3f3f3f3f
#define maxn 800000
using namespace std;
int n,m,I;
int a[maxn+5];
int b[maxn+5];
int sum1[maxn+5];
inline int cnt(int l,int r){
return sum1[r]-sum1[l-1];
}
int check(int lb,int rb){
int ans1=cnt(1,lb-1);
int ans2=cnt(rb+1,m);
int k=rb-lb+1;
// int cnt=2+sum1[lb+1]+sum1[rb-1];
if(ceil(log2(k))*n<=I*8) return ans1+ans2;
else return -1;
}
int bin_search(int t){
int l=t,r=m;
int ans=INF;
int mid;
while(l<=r){
mid=(l+r)>>1;
int val=check(t,mid);
if(val!=-1){
ans=val;
l=mid+1;
}else r=mid-1;
}
return ans;
}
int mark[maxn+5];
int main(){
// freopen("input.txt","r",stdin);
scanf("%d",&n);
scanf("%d",&I);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(a+1,a+1+n);
sort(b+1,b+1+n);
m=unique(b+1,b+1+n)-b-1;
for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+1+m,a[i])-b;
for(int i=1;i<=n;i++){
sum1[a[i]]++;
}
for(int i=1;i<=m;i++) sum1[i]+=sum1[i-1];
// printf("%d\n",m);
int ans=INF;
for(int l=1;l<=m;l++){
int tmp=bin_search(l);
ans=min(ans,tmp);
}
printf("%d\n",ans);
}
[Codeforces 1199C]MP3(离散化+二分答案)的更多相关文章
- Codeforces 772A Voltage Keepsake - 二分答案
You have n devices that you want to use simultaneously. The i-th device uses ai units of power per s ...
- 2018.12.08 codeforces 939E. Maximize!(二分答案)
传送门 二分答案好题. 题意简述:要求支持动态在一个数列队尾加入一个新的数(保证数列单增),查询所有子数列的 最大值减平均值 的最大值. 然而网上一堆高人是用三分做的. 我们先考虑当前的答案有可能由什 ...
- Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案
Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...
- codeforces 633D - Fibonacci-ish 离散化 + 二分查询
Fibonacci-ish Yash has recently learnt about the Fibonacci sequence and is very excited about it. He ...
- Codeforces - 1199C - MP3 - 尺取
https://codeforc.es/contest/1199/problem/C 擦,最后移位运算符溢出了,真的蠢. 肯定是选中间的连续的某段是最优的,维护这个段的长度和其中的元素种类就可以了.小 ...
- Codeforces 700A As Fast As Possible(二分答案)
[题目链接] http://codeforces.com/problemset/problem/700/A [题目大意] 有一辆限载k人速度为v2的车,n个步行速度均为v1的人要通过一段长度为l的距离 ...
- Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)
链接:http://codeforces.com/contest/484/problem/E 题意: 给你n个数的,每个数代表高度: 再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值: ...
- Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论
n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
随机推荐
- springcloud 配置actuator
pom.xml <!--Spring Boot Actuator,感应服务端变化--> <dependency> <groupId>org.springframew ...
- py-R-FCN的caffe配置(转)
参考:https://blog.csdn.net/wei_guo_xd/article/details/74451443 下载程序,git clone https://github.com/Orpin ...
- POJ 2778 DNA Sequence ( AC自动机、Trie图、矩阵快速幂、DP )
题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析 : 这题搞了我真特么久啊,首先你需要知道的前置技能包括 AC自动机.构建Trie图.矩阵快速幂,其中矩 ...
- 10分钟学会React Context API
Create-react-app来学习这个功能: 注意下面代码红色的即可,非常简单. 在小项目里Context API完全可以替换掉react-redux. 修改app.js import React ...
- DOS命令里面的EQ、NE、GT、LT、GE、LE分别是什么意思
EQ 就是 EQUAL等于NQ 就是 NOT EQUAL不等于 GT 就是 GREATER THAN大于 LT 就是 LESS THAN小于 GE 就是 GREATER THAN OR EQUAL 大 ...
- R 配色(挑花眼的色卡19色)
cols =c("#999999","#FF0099", "#E69F00", "#56B4E9", "#00 ...
- IOC和AOP使用扩展之AOP详解实现类
摘要: “Depend on yourself” is what nature says to every man. Parents can help you. Teachers can hel ...
- Java如何进行Base64的编码(Encode)与解码(Decode)
关于base64编码Encode和Decode编码的几种方式 Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个Binary资料和字串资料彼此之间是可以互相转换的,十分方便 ...
- 《SQL Server 2012 T-SQL基础》读书笔记 - 8.数据修改
Chapter 8 Data Modification SQL Server 2008开始,支持一个语句中插入多行: INSERT INTO dbo.Orders (orderid, orderdat ...
- glm初试,关于行矩阵列矩阵问题
/*** * glm中矩阵是行优先存储的,这不同于opengl默认的以列优先存储的方式??,以下面矩阵mat为例 * 它是用四个行向量来模拟存储四个行:vec4 value[4],其中 * value ...