2 solutions

  • 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
      119
      Accepted
      47
      Uploaded By