题解 | #圆覆盖#

圆覆盖

https://www.nowcoder.com/practice/4f96afe5dfe74dad88dbe419d33f9536

按照对所有的点进行排序 然后求出点权的前缀和 很容易想到双二分, 注意一下数据的边界, 如果无解

#include <bits/stdc++.h>

#define x first
#define y second
#define all(x) x.begin(), x.end()
#define vec1(T, name, n, val) vector<T> name(n, val)
#define vec2(T, name, n, m, val) vector<vector<T>> name(n, vector<T>(m, val))
#define vec3(T, name, n, m, k, val) vector<vector<vector<T>>> name(n, vector<vector<T>>(m, vector<T>(k, val)))
#define vec4(T, name, n, m, k, p, val) vector<vector<vector<vector<T>>>> name((n), vector<vector<vector<T>>>((m), vector<vector<T>>((k), vector<T>((p), (val)))))

using namespace std;
using i128 = __int128;
using u128 = unsigned __int128;
using LL = long long;
using LD = long double;
using ULL = unsigned long long;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using PLD = pair<LD, LD>;

const int N = 1e5 + 10, MOD = 998244353;
const int INF = 1e9;
const LL LL_INF = 2e18;
const LD EPS = 1e-8;
const int dx4[] = {-1, 0, 1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy8[] = {-1, 0, 1, -1, 1, -1, 0, 1};

istream& operator>>(istream& is, i128& val) {
    string str;
    is >> str;
    val = 0;
    bool flag = false;
    if (str[0] == '-') flag = true, str = str.substr(1);
    for (char& c : str) val = val * 10 + c - '0';
    if (flag) val = -val;
    return is;
}

ostream& operator<<(ostream& os, i128 val) {
    if (val < 0) os << "-", val = -val;
    if (val > 9) os << val / 10;
    os << static_cast<char>(val % 10 + '0');
    return os;
}

bool cmp(LD a, LD b) {
    if (fabs(a - b) < EPS) return 1;
    return 0;
}

LL qpow(LL a, LL b) {
    LL ans = 1;
    a %= MOD;
    while (b) {
        if (b & 1) ans = ans * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return ans;
}

void solve() {
    int n;
    LL s;
    cin >> n >> s;
    
    struct F {
        int x, y;
        LL v;
        bool operator< (const F &f) const {
            return 1ll * x * x + 1ll * y * y < 1ll * f.x * f.x + 1ll * f.y * f.y;
        };
    };

    vector<F> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i].x >> a[i].y >> a[i].v;
    }

    sort(all(a));

    auto calc = [&](LD v) {
        int res = -1;
        int l = 0, r = n - 1;
        while (l <= r) {
            int mid = l + r >> 1;
            if (1.0l * a[mid].x * a[mid].x + 1.0l * a[mid].y * a[mid].y > v) {
                res = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }

        if (res == -1) res = n - 1;
        return res;
    };

    vector<LL> sm(n + 1);
    for (int i = 1; i <= n; ++i) sm[i] = sm[i - 1] + a[i - 1].v;

    LD ans = -1;
    LD l = 0.0, r = 2.0l * INF;
    while (!cmp(l, r)) {
        LD mid = (l + r) / 2;
        int idx = calc(mid * mid);
        if (sm[idx] >= s) {
            ans = mid;
            r = mid;
        }
        else l = mid;
    }

    if (ans >= 2.0l * INF) ans = -1;
    cout << fixed << setprecision(15) << ans << '\n';

/**/ #ifdef LOCAL
            cout
        << flush;
/**/ #endif
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int T = 1;
    while (T--) solve();
    cout << fixed << setprecision(15);

    return 0;
}
全部评论

相关推荐

Gardenia06...:刚开始学是这样的,可以看看左神和灵神都讲的不错
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务