1400: 神奇的操作 [二分、vector]
時間限制: 1 Sec 內存限制: 128 MB提交: 39 解決: 9 統計
題目描述
有Q次操作,分兩類:
1 x —— 向集合(可能會有相同元素)裏面插入元素x。
2 y —— 查詢集合裏面第y大的元素。
輸入
第一行輸入一個整數t,代表有t組測試數據(t <= 10)
每組數據第一行輸出一個整數Q,代表操作次數。
保證:1 <= Q <= 100000,且所有元素均在int範圍內。
輸出
對出現的第二個操作輸出一個整數,代表結果,如果集合裏面元素個數小於y,輸出-1。
樣例輸入
2
2
1 2
2 2
3
1 1
1 3
2 2
樣例輸出
-1
1
來源
題解:之前一直沒看懂題目,‘1’代表第一種操作,即插入;‘2’代表查詢。。。。用vector很容易解決。。。
AC代碼:
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
vector<int>V;
vector<int>::iterator it;
int main()
{
int t;
cin>>t;
while(t--)
{
V.clear();
int q;
cin>>q;
while(q--)
{
int a,b;
cin>>a>>b;
if(a==1)
{
if(V.empty())
V.push_back(b);
else
{
it=lower_bound(V.begin(),V.end(),b);
if((it-V.begin())==V.size())
V.push_back(b);
else
V.insert(it,b);
}
}
else
{
if(V.size()<b) puts("-1");
else printf("%d\n",V[V.size()-b]);
}
}
}
return 0;
}