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; }

    • 0
      @ 2022-2-26 1:23:30

      二分查找可以偷个小懒~

      #include<bits/stdc++.h>
      
      using namespace std;
      
      const int N = 1e5 + 5;
      int num[N];
      
      int main(void)
      {
      	int n;
      	cin >> n;
      	for (int i = 0; i < n; i++) cin >> num[i];
      	
      	int t;
      	cin >> t;
      	for (int k = 0; k < t; k++)
      	{
      		int x;
      		cin >> x;
      
      		if (x == 0)
      		{
      			int i;
      			cin >> i;
      			int a = lower_bound(num, num + n, i)-num;
      			int b = upper_bound(num, num + n, i) - num-1;
      			if (num[a] == num[b]) cout << b << endl;
      			else cout << -1 << endl;
      		}
      
      		else if (x == 1)
      		{
      			int i;
      			cin >> i;
      			int a = lower_bound(num, num + n, i) - num;
      			int b = upper_bound(num, num + n, i) - num - 1;
      			if (num[a] == num[b]) cout << a << endl;
      			else cout << -1 << endl;
      		}
      
      		else if (x == 2)
      		{
      			int j, k;
      			cin >> j >> k;
      			int a = lower_bound(num, num + n, j) - num;
      			int b = upper_bound(num, num + n, k) - num - 1;
      			if (num[n - 1] < j) cout << -1 << endl;
      			else cout << b-a+1 << endl;
      		}
      
      		else if (x == 3)
      		{
      			int i;
      			cin >> i;
      			int a = upper_bound(num, num + n, i) - num;
      			if (num[n - 1] < i) cout << -1 << endl;
      			else cout << a << endl;
      		}
      		
      		else if (x == 4)
      		{
      			int i;
      			cin >> i;
      			int a = lower_bound(num, num + n, i) - num-1;
      			int b = lower_bound(num, num + n, num[a]) - num;
      			if (num[n - 1] < i) cout << -1 << endl;
      			else cout << b << endl;
      		}
      
      	}
      	return 0;
      }
      

      芜湖起飞~

      • 0
        @ 2022-2-13 17:32:07
        #include<bits/stdc++.h>
        using namespace std;
        const int N=1e6+5;
        #define ll long long
        ll f[N],x,y,z,l,r,n;
        ll find0(ll g){
        	l=1,r=n;
        	while(l<=r){
        		ll mid=(l+r)/2;
        		if(f[mid]<=g){
        			l=mid+1;
        		}
        		else{
        			r=mid-1;
        		}
        	}
        	if(f[l-1]==g){
        		return l-2;
        	}
        	else{
        		return -1;
        	}
        }
        ll find1(ll g){
        	l=1,r=n;
        	while(l<=r){
        		ll mid=(l+r)/2;
        		if(f[mid]>=g){
        			r=mid-1;
        		}
        		else{
        			l=mid+1;
        		}
        	}
        	if(f[l]==g){
        		return l-1;
        	}
        	else{
        		return -1;
        	}
        }
        ll find3(ll g){
        	l=1,r=n;
        	while(l<=r){
        		ll mid=(l+r)/2;
        		if(f[mid]<=g){
        			l=mid+1;
        		}
        		else{
        			r=mid-1;
        		}
        	}
        	if(f[l]>g){
        		return f[l];
        	}
        	else{
        		return -1;
        	}
        }
        ll find4(int g){
        	l=1,r=n;
        	while(l<=r){
        		ll mid=(l+r)/2;
        		if(f[mid]>=g){
        			r=mid-1;
        		}
        		else{
        			l=mid+1;
        		}
        	}
        	if(f[r]<g){
        		return f[r];
        	}
        	else{
        		return -1;
        	}
        }
        ll findy(ll g){
        	l=1,r=n;
        	while(l<=r){
        		ll mid=(l+r)/2;
        		if(f[mid]<=g){
        			l=mid+1;
        		}
        		else{
        			r=mid-1;
        		}
        	}
        	if(f[l-1]==g){
        		return l-2;
        	}
        	else{
        		return r;
        	}
        }
        ll findz(ll g){
        	l=1,r=n;
        	while(l<=r){
        		ll mid=(l+r)/2;
        		if(f[mid]>=g){
        			r=mid-1;
        		}
        		else{
        			l=mid+1;
        		}
        	}
        	if(f[l]==g){
        		return l-1;
        	}
        	else{
        		return r-1;
        	}
        }
        int main(){
        	cin>>n;
        	for(ll i=1;i<=n;i++){
        		cin>>f[i];
        	}
        	int t;
        	cin>>t;
        	while(t--){
        		ll ans=0;
        		cin>>x;
        		if(x!=2){
        			cin>>y;
        			if(x==0){
        				cout<<find0(y)<<endl;
        			}
        			if(x==1){
        				cout<<find1(y)<<endl;
        			}
        			if(x==3){
        				cout<<find3(y)<<endl;
        			}
        			if(x==4){
        				cout<<find4(y)<<endl;
        			}
        		}
        		else{
        			cin>>y>>z;
        			ans=findy(z)-findz(y)+1;
        			cout<<ans<<endl;
        		}
        	}
        	return 0;
        }
        • 1

        Information

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