洛谷5月月赛 T2 偷上网 题解

版权声明:随便转载。 https://blog.csdn.net/zhang14369/article/details/80433088

作者:岸芷汀兰:

一、题目:

洛谷原题

二、思路:

暴力搜索出奇迹。
随机大法出奇迹。
这道题真的必须用随机。每次随机一个点,判断一下是否可行,限制一下循环次数,如果随机了1000次仍不行,输出GG。


注意精度问题。

三、代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>

typedef long long ll;

using namespace std;
inline int read(void)
{
    int x = 0, f = 1; char ch = getchar();
    while (ch<'0' || ch>'9') {
        if (ch == '-')f = -1;
        ch = getchar();
    }
    while (ch >= '0'&&ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return f * x;
}

const int maxn = 15;

int n;
double l, x[maxn], y[maxn];

inline double calc(double x1, double y1, double x2, double y2) {
    return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
}

inline double random1() {
    //    return x % ((int)l + 1);
    ll x = (ll)rand() * (ll)998244353 + (ll)rand();
    x %= ((ll)l * 1000);
    return (double)x / 1000.0;
}

inline bool check(double xx, double yy) {
    bool flag = true;
    for (register int i = 1; i <= n; i++) {
        if (calc(xx, yy, x[i], y[i]) < l * l / n / n) { flag = false; break; }
    }
    return flag;
}

int main() {
    srand((unsigned)time(0));
    cin >> n >> l;
    for (register int i = 1; i <= n; i++) {
        cin >> x[i] >> y[i];
    }
    for (register int i = 1; i <= 1000; i++) {
        double xx = random1(), yy = random1();
        if (check(xx, yy)) {
            printf("%.3f %.3f\n", (double)xx, (double)yy); return 0;
        }
    }
    puts("GG");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhang14369/article/details/80433088
今日推荐