牛客网 牛客小白月赛8 F数列操作(vector用法技巧)

题目链接:https://www.nowcoder.com/acm/contest/214/F

题目:

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

题解:学到一套vector的新用法。

 1 #include <set>
 2 #include <map>
 3 #include <queue>
 4 #include <deque>
 5 #include <stack>
 6 #include <cmath>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <string>
10 #include <cstring>
11 #include <fstream>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 #define eps 1e-8
17 #define pb push_back
18 #define PI acos(-1.0)
19 #define INF 0x3f3f3f3f
20 #define clr(a,b) memset(a,b,sizeof(a)
21 #define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
22 
23 const int N=1e5+10;
24 typedef long long ll;
25 typedef unsigned long long ull;
26 vector <int> v;
27 
28 int main(){
29     int n,op,x;
30     scanf("%d",&n);
31     //vector预留空间 数组初始化
32     v.reserve(N);
33     for(int i=1;i<=n;i++){
34         scanf("%d%d",&op,&x);
35         if(op==1){
36             v.insert(lower_bound(v.begin(),v.end(),x),x);
37         }
38         else if(op==2){
39             v.erase(lower_bound(v.begin(),v.end(),x));
40         }
41         else if(op==3){
42             printf("%d\n",lower_bound(v.begin(),v.end(),x)-v.begin()+1);
43         }
44         else if(op==4){
45             printf("%d\n",v[x-1]);
46         }
47         else if(op==5){
48             printf("%d\n",v[lower_bound(v.begin(),v.end(),x)-v.begin()-1]);
49         }
50         else if(op==6){
51             printf("%d\n",v[lower_bound(v.begin(),v.end(),x+1)-v.begin()]);
52         }
53     }
54     return 0;
55 }

猜你喜欢

转载自www.cnblogs.com/ehanla/p/9824414.html
今日推荐