Codeforces 989D A Shade of Moonlight

A Shade of Moonlight

列列式子发现, 对于同一个云来说只有反向的云才能和它相交, 然后我们发现这个东西有单调性,然后二分就好啦。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long

using namespace std;

const int N = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;
const double PI = acos(-1);

int n, l, w;
vector<int> a;
vector<int> b;

bool check(int x1, int x2) {
    return abs(x1 + x2 + l) < 1LL * (x2 + l - x1) * w;
}

int main() {
    scanf("%d%d%d", &n, &l, &w);
    for(int i = 1; i <= n; i++) {
        int x, v; scanf("%d%d", &x, &v);
        if(v == 1) a.push_back(x);
        else b.push_back(x);
    }
    LL ans = 0;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    for(auto& x2 : b) {
        int low = 0;
        int high = lower_bound(a.begin(), a.end(), x2) - a.begin() - 1;
        int p = -1;
        while(low <= high) {
            int mid = (low + high) >> 1;
            if(check(a[mid], x2)) p = mid, low = mid + 1;
            else high = mid - 1;
        }
        ans += p + 1;
    }
    printf("%lld
", ans);
    return 0;
}

/*
*/
原文地址:https://www.cnblogs.com/CJLHY/p/10679333.html