1. 程式人生 > >牛客網 牛客小白月賽8 F數列操作(vector用法技巧)

牛客網 牛客小白月賽8 F數列操作(vector用法技巧)

操作 const 預留空間 數列 iostream eof 需要 \n ==

題目鏈接: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 }

牛客網 牛客小白月賽8 F數列操作(vector用法技巧)