4 条题解

  • 1
    @ 2025-1-5 14:20:59
    #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
      @ 2024-4-19 22:32:29
      #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
        @ 2024-4-10 17:58:32

        #include <iostream> #include <map> #include <set> 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
          @ 2024-4-7 22:46:14
          #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;
          }
          
          • 1

          信息

          ID
          70
          时间
          2000ms
          内存
          256MiB
          难度
          6
          标签
          递交数
          272
          已通过
          84
          上传者