7-4 快速排序

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]--;
	}
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微__凉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值