神奇的操作 [vector、二分]

来源:互联网 发布:网络黄金egd还有希望吗 编辑:程序博客网 时间:2024/06/07 12:02

Description

有Q次操作,分两类:

1 x —— 向集合(可能会有相同元素)里面插入元素x。

2 y —— 查询集合里面第y大的元素。

Input

第一行输入一个整数t,代表有t组测试数据(t <= 10)

每组数据第一行输出一个整数Q,代表操作次数。

保证:1 <= Q <= 100000,且所有元素均在int范围内。

Output

对出现的第二个操作输出一个整数,代表结果,如果集合里面元素个数小于y,输出-1

Sample Input

2
2
1 2
2 2
3
1 1
1 3
2 2

Sample Output

-1
1

Hint

题意

题解:

第k大 第一个最大

AC代码

#include <cstdio>#include <iostream>#include <queue>#include <map>#include <vector>#include <cmath>#include <set>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int N = 5e4+5;const double eps = 1e-4;vector<int > a;set<int > s;int vis[N];int main(){    int t;    scanf("%d",&t);    while (t--){        a.clear();        int q;        scanf("%d",&q);        int x,y;        for (int i = 1; i <= q; ++i){            scanf("%d%d",&x,&y);            if (x==1){                    if (a.empty()) a.push_back(y);                    else {                        if (y>=a[a.size()-1]) a.push_back(y);                        else{                            int ans = lower_bound(a.begin(),a.end(),y)-a.begin();                            a.insert(a.begin()+ans,y);                        }                    }            }            else {                if (a.size()<y) printf("-1\n");                else {                printf("%d\n",a[a.size()-y]);                }            }        }    }    return 0;}
原创粉丝点击