不慌不忙、不急不躁

再坚持一下,一切都会好起来的。

hdu 5531 Rebuild(三分)

题目链接:hdu 5531 Rebuild

解题思路

假设每条边的长度为a,b,c,d。那么如果r0=x,那么r1=ax,r2=ba+x,r3=cb+ax...依次类推,可以确定x的取值范围[L,R],然后在区间上三分即可。注意,如果n为奇数是,L=R,即最后r0=cb+ar0,x最后要等于奇数边的长度和减除偶数边长度和除二;如果n为偶数,则要保证奇数边长度和等于偶数边长度和才能有解。

代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
const int maxn = 1e4 + 5;
const double inf = 1e20;
const double pi = 4 * atan(1.0);
const double eps = 1e-8;

int N;
double L, R, X[maxn], Y[maxn], A[maxn];

double getDistance(double x, double y) { return sqrt(x * x + y * y); }

void init () {
    scanf("%d", &N);
    for (int i = 0; i < N; i++) scanf("%lf%lf", &X[i], &Y[i]);

    double s = 0;
    L = 0, R = inf;
    for (int i = 0; i < N; i++) {
        int v = (i + 1) % N;
        s = getDistance(X[i]-X[v], Y[i]-Y[v]) - s;
        if (i&1) L = max(L, -s);
        else R = min(R, s);
    }

    /*
    s = 0;
    for (int i = 1; i <= N; i++) {
        int u = N - i;
        int v = (N - i + 1) % N;
        s = getDistance(X[u]-X[v], Y[u]-Y[v]) - s;
        if (i&1) R = min(R, s);
        else L = max(L, -s);
    }
    */
}

double get(double m) {
    double ret = m * m;
    A[0] = m;
    for (int i = 1; i < N; i++) {
        double s = getDistance(X[i] - X[i-1], Y[i] - Y[i-1]);
        m = s - m;
        ret += m * m;
        A[i] = m;
    }
    return ret;
}

double solve () {
    while (R - L > eps) {
        double l = (2 * L + R) / 3;
        double r = (L + 2 * R) / 3;
        if (get(l) >= get(r)) L = l;
        else R = r;
    }
    return L;
}

int main () {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        init();
        double ans = 0;
        for (int i = 0; i < N; i++) {
            int v = (i + 1) % N;
            ans = getDistance(X[i] - X[v], Y[i] - Y[v]) - ans;
        }

        if ((L > R) || (N % 2 == 0 && fabs(ans) > eps)) printf("IMPOSSIBLE\n");
        else {

            if (N&1) ans /= 2;
            else ans = solve();
            printf("%.2lf\n", get(ans) * pi);
            for (int i = 0; i < N; i++) printf("%.2lf\n", A[i]);
        }
    }
    return 0;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011328934/article/details/49583527
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