46 lines
1.4 KiB
C++
46 lines
1.4 KiB
C++
|
|
#include <bits/stdc++.h>
|
|||
|
|
|
|||
|
|
using namespace std;
|
|||
|
|
const int N = 10010;
|
|||
|
|
int a[N];
|
|||
|
|
int q, cmd, number, cnt;
|
|||
|
|
long long p;
|
|||
|
|
|
|||
|
|
//解法1:暴力(使用二分优化查询)
|
|||
|
|
//lower_bound(begin,end,number)函数是求begin-end中(排好序)第一个大于等于 number的数
|
|||
|
|
//upper_bound(begin,end,number)函数是求begin-end中(排好序)第一个大于 number的数
|
|||
|
|
int main() {
|
|||
|
|
//q次询问
|
|||
|
|
cin >> q;
|
|||
|
|
while (q--) {
|
|||
|
|
cin >> cmd; //1-5共5个命令
|
|||
|
|
switch (cmd) {
|
|||
|
|
case 1:
|
|||
|
|
cin >> number;
|
|||
|
|
cout << lower_bound(a + 1, a + cnt + 1, number) - a << endl;
|
|||
|
|
break;
|
|||
|
|
case 2:
|
|||
|
|
cin >> number;
|
|||
|
|
cout << a[number] << endl;
|
|||
|
|
break;
|
|||
|
|
case 3:
|
|||
|
|
cin >> number;
|
|||
|
|
p = lower_bound(a + 1, a + cnt + 1, number) - a;
|
|||
|
|
if (p == 1) cout << -2147483647 << endl;
|
|||
|
|
else cout << a[p - 1] << endl;
|
|||
|
|
break;
|
|||
|
|
case 4:
|
|||
|
|
cin >> number;
|
|||
|
|
p = upper_bound(a + 1, a + cnt + 1, number) - a;
|
|||
|
|
if (p == cnt + 1) cout << 2147483647 << endl;
|
|||
|
|
else cout << a[p] << endl;
|
|||
|
|
break;
|
|||
|
|
case 5:
|
|||
|
|
cin >> number;
|
|||
|
|
a[++cnt] = number;
|
|||
|
|
sort(a + 1, a + cnt + 1);
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return 0;
|
|||
|
|
}
|