Given a sequence A1.A2....An, give you an integer K, find the number of pairs (i, j) that satisfy all Ai+Aj>=k

 

#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define maxn 100010
/*
Given a sequence A1.A2....An, give you an integer K, find the number of pairs (i, j) that satisfy all Ai+Aj>=k
*/ 
int main()
{
    int n,a[105],k,low,upp;

    while(cin >> n >> k){
        int ans1 = 0, ans2 = 0;

        for(int i=0; i<n; i++)
            cin >> a[i];
        sort(a,a+n);
        //ans1 = lower_bound(a,a+n,k)-a;
        //ans2 = upper_bound(a,a+n,k)-a;

        for(int i=0; i<n; i++){
            ans1 += n - (lower_bound(a+i+1,a+n,k-a[i]) - a);/*
            Converted to A[j]>=kA[i], binary search in A[i]~A[n] for the first element A[j] greater than or equal to kA[i],
            All elements up to the end of the array are solved and can be added up. */ 
            ans2 += n - (upper_bound(a+i+ 1 ,a+n,ka[i]) - a); // (>) 
        }   
         /*
        for(int i=0; i<n; i++){
            ans1 = lower_bound(a+i+1,a+n,k) - a;//Find the first position where this number appears
            ans2 = upper_bound(a+i+1,a+n,k) - a;//Find the position after the last position where this number appears
        }*/
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}
two points

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324598442&siteId=291194637