<每日一题> Day7:CodeForces-1166C.A Tale of Two Lands (二分 + 排序)

原题链接

参考代码:

#include <cstdio>
#define mid ((l + r) / 2)
#include <algorithm>
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;
}

 

posted @ 2019-05-28 00:06  Cruel_King  阅读(179)  评论(0编辑  收藏  举报