Day7:CodeForces-1166C.A Tale of Two Lands (二分 + 排序)

原题链接

参考代码:

#include 
#define mid ((l + r) / 2)
#include 
using namespace std;

const int maxn = 200000 + 5, maxvalue = 1e9;
int n, value[maxn];

int main() {
    scanf("%d", &n);
    for(int i = 0; i < n; i ++) {
        scanf("%d", &value[i]);
        if(value[i] < 0)
            value[i] = - value[i];
    }
    sort(value, value + n);
    long long ans = 0;
    int l, r;
    for(int i = 0; i < n; i ++) {
        l = i + 1, r = n - 1;
        while(l <= r) {
            if(2 * value[i] >= value[mid]) l = mid + 1;
            else r = mid - 1;
        }
        ans += (l - i - 1);
    }
    printf("%lld\n", ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/bianjunting/p/10934506.html

你可能感兴趣的:(Day7:CodeForces-1166C.A Tale of Two Lands (二分 + 排序))