LG1801 【黑匣子_NOI导刊2010提高(06)】
看到各路dalao用平衡树的做法,表示本人不才,并不会。
然而我会优先队列_huaji_,并且发现用堆解题的dalao们并没有基于在线的做法
于是我的showtime到了
评测结果:https://www.luogu.org/record/show?rid=4645103
先讲一下思路:
题干所描述的** i **的实际值并不重要,他只是作为堆的一个位置
当然如果你暴力模拟的话肯定会超时
为此我们考虑维护两个堆,一个是大根堆,一个是小根堆,且满足小根堆中的数比大根堆中的数大
用bool变量flag来表示当前所操作的是大根堆还是小根堆
首先是ADD(x),按flag和x分为四种情况
然后是GET,按flag分为两种情况
具体操作见以下代码:
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#pragma GCC optimize(3)//STL必带
using namespace std;
#define ll long long
const int maxn=2e5+10;
const int INF=0x7fffffff;
inline void read(int&x){//卡常的输入
int data=0,w=1;
char ch=getchar();
while(ch!='-'&&!isdigit(ch))
ch=getchar();
if(ch=='-')
w=-1,ch=getchar();
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
x=data*w;
}
void write(int x){//卡常的输出
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar('0'+x%10);
}
int a[maxn],u[maxn]={0};
priority_queue <int,vector<int>,less<int> > lrh;//大根堆,large root heap
priority_queue <int,vector<int>,greater<int> > srh;//小根堆,small root heap
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int m,n;
read(m);read(n);
for(int i=1;i<=m;++i)
read(a[i]);
int t;
for(int i=1;i<=n;++i){
read(t);
++u[t];
}
/*clog<<"u:"<<endl;
for(int i=1;i<=m;++i)
clog<<i<<" "<<u[i]<<endl;
clog<<"uend"<<endl;*/
bool flag=1;
srh.push(INF); //这个细节很重要,INF的值对此题没有影响,但是如果不加对第一个数的操作会RE的
#define x a[i]
#define y u[i]
for(int i=1;i<=m;++i){
if(flag)//ADD(x)
if(x<srh.top()){
lrh.push(x);
flag=0;
}
else
srh.push(x);
else
if(x<lrh.top()){//这一步是ADD的精华所在,对两个对做了等价变换
srh.push(lrh.top());//i值前移,因而大根堆要把最大数给小根堆
lrh.pop();
lrh.push(x);
}
else
srh.push(x);
while(y--){//GET
// clog<<"once in "<<i<<endl;
if(flag){//这一步是GET的精华所在,对两个对做了等价变换
write(srh.top());putchar('\n');
lrh.push(srh.top());//i值后移,因而小根堆要把最小数给大根堆
srh.pop();
}
else{
write(lrh.top());putchar('\n');
flag=1;
}
}
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
LG1801 【黑匣子_NOI导刊2010提高(06)】的更多相关文章
- P1801 黑匣子_NOI导刊2010提高(06)
P1801 黑匣子_NOI导刊2010提高(06) 题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个 ...
- Luogu P1801 黑匣子_NOI导刊2010提高(06)
P1801 黑匣子_NOI导刊2010提高(06) 题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个 ...
- 洛谷 P1801 黑匣子_NOI导刊2010提高(06)(未完)
P1801 黑匣子_NOI导刊2010提高(06) 题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个 ...
- 【洛谷】【堆】P1801 黑匣子_NOI导刊2010提高(06)
[题目描述:] Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两 ...
- 题解 P1801 【黑匣子_NOI导刊2010提高(06)】
蒟蒻来发题解了.我仔细看了一下其他题解,各位巨佬用了堆,红黑树,splay,treap之类的强大算法,表示蒟蒻的我只会口胡这些算法,所以我决定用一种极其易理解的算法————fhq treap,作为tr ...
- 【luogu P1801 黑匣子_NOI导刊2010提高(06)】 题解
题目链接:https://www.luogu.org/problemnew/show/P1801 替罪羊树吼啊! #include <cstdio> #include <cstrin ...
- [洛谷P1801]黑匣子_NOI导刊2010提高(06)
题目大意:两个操作:向一个可重集中加入一个元素:询问第$k$大的数($k$为之前询问的个数加一) 题解:离散化,权值线段树直接查询 卡点:无 C++ Code: #include <cstdio ...
- 洛谷 P1801 黑匣子_NOI导刊2010提高(06)
题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两种: ...
- 黑匣子_NOI导刊2010提高(06) Splay Tree
题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两种: ...
随机推荐
- spring boot 启动报错(spring-boot-devtools热部署后):The elements [spring.resources.cache-period] were left unbound. Update your application's configuration
详细错误代码: *************************** APPLICATION FAILED TO START *************************** Descript ...
- 第五章 [BX]和loop指令
5.1 [bx] [bx]是什么 和 [0] 有些类似,[0] 表示内存单元,它的偏移地址是 0. 例如: mov ax, [0] 内存以字节为单位:ax以字(16bit = 2Byte)为单位:al ...
- JS-构造函数2
一.如何创建对象 1.对象字面量 var obj1={ name:"吻别", singer:"张学友", type:"流行" } 2.构造函 ...
- hdu-2227-dp+bit
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- hdu-2509-反nim博弈
Be the Winner Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- Razor视图引擎 语法学习
下面就和大家分享下我在asp.net官网看到的资料,学习到的点语法.1.通过使用@符号,可以直接在html页面中写C#或者VB代码:运行后: 2.页面中的C#或者VB代码都放在大括号中.运行后: 3. ...
- transition多个属性同时渐变(left/top)
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- spring cloud 学习(一)初学SpringCloud
初学SpringCloud 前言 在SpringBoot的坑还没填完的情况下,我又迫不及待地开新坑了.主要是寒假即将结束了,到时又得忙于各种各样的事情……留个坑给自己应该就会惦记着它,再慢慢地补上…… ...
- apache-service的使用
apache service目录设置 设置Apache HTTP Server的文件根目录(DocumentRoot) 安装Apache 时,系统会给定一个缺省的文件根目录 如果你觉得这个网页存在这个 ...