3 solutions

  • 0
    @ 2022-4-5 11:36:49
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N = 100010;
    int a[N];
    int b[N];
    int SR(int x,int l,int r) {
        if(l >= r)
            return -1;
        while(l < r) {
            int mid = l + r + 1  >> 1;
            if(a[mid] <= x) {
                l = mid;
            }
            else r = mid - 1;
        }
        if(a[r]==x)return r;
        return -1;
    }
    int main () {
        int n ;
        cin >> n;
        for (int i = 1; i <= n; i ++) {
            cin >> a[i];
        }
        int m;
        cin >> m;
        for (int i = 1; i <= m ; i ++) {
            cin >> b[i];
        }
        for(int i = 1; i <= m; i ++) {
            cout << SR(b[i],0,n) <<' ';
        }
        return 0;
    }
    
    • 0
      @ 2022-2-20 23:31:56

      废话少说看代码

      #include<bits/stdc++.h>
      
      using namespace std;
      const int N = 1e5 + 5;
      int num1[N];
      
      int main(void)
      {
      	int n, m;
      	cin >> n;
      	for (int i = 1; i <= n; i++) cin >> num1[i];
      	cin >> m;
      	for (int i = 0; i < m; i++)
      	{
      		int t,l;
      		cin >> t;
      		
      		l = upper_bound(num1 + 1, num1 + 1 + n, t) - num1-1;
      		if (num1[l] != t) cout << -1 << " ";
      		else cout << l << " ";
      	}
      
      
      	return 0;
      }
      
      • 0
        @ 2022-2-13 20:28:46
        #include<bits/stdc++.h>
        using namespace std;
        const int N=1e6+5;
        int f[N];
        int n,l,r,m,g;
        int find(int k){
        	l=1,r=n;
        	while(l<=r){
        		int mid=(l+r)/2;
        		if(f[mid]<=k){
        			l=mid+1;
        		}
        		else{
        			r=mid-1;
        		}
        	}
        	if(f[l-1]==k){
        		return l-1;
        	}
        	else{
        		return -1;
        	}
        }
        int main(){
        	cin>>n;
        	for(int i=1;i<=n;i++){
        		cin>>f[i];
        	}
        	cin>>m;
        	while(m--){
        		int g;
        		cin>>g;
        		cout<<find(g)<<" ";
        	}
        	return 0;
        }
        • 1

        Information

        ID
        1231
        Time
        1000ms
        Memory
        128MiB
        Difficulty
        6
        Tags
        # Submissions
        245
        Accepted
        73
        Uploaded By