3 solutions

  • 0
    @ 2024-11-2 9:27:48

    #include <stdio.h> void find(int t,int k,int n,int a[]) { int y; 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(t0){ if(a[r-1]k){ printf("%d\n",r-1); } else{printf("-1\n");} } else if(t1){ if(a[l]k){ while(a[l]k){ l--; if(a[l]!=k){ l++; printf("%d\n",l); break; } } } else{printf("-1\n");} } else if(t2){ scanf("%d",&y); int i=0,cnt=0; while(i<=n){ if(a[i]>=k&&a[i]<=y){ cnt++; } i++; } if(cnt0){printf("-1\n");} else{printf("%d\n",cnt);} } else if(t3){ if(a[l]==k){ while(a[l]k){ l++; if(a[l]!=k){ printf("%d\n",a[l]); break; } } } else{printf("-1\n");} } else if(t4){ if(a[l]==k){ while(a[l]==k){ l--; if(a[l]!=k){ l--; printf("%d\n",l-2); break; } } } else{printf("-1\n");} } } int main() { int n,i,m,t,k; scanf("%d",&n); int a[n]; for(i=0;i<n;i++){ scanf("%d",&a[i]); } scanf("%d",&m); while(m>0){ scanf("%d %d",&t,&k); find(t,k,n,a); m--; } return 0; }

    Information

    ID
    1529
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    175
    Accepted
    62
    Uploaded By