洛谷 [TJOI2010]中位数
题目链接
题解
比较水。。
常见套路,维护两个堆
Code
#include<bits/stdc++.h>
#define LL long long
#define RG register
using namespace std;
inline int gi() {
int f = 1, s = 0;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') s = s*10+c-'0', c = getchar();
return f == 1 ? s : -s;
}
priority_queue<int> p;
priority_queue<int, vector<int>, greater<int> > q;
const int N = 100010;
int a[N];
char s[10];
int main() {
int n = gi();
for (int i = 1; i <= n; i++) a[i] = gi();
sort(a+1, a+1+n);
int t = gi(), s1 = (n+1)/2, s2 = n-s1;
for (int i = 1; i <= s1; i++)
p.push(a[i]);
for (int i = s1+1; i <= n; i++)
q.push(a[i]);
while (t--) {
cin >> s;
if (s[0] == 'a') {
q.push(gi());
s2++;
if (q.top() < p.top()) {
int tmp = q.top(); q.pop();
q.push(p.top()); p.pop();
p.push(tmp);
}
if (s1 < s2) {
s2--;
s1++;
p.push(q.top());
q.pop();
}
}
else printf("%d\n", p.top());
}
return 0;
}
洛谷 [TJOI2010]中位数的更多相关文章
- 洛谷P1168 中位数
题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数.[color=red]即[/color] ...
- [洛谷P1168]中位数(Splay)/(主席树)
Description 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], -, A[2k - 1]的中位数.即前1,3,5,--个数的 ...
- 洛谷 P1168 中位数
题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[3], …, A[2k - 1]的中位数.[color=red]即[/color] ...
- 洛谷——P1168 中位数
P1168 中位数 题目描述 给出一个长度为NN的非负整数序列$A_i$,对于所有1 ≤ k ≤ (N + 1),输出$A_1, A_3, …, A_{2k - 1}A1,A3,…,A2k−1 ...
- 洛谷—— P1168 中位数
https://www.luogu.org/problem/show?pid=1168 题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], ...
- 洛谷P1168 中位数——set/线段树
先上一波链接 https://www.luogu.com.cn/problem/P1168 这道题我们有两种写法 第一种呢是线段树,我们首先需要将原本的数据离散化,线段树维护的信息就是区间内有多少个数 ...
- 洛谷 P1168 中位数(优先队列)
题目链接 https://www.luogu.org/problemnew/show/P1168 解题思路 这个题就是求中位数,但是暴力会tle,所以我们用一种O(nlogn)的算法来实现. 这里用到 ...
- 洛谷P1168中位数
传送门啦 基本思想就是二分寻找答案,然后用树状数组去维护有几个比这个二分出来的值大,然后就没有了: 数据要离散,这个好像用map也可以,但是不会: 那怎么离散呢? 我们先把a数组读入并复制给s数组,然 ...
- 洛谷 P3871 中位数
->题目链接 题解: 暴力 经鉴定,此题数据水到没朋友. #include<algorithm> #include<iostream> #include<cstdi ...
随机推荐
- EF添加和修改
(1)//添加操作 public bool addDate() { try { //声明上下文 a_context = new AEntities(); //声明数据模型实体 //执行代码时候会先验证 ...
- Python2.7的安装、python3的安装
>登录python官网下载python2.7的相关版本 python官网链接 >根据平台选择相应的版本 >下载完毕后点击安装即可 >配置环境变量 >安装成功 2 pyth ...
- C++获取时间并命名为文件名
#include <time.h> char pStrPath1[20];time_t currTime;struct tm *mt;Mat saveImg1; currTime = ti ...
- c语言实践 判断一个数是不是素数
int main() { int input = 0; scanf_s("%d",&input); if (input < 2) { printf("wro ...
- laravel中的attach and detach toggle method
创建模型 post and user 以及 users , posts ,user_post(favorities)测试数据 在此可以看上一篇中的数据,本次测试数据利用的上一篇的数据.detach ...
- Part10-C语言环境初始化-栈初始化lesson1
1.概念解析 ARM系统使用的是满栈! ARM采用降栈!!! 栈帧 每一个进程会有一个栈,该进程中的每一个函数会分割栈的一部分,那么每一个函数使用的那部分栈就叫做栈帧.那么所有栈帧组成了整个栈. 子函 ...
- HTTP文件上传插件开发文档-ASP
版权所有 2009-2016 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/http-u ...
- Java之集合框架vector类设计原理
- Gabor filter与Gabor transform
https://en.wikipedia.org/wiki/G%C3%A1bor Gabor filter:a linear filter used in image processing一种线性滤波 ...
- python 日志模块工具类
#!/usr/bin/env python # -*- coding: utf-8 -*- import logging # logName 日志中的某个格式化的字段名,logFile生成的日志文件名 ...