[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 ...
随机推荐
- postman(二):详解在Pre-request Script中如何执行请求
一.这里以Pre-request Script标签来介绍 postman提供了postman提供了一个"Send a request"代码段,他是已经封装好的发送请求的方法 一个& ...
- SpringToolSuit(STS)添加了Lombok后仍然报错
参见这篇博客 https://blog.csdn.net/qq_27442469/article/details/90612918 上面步骤做完后,在项目右键->Maven->Update ...
- Spring Cloud第十二篇 | 消息总线Bus
本文是Spring Cloud专栏的第十二篇文章,了解前十一篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring ...
- python中strftime和strptime函数
strftime和strptime函数均来自包datetime from datetime import * strftime: 将datetime包中的datetime类,按照入参格式生成字符串变量 ...
- 两个jquery编写插件实例
(1) 封装基于jq弹窗插件 相信码友们对于$.fn.extexd();$.extend()以及$.fn.custom和$.custom都有一定的了解:我阐述一下我自己对于$.fn.custom和 ...
- 【leetcode】1154. Day of the Year
题目如下: Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the ...
- Ubuntu 16.04 + GTX970 + cuda8.0.44安装配置等问题(转)
参考:https://blog.csdn.net/u010094199/article/details/54380086 参考:https://blog.csdn.net/jonms/article/ ...
- C++STL手写版
手写STL,卡常专用. node为变量类型,可以自由定义,以下不再赘述. 1.stack(栈) 开一个数组,和一个top指针,压栈时++,弹栈时--即可. struct stack{ int tp;n ...
- [JSOI2008]最大数 题解
前言 巨佬说:要有线段树,于是蒟蒻就开始打线段树. 蒟蒻只能拿之前0分的板子题开刀了QAQ. 题解 一开始我以为插入操作不带取模,于是打了下面这个弱智玩意 下面的代码是会WA的 #include &l ...
- 【BZOJ4337】树的同构(树同构,哈希)
题意: 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如果能够把树T1T ...