2 条题解

  • 1
    @ 2025-12-29 17:26:16
    #include<bits/stdc++.h>
    using namespace std;
    int a[10000010],n;
    int f(int x){
        int u=lower_bound(a+1,a+1+n,x)-a;;
        if(a[u]!=x||(a[1]!=0&&x==0))
            return -1;
        return u;
    }
    int main(){
        int m;
        ios::sync_with_stdio(false);
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        cin>>m;
        for(int i=1;i<=m;i++){
            int t;
            cin>>t;
            cout<<f(t)<<" ";
        }
        return 0;
    }
    
    • -1
      @ 2025-12-16 20:44:32

      #include #include #include using namespace std;

      int main() { int n; cin >> n;

      vector<int> a(n);
      for (int i = 0; i < n; i++) {
          cin >> a[i];
      }
      
      int m;
      cin >> m;
      
      for (int i = 0; i < m; i++) {
          int x;
          cin >> x;
          
          // 使用 lower_bound 找到第一个 >= x 的位置
          auto it = lower_bound(a.begin(), a.end(), x);
          
          // 检查是否找到且值等于 x
          if (it != a.end() && *it == x) {
              // 输出位置(从1开始编号)
              cout << (it - a.begin() + 1) << " ";
          } else {
              cout << "-1 ";
          }
      }
      
      cout << endl;
      
      return 0;
      

      }

      • 1

      信息

      ID
      14541
      时间
      1000ms
      内存
      256MiB
      难度
      10
      标签
      递交数
      4
      已通过
      3
      上传者