洛谷 [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 ...
随机推荐
- 507. Perfect Number 因数求和
[抄题]: We define the Perfect Number is a positive integer that is equal to the sum of all its positiv ...
- 使用mail架包发送邮件javax.mail.AuthenticationFailedException: failed to connect at javax.mail.Service.connec
这个错误是因为连接不上邮箱服务器导致的,可能有以下几个原因(以网易邮箱为例) 1.当使用第三方登录邮箱时需要有邮箱的授权码,且要开启POP3/SMTP/IMAP:服务 2.在代码中要调用网易邮箱的密码 ...
- 2.sql分类
SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法.但是 SQL 语言也包含用于更新.插 ...
- [存储过程]中的事务(rollback)回滚
在编写SQL Server 事务相关的存储过程代码时,经常看到下面这样的写法: begin tran update statement 1 ... update statement 2 ... del ...
- Python基础入门-函数实战登录功能
''' 函数实战: .加法计算器 .过滤器 .登录功能实战 ''' def add(a,b): return a+b def login_order(): return 'asdfasdfdasfad ...
- Detailed ASP.NET MVC Pipeline
Posted By : Shailendra Chauhan, 27 Jan 2014 P.NET MVC is an open source framework built on the top o ...
- Training Very Deep Networks
Rupesh Kumar SrivastavaKlaus Greff ̈J urgenSchmidhuberThe Swiss AI Lab IDSIA / USI / SUPSI{rupesh, k ...
- POJ - 3984 迷宫问题 BFS求具体路径坐标
迷宫问题 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...
- android IntentService和ResultReceiver的异步处理
IntentService和ResultReceiver的异步处理 1.在下载手机上从网络下载东西的时候会用到AsyncTask来方便处理,这里可以在用IntentService和ResultRece ...
- Atcoder Beginner Contest 121D(异或公式)
#include<bits/stdc++.h>using namespace std;int main(){ long long a,b; cin>>a>&g ...