题解分享
题解分享简介
set的查询 - 题解
```
#include<bits/stdc++.h> // 包含标准库头文件
using namespace std; // 使用标准命名空间
int main(){
set<int>se;
int n,m,t;
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>t;
se.insert(t);
}
int c,num;
while(m--){
cin>>c>>num;
if(c==1){
auto j = se.find(num);
if(j!=se.end()) cout<<*j<<endl;
else cout<<"NO"<<endl;
}else if(c==2){
auto x = se.begin();
if(num<=*x) cout<<"NO"<<endl;
else{
auto j = se.lower_bound(num);
j--;
cout<<*j<<endl;
}
}else if(c==3){
auto x = se.end();
x--;
if(num>=*x) cout<<"NO"<<endl;
else{
auto j = se.upper_bound(num);
cout<<*j<<endl;
}
}
}
}
```
查看全文
0
0
1
2
set的查询 - 题解
```
#include<iostream>
#include<set>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
set<int> s;
int d;
for(int i=1;i<=n;i++)
{
cin>>d;
s.insert(d);
}
for(int i=1;i<=m;i++)
{
int c,x;
cin>>c>>x;
if(c==1)
{
auto it=s.find(x);
if(it!=s.end())
{
cout<<*it<<'\n'<<endl;
}
else
cout<<"NO"<<endl;
}
if(c==2)
{
auto it=s.lower_bound(x);
if(it!=s.begin()){
--it;
cout<<*it<<'\n'<<endl;
}
else
cout<<"NO"<<endl;
}
if(c==3)
{
auto it=s.upper_bound(x);
if(it!=s.end())
{
cout<<*it<<'\n'<<endl;
}
else
cout<<"NO"<<endl;
}
}
return 0;
}
```
查看全文
0
0
1
1
set的查询 - 题解
```
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
using ULL = unsigned long long;
const int N = 1e6+5;
int n, m;
set<int> s;
inline void solve() {
cin >> n >> m;
for (; n-- ;) {
int x; cin >> x;
s.insert(x);
}
for (; m-- ;) {
int a, b; cin >> a >> b;
if (a == 1) {
if (s.find(b) != s.end()) {
cout << b << endl;
}else {
cout << "NO" << endl;
}
}
if (a == 2) {
auto idx = s.lower_bound(b);
if (idx != s.begin()) {
--idx;
cout << *idx << endl;
}else
cout << "NO" << endl;
}
if (a == 3) {
auto id = s.upper_bound(b);
if (id != s.end()) {
cout << *id << endl;
}
else
cout << "NO" << endl;
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
//int _; cin >> _;
while (_--) solve();
return 0;
}
```
查看全文
0
0
0
3
set的查询 - 题解
```
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin >> n >> m;
set<int> num;
for ( int i = 0; i < n; i++ ) {
int temp;
cin >> temp;
num.insert(temp);
//cout << num[i]; 不能这样用
}
//cheek
//for ( int a : num )
// cout << a << endl;
for ( int i = 0; i < m; i++ ) {
int x,y;
cin >> x >> y;
if (x == 1) {
// 如果 y 存在则输出 y,否则输出 NO
if (num.find(y) != num.end()) {
cout << y << endl;
} else {
cout << "NO" << endl;
}
} else if (x == 2) {
// 输出小于 y 且最接近 y 的元素,如果不存在输出 NO
auto it = num.lower_bound(y);
if (it == num.begin()) {
cout << "NO" << endl; // 没有小于 y 的元素
} else {
--it; // 回退到小于 y 的最接近的元素
cout << *it << endl;
}
} else if (x == 3) {
// 输出大于 y 且最接近 y 的元素,如果不存在输出 NO
auto it = num.upper_bound(y);
if (it == num.end()) {
cout << "NO" << endl; // 没有大于 y 的元素
} else {
cout << *it << endl;
}
}
}
return 0;
}
```
查看全文
0
0
0
1
set的查询 - 题解
```
#include <iostream>
#include <set>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
set<int> s;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
s.insert(num);
}
for (int i = 0; i < m; i++) {
int c, x;
cin >> c >> x;
if (c == 1) {
auto it = s.find(x);
if (it != s.end()) {
cout << x << endl;
} else {
cout << "NO" << endl;
}
} else if (c == 2) {
auto it = s.lower_bound(x);
if (it != s.begin()) {
--it;
cout << *it << endl;
} else {
cout << "NO" << endl;
}
} else if (c == 3) {
auto it = s.upper_bound(x);
if (it != s.end()) {
cout << *it << endl;
} else {
cout << "NO" << endl;
}
}
}
return 0;
}
```
查看全文
0
0
0
1
set的查询 - 题解
include
using namespace std;
int main(){
int n,m,t,c,x;
set
s;
cin>>n>>m;
for(int i=1;i
>t;
s.insert(t);
}
for(int i=1;i
>c>>x;
if(c==1){
if(s.find(x)!=s.end())
cout
::iterator iter = s.begin();
if(x
::iterator it = s.lower_bound(x);
cout
::iterator iter = s.end();
if(x>=(--iter)){
cout
::iterator it = s.upper_bound(x);
cout<<it<<endl;
}
}
}
return 0;
}
查看全文
0
0
0
1
set的查询 - 题解
include
include
include
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
```
int n, m, t;
cin >> n >> m;
set<int> s;
for (int i = 0; i < n; i++)
{
cin >> t;
s.insert(t);
}
int a, b;
while (m--)
{
cin >> a >> b;
if (a == 1)
{
if (s.find(b) == s.end())
{
cout << "NO" << endl;
}
else
{
cout << b <<endl;
}
}
if (a == 2)
{
set<int> ::iterator it;
it = s.lower_bound(b);
if (it != s.begin())
{
cout << *(--it) << endl;
}
else
{
cout << "NO\n";
}
}
if (a == 3)
{
set<int> ::iterator it;
it = s.upper_bound(b);
if (it != s.end())
{
cout << *it << endl;
}
else
{
cout << "NO\n";
}
}
}
return 0;
```
}
查看全文
0
0
0
2
set的查询 - 题解
```
#include <iostream>
#include<set>
using namespace std;
int main()
{
int n,m,tmp;
int opt,x;
set<int>num;
cin>>n>>m;
for(int i=0; i<n; i++)
{
cin>>tmp;
num.insert(tmp);
}
for(int i=0; i<m; i++)
{
cin>>opt>>x;
set<int>::iterator iter;
if(opt==1)
{
iter=num.find(x);
if(iter!=num.end())
{
cout<<*iter<<endl;
}
else
cout<<"NO"<<endl;
}
else if(opt==2)
{
iter=num.lower_bound(x);
iter--;
if(iter!=num.end())
{
cout<<*iter<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
else
{
iter=num.upper_bound(x);
if(iter!=num.end())
{
cout<<*iter<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
}
return 0;
}
```
查看全文
0
0
0
1
set的查询 - 题解
```
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
set<int>set;
int n,m,t;cin>>n>>m;
for(int i=0;i<n;++i)
{
cin>>t;
set.insert(t);
}
for(int i=0;i<m;++i)
{
int c,num;
cin>>c>>num;
auto it = set.find(num);
if(c==1)
{
if(it!=set.end())
{
cout<<num;
}
else
{
cout<<"NO";
}
}
else if(c==2)
{
it = set.lower_bound(num);
if((--it)!=set.end())
{
cout<<*it<<endl;
}
else cout<<"NO";
}
else if(c==3)
{
it = set.upper_bound(num);
if(it!=set.end())
{
cout<<*it<<endl;
}
else
{
cout<<"NO";
}
}
cout<<endl;
}
return 0;
}
```
查看全文
0
0
0
1



