分析

首先按小到大排序,考虑枚举两个都喜欢的个数\(i\)

那么只喜欢一个的个数各需要\(k-i\),剩下要补充到\(m-k*2+i\)个,

考虑用对顶堆维护大根堆大小仅有\(m-k*2+i\)即可


代码

#include <cstdio>
#include <cctype>
#include <algorithm>
#define rr register
using namespace std;
const int N=200011; typedef long long lll; lll ans=1e18,sum;
int n1,n2,n3,like[N],A[N],B[N],C[N],n,m,k,o[N];
inline signed iut(){
rr int ans=0; rr char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans;
}
inline lll min(lll a,lll b){return a<b?a:b;}
inline void Swap(int &a,int &b){rr int t=a; a=b; b=t;}
struct Max_Heap{
int cnt,heap[N];
inline void Push(int now){
heap[++cnt]=now;
rr int x=cnt;
while (x>1){
if (heap[x]>heap[x>>1])
Swap(heap[x>>1],heap[x]),x>>=1;
else return;
}
}
inline void Pop(){
heap[1]=heap[cnt--];
rr int x=1;
while ((x<<1)<=cnt){
rr int y=x<<1;
if (y<cnt&&heap[y+1]>heap[y]) ++y;
if (heap[y]>heap[x]) Swap(heap[y],heap[x]),x=y;
else return;
}
}
}heap1;
struct Min_Heap{
int cnt,heap[N];
inline void Push(int now){
heap[++cnt]=now;
rr int x=cnt;
while (x>1){
if (heap[x]<heap[x>>1])
Swap(heap[x>>1],heap[x]),x>>=1;
else return;
}
}
inline void Pop(){
heap[1]=heap[cnt--];
rr int x=1;
while ((x<<1)<=cnt){
rr int y=x<<1;
if (y<cnt&&heap[y+1]<heap[y]) ++y;
if (heap[y]<heap[x]) Swap(heap[y],heap[x]),x=y;
else return;
}
}
}heap2;
signed main(){
n=iut(); m=iut(); k=iut();
for (rr int i=1;i<=n;++i) o[i]=iut();
for (rr int T=iut();T;--T) like[iut()]=1;
for (rr int T=iut();T;--T) like[iut()]|=2;
for (rr int i=1;i<=n;++i){
switch (like[i]){
case 0:heap2.Push(o[i]); break;
case 1:A[++n1]=o[i]; break;
case 2:B[++n2]=o[i]; break;
case 3:C[++n3]=o[i]; break;
}
}
sort(A+1,A+1+n1),sort(B+1,B+1+n2),sort(C+1,C+1+n3);
if (k<n3) for (rr int i=k+1;i<=n3;++i) heap2.Push(C[i]);
for (rr int i=1;i<=n1;++i) sum+=A[i];
for (rr int i=1;i<=n2;++i) sum+=B[i];
rr int j1=n1,j2=n2,len=m-n1-n2;
for (rr int i=0,t=min(n3,k);i<=t;++i,--len){
for (;j1>k-i;--j1,++len) heap2.Push(A[j1]),sum-=A[j1];
for (;j2>k-i;--j2,++len) heap2.Push(B[j2]),sum-=B[j2];
if (j1+i>=k&&j2+i>=k){
for (;len>0&&heap2.cnt;heap2.Pop(),--len)
sum+=heap2.heap[1],heap1.Push(heap2.heap[1]);
if (heap1.cnt&&heap2.cnt)
while (heap1.heap[1]>heap2.heap[1]){
rr int X=heap1.heap[1],Y=heap2.heap[1];
heap1.Pop(),heap2.Pop(),sum+=Y-X;
heap1.Push(Y),heap2.Push(X);
}
if (!len) ans=min(ans,sum);
}
sum+=C[i+1];
}
if (ans==1e18) printf("-1");
else printf("%lld",ans);
return 0;
}

