2 solutions

  • 0
    @ 2024-11-1 17:17:00

    #include <stdio.h> void find(int k,int n,int a[]) { int l=-1; int r=n; while(l+1<r){ int mid=(r+l)/2; if(a[mid]>k){ r=mid; } else{ l=mid; } } if(a[l]==k){ while(a[l]==k){ l--; if(a[l]!=k){ l++; printf("%d ",l+1); break; } } } else{ printf("-1 "); } } int main() { int n,m,i; scanf("%d",&n); int a[n]; for(i=0;i<n;i++){ scanf("%d",&a[i]); } scanf("%d",&m); while(m>0){ int q; scanf("%d",&q); find(q,n,a); m--; } return 0; }

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

      Information

      ID
      1230
      Time
      1000ms
      Memory
      128MiB
      Difficulty
      5
      Tags
      # Submissions
      244
      Accepted
      91
      Uploaded By