5 solutions

  • 1
    @ 2026-5-6 18:38:41
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    const int MAXN = 1e9;
    int n, m;
    vector <int> num;
    
    int b_s(vector<int>& tmp, int tgt){//二分 
    	int l = 0;
    	int r = tmp.size() - 1;
    	
    	int ans = -2;
    	
    	while(l <= r){
    		int mid = l + ((r - l) >> 1);
    		if(tmp[mid] == tgt) {
    			ans = mid;
    			r = mid - 1;
    		} else if (tmp[mid] < tgt) {
    			l = mid + 1;	
    		}else {
    			r = mid - 1;
    		}
    	}
    	return ans;
    }
    
    int main() {
    	ios::sync_with_stdio(false);
    	cin.tie(0),cout.tie(0);//提升读入速度 
    	
    	cin >> n >> m;
    	for(int i = 0;i < n;i++) {
    		int x;
    		cin >> x;
    		num.push_back(x);
    	}
    	
    	while(m--){
    		int op;
    		cin >> op;
    		int ans = b_s(num, op);
    		cout << ans + 1 << ' ';//下标加1,如果找不到输出-2+1就是-1(不要学我qqq) 
    	}
    	return 0;
    }
    

    这题不是用来讲倍增的吗

    • 0
      @ 2026-5-6 18:52:21
      #include<bits/stdc++.h>
      using namespace std;
      const int N = 1e6+10;
      int n,m,a[N];
      int main(){
      	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
      	cin >> n >> m;
      	for(int i = 1; i <= n; i++){
      		cin >> a[i];
      	}
      	while(m--){
      		int x,p=0,st=1;cin>>x;
      		while(1){
      			if(p+st<=n&&a[st-p]<x)p+=st,st<<=1;
      			else st>>=1;
      			if(!st){
      				if(a[p+1]==x)cout<<p+1<<' ';
      				else cout<<-1<<' '; 
      				break;
      			}
      		}
      		
      	}
      	return 0; 
      }
      

      代码有错,改一个字符。

      • 0
        @ 2026-5-6 18:37:12
        #include<bits/stdc++.h>
        using namespace std;
        long long n,m,a[1000010];
        int main(){
          cin>>n>>m;
          for(int i=1;i<=n;i++){
            cin>>a[i];
        	}
        	while(m){
        		long long q;
        		cin>>q;
        		long long ans=lower_bound(a+1,a+1+n,q)-a;
        		if(a[ans]==q)cout<<ans<<" ";
        		else cout<<-1<<" ";
        		m--;
        	}
        	return 0;
        }
        
        
        • -2
          @ 2026-5-6 18:33:18

          #include<bits/stdc++.h> using namespace std; long long n,m,a[1000010]; int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ cin>>a[i]; } while(m){ long long q; cin>>q; long long ans=lower_bound(a+1,a+1+n,q)-a; if(a[ans]==q)cout<<ans<<" "; else cout<<-1<<" "; m--; } return 0; }**

          • -2
            @ 2026-5-6 18:32:53

            #include<bits/stdc++.h> using namespace std; long long n,m,a[1000010]; int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ cin>>a[i]; } while(m){ long long q; cin>>q; long long ans=lower_bound(a+1,a+1+n,q)-a; if(a[ans]==q)cout<<ans<<" "; else cout<<-1<<" "; m--; } return 0; }

            • 1

            Information

            ID
            4733
            Time
            1000ms
            Memory
            125MiB
            Difficulty
            2
            Tags
            # Submissions
            48
            Accepted
            11
            Uploaded By