全部评论 2

  • 超时了,范围太小了

    2024-08-19 来自 江苏

    0
  • 同问

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    ll a[10000005];
    int main()
    {
        ll n;
        cin>>n;
        ll t;
        cin>>t;
        for(ll i = 1;i<=n;i++)
            cin>>a[i];
        while(t--){
            ll k;
            bool flag = false;
            cin>>k;
            ll l = 1;
            ll r = n;
            while(l<=r)
            {
                ll mid = (l+r)/2;
                if(a[mid]==k&&a[mid-1]==k||a[mid]>k)
                {
                    r=mid-1;
                }
                else if(a[mid]<k)
                {
                       l=mid+1;
                }
                else
                {
                    cout<<mid<<" ";
                    flag = true;
                    break;
                }
            }
            if(!flag)
            {
                cout<<-1<<" ";
            }
            flag=false;
        }
        return 0;
    }
    

    2024-08-03 来自 陕西

    0
    • +1

      #include<iostream>
      using namespace std;
      int main(){
          int a,b,arr[9999999],c;
          cin >> a >> b;
          for(int i = 0 ; i < a ; i++) cin >> arr[i];
          for(int i = 0 ; i < b ; i++){
              cin >> c;
              int temp = -1;
              for(int i = 0 ; i < a ; i++){
                  if(arr[i] == c){
                      temp = i+1;
                      break;
                  }
              }
              cout << temp << ' ';
          }
      }
      

      2024-08-11 来自 陕西

      0
首页