站外题求助
  • 板块灌水区
  • 楼主fish_gugu
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/24 21:40
  • 上次更新2024/3/25 11:44:06
查看原帖
站外题求助
759436
fish_gugu楼主2024/3/24 21:40

给定一个初始为空的可重复集合。接下来执行若干次询问,询问有六种类型:

  • op=1op = 1,表示向集合中添加数值 xx

  • op=2op = 2,表示删除集合中数值为 xx 的一个元素,若不存在则忽略该操作

  • op=3op = 3,表示询问集合中,最小的值是多少

  • op=4op = 4,表示询问集合中,最大的值是多少

  • op=5op = 5,表示询问集合中,xx 的前驱,即最大的 <x< x 的数

  • op=6op = 6,表示询问集合中,xx 的后继,即最小的 >x> x 的数

代码

#include <bits/stdc++.h> 
using namespace std; 
    
multiset<int> tree;  
    
void add(int x) 
{  
    tree.insert(x);  
}  
   
void remove(int x) 
{  
    tree.erase(x);  
}  
   
int getBig()
{  
    return *tree.begin();  
}  
    
int getSmall() 
{  
     return *prev(tree.end());
}  
  
int getPredecessor(int x) 
{  
    multiset<int>::iterator it = tree.lower_bound(x);  
    if (it == tree.begin()) return -1;  
    --it;  
    return *it;  
}  
  
int getSuccessor(int x)
{  
    multiset<int>::iterator it = tree.upper_bound(x);  
    if (it == tree.end()) return -1;  
    return *it;  
}  

  
int main() 
{  
    int q,op,x;
    cin >> q;
    for(int i = 1;i <= q;i++)
    {
    	cin >> op;
    	if(op == 1)
    	{
    		cin >> x;
    		add(x);
		}
		if(op == 2)
		{
			cin >> x;
			remove(x);
		}
		if(op == 3) cout << getBig() << endl;
		if(op == 4) cout << getSmall() << endl;
		if(op == 5)
		{
			cin >> x;
			cout << getPredecessor(x) << endl;
		} 
		if(op == 6)
		{
			cin >> x;
			cout << getSuccessor(x) << endl;
		}
	}
    return 0;  
}

本地样例通过,然而却WA了,能解释一下吗

2024/3/24 21:40
加载中...