牛客小白月赛8 vector的使用

链接:https://www.nowcoder.com/acm/contest/214/F
来源:牛客网
 

clccle是个蒟蒻,她经常会在学校机房里刷题,也会被同校的dalao们虐,有一次,她想出了一个毒瘤数据结构,便兴冲冲的把题面打了出来,她觉得自己能5s内切掉就很棒了,结果evildoer过来一看,说:"这思博题不是1s就能切掉嘛",clccle觉得自己的信心得到了打击,你能帮她在1s中切掉这道水题嘛?

你需要写一个毒瘤(划掉)简单的数据结构,满足以下操作
1.插入一个数x(insert)
2.删除一个数x(delete)(如果有多个相同的数,则只删除一个)
3.查询一个数x的排名(若有多个相同的数,就输出最小的排名)
4.查询排名为x的数
5.求一个数x的前驱
6.求一个数x的后继

输入描述:

第一行,输入一个整数n,表示接下来需要输入n行

接下来n行,输入 一个整数num和一个整数x

输出描述:

当num为3,4,5,6时,输出对应的答案

示例1

输入

复制

8
1 10
1 20
1 30
3 20
4 2
2 10
5 25
6 -1

输出

复制

2
20
20
20

这道题就是充分使用lower_bound()函数和upper_bound()函数。迭代器的使用也很重要。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pi acos(-1)
#define For(i, a, b) for(int (i) = (a); (i) <= (b); (i) ++)
#define Bor(i, a, b) for(int (i) = (b); (i) >= (a); (i) --)
#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a)<(b))?(a):(b))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define eps 1e-7
using namespace std;
typedef long long ll;
const int maxn = 100 + 10;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
const ll mod = 1e9 + 7;
inline int read(){
    int ret=0,f=0;char ch=getchar();
    while(ch>'9'||ch<'0') f^=ch=='-',ch=getchar();
    while(ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
    return f?-ret:ret;
}
int n;
int main(){
	scanf("%d",&n);
	vector <int> q;
	vector <int> :: iterator it;
	while(n--){
		int num, x;
		scanf("%d%d",&num,&x);
		if(num == 1)
			q.insert(lower_bound(q.begin(), q.end(), x), x);
		else if(num == 2)
			q.erase(lower_bound(q.begin(), q.end(),x));
		else if(num == 3)
			printf("%d\n",lower_bound(q.begin(),q.end(),x)-q.begin() +1);
		else if(num == 4)
			printf("%d\n",q[x-1]);
		else if(num == 5){
			it = lower_bound(q.begin(),q.end(),x);
			printf("%d\n",*(--it));
		}
		else if(num == 6){
			it = upper_bound(q.begin(), q.end(),x);
			printf("%d\n",*it);
		}
						
	}
	return 0;
}

继续加油。

猜你喜欢

转载自blog.csdn.net/ab1605014317/article/details/83515157