#对顶堆#nssl 1477 赛的更多相关文章

  1. hdu3282 链表或者对顶堆

    维护序列的动态中位数 第一次用链表做题..感觉指针指来指去也挺麻烦的.. 本题链表解法就是用数组模拟出一个链表,然后离线输入所有数,排序,按照输入顺序在链表里删除元素,一次性删掉两个,然后中位数指针对 ...

  2. 【uoj#280】[UTR #2]题目难度提升 对顶堆+STL-set

    题目描述 给出 $n$ 个数 $a_1,a_2,...,a_n$ ,将其排为序列 $\{p_i\}$ ,满足 $\{前\ i\ 个数的中位数\}$ 单调不降.求字典序最大的 $\{p_i\}$ . 其 ...

  3. hdu4261 Estimation[暴力dp+对顶堆]

    https://vjudge.net/problem/HDU-4261 对于一个长2000的数列划分最多25个块,每块代价为块内每个数与块内中位数差的绝对值之和,求最小总代价. 套路化地,设$f[i] ...

  4. 【POJ 3784】 Running Median (对顶堆)

    Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...

  5. P1168 中位数(对顶堆)

    题意:维护一个序列,两种操作 1.插入一个数 2.输出中位数(若长度为偶数,输出中间两个较小的那个) 对顶堆 维护一个小根堆,一个大根堆,大根堆存1--mid,小根堆存mid+1---n 这样堆顶必有 ...

  6. poj3784 Running Median[对顶堆]

    由于我不会讲对顶堆,所以这里直接传上一个巨佬的学习笔记. 对顶堆其实还是很容易理解的,想这题的时候自己猜做法也能把没学过的对顶堆给想出来.后来了解,对顶堆主要还是动态的在线维护集合$K$大值.当然也可 ...

  7. 洛谷 - P1801 - 黑匣子 - 对顶堆

    这道题是提高+省选-的难度,做出来的话对数据结构题目的理解会增加很多. 可以使用一种叫做对顶堆的东西,对顶堆是在线维护第n小的logn的算法.大概的思路是,假如我们要找的是第n小,我们就维护一个大小为 ...

  8. bzoj 1112: [POI2008]砖块Klo【对顶堆】

    priority_queue实现的对顶堆,细节超级多WA了十几次--但是理论上是最简便的orz其实是我已经不会写平衡树了 枚举左端点,显然要把这一段的高度搞成(l,l+k-1)的高度中位数,所以需要一 ...

  9. 【Luogu P1168】【Luogu P1801&UVA 501】中位数&黑匣子(Black Box)——对顶堆相关

    Luogu P1168 Luogu P1801 UVA 501(洛谷Remote Judge) 前置知识:堆.优先队列STL的使用 对顶堆 是一种在线维护第\(k\)小的算法. 其实就是开两个堆,一个 ...

  10. luogu 3466 对顶堆

    显然答案是将一段区间全部转化成了其中位数这样的话,需要维护一个数据结构支持查询当前所有数中位数对顶堆 用两个堆将 < 中位数的数放入大根堆将 > 中位数的数放入小根堆这样就会存在删除操作 ...

随机推荐

  1. 【Android 逆向】【攻防世界】easyjni

    1. apk 安装到手机,提示需要输入flag 2. jadx打开apk public class MainActivity extends c { static { System.loadLibra ...

  2. 【Azure Function】Function本地调试时遇见跨域问题(blocked by CORS policy)

    问题描述 在本地调试Azure Function时,遇见了跨域问题: Access to XMLHttpRequest at 'http://localhost:7071/api/HttpTrigge ...

  3. 多线程系列(十一) -浅析并发读写锁StampedLock

    一.摘要 在上一篇文章中,我们讲到了使用ReadWriteLock可以解决多线程同时读,但只有一个线程能写的问题. 如果继续深入的分析ReadWriteLock,从锁的角度分析,会发现它有一个潜在的问 ...

  4. 为什么带NOLOCK的查询语句还会造成阻塞

    背景 客户反映HIS数据库在11点出现了长时间的阻塞,直到手动KILL掉阻塞的源头.请我们协助分析原因,最终定位到.NET程序中使用的SqlDataReader未正常关闭导致. 现象 登录SQL专家云 ...

  5. Inertial Explorer Xpress 学习笔记

    KEY WORDS: Coordinate Updates (CUPTs) Zero Velocity Updates (ZUPTs) Google Protocol Buffer (GPB)  是一 ...

  6. Android\C++\C#\Java

    关于:(38条消息) 千万不能错过的Android NDK下载安装及配置_石子君的博客-CSDN博客_android ndk下载 (38条消息) Android扩展知识 - so文件生成及其使用_L- ...

  7. C# DiagnosticSource and DiagnosticListener

    class Program { private static readonly DiagnosticSource testDiagnosticListener = new DiagnosticList ...

  8. 聊聊Web项目中的权限设计

    一般的Web项目中都少不了登录这个环节,登录之后就需要跳转到首页,并且根据 当前用户的信息,获取到对应的菜单信息,可以操作的方法信息等等.这个只是针对于 操作权限,至于数据权限处理起来会更加复杂一些. ...

  9. day04-3服务器推送新闻

    多用户即时通讯系统04 4.编码实现03 4.7功能实现-服务器推送消息功能实现 4.7.1思路分析 服务器推送新闻,本质其实就是群发消息 在服务器启动一个独立线程,专门负责推送新闻 该线程通过管理线 ...

  10. Ubuntu下安装Android Studio

    一.系统环境 二.安装源文件 Android Studio 4.2.2:android-studio-ide-202.7486908-linux.tar.gz Java SE Development ...