7-4 快速排序 (20 分)
注:由于是用map计数暴力实现的,所以当数据量大的时候就会WA
众所周知,Keven是一个ACMer,他今天刚刚学会了快速排序,他非常开心,因为他可以快速的找到区间第K小的数字了。但是同为ACMer的JOJO看不下去了,他觉得快速排序应该是众所周知的,于是JOJO向Keven提问,问题内容如下:
你需要完成N次操作,其中所有操作都属于下面三种
1、add X,其中add是一个字符串,X是一个数字,表示将X加入到序列中。 1<= X <=200000
2、delete X,其中delete是一个字符串,X是一个数字,表示将X从序列中删除。保证数字 X 一定在原序列中
3、query X,其中query是一个字符串,X是一个数字,表示求当前序列中第K小的数字。1<= X <=当前序列长度
输入格式:
第一行给出一个数字N,表示总操作次数。1<=N<=200000
接下来N行,每行给出一个字符串和一个数字,表示上面的3种操作之一。
输出格式:
每个query命令,在一行种输出序列第K小的数字。
输入样例:
10
add 5
query 1
add 3
query 1
query 2
add 5
query 2
query 3
detele 3
query 1
结尾无空行
输出样例:
5
3
5
5
5
5
结尾无空行
提示:
第一次询问时,序列为5,查询第1小值,输出5 第二次询问时,序列为3 5,查询第1小值,输出3 第三次询问时,序列为3 5,查询第2小值,输出5 第四次询问时,序列为3 5 5,查询第2小值,输出5 第五次询问时,序列为3 5 5,查询第3小值,输出5 第六次询问时,序列为5 5,查询第1小值,输出5
C++(g++):
using namespace std;
#include <algorithm>
#include<cstring>
#include <iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<iomanip>
#include<sstream>
#include<vector>
#include <map>
int find(const map<int,int>&m,const int&a)//查找第a小的数字
{
int c = 0;
for (auto it = m.begin(); it != m.end(); it++)
{
if ((*it).second + c < a)c+=(*it).second;
else return (*it).first;
}
}
int main()
{
int n; cin >> n;
map<int, int>m;
while (n--)
{
string s; int t;
cin >> s; cin >> t;
if (s.front() == 'a')m[t]++;
else if (s.front() == 'q')cout << find(m, t) << endl;
else m[t]--;
}
}