HDU 5531 Rebuild(几何)

Rebuild

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2660    Accepted Submission(s): 584


Problem Description
Archaeologists find ruins of Ancient ACM Civilization, and they want to rebuild it.

The ruins form a closed path on an x-y plane, which has n endpoints. The endpoints locate on (x1,y1) , (x2,y2) , ,(xn,yn) respectively. Endpoint i and endpoint i1 are adjacent for 1<in , also endpoint 1 and endpoint n are adjacent. Distances between any two adjacent endpoints are positive integers.

To rebuild, they need to build one cylindrical pillar at each endpoint, the radius of the pillar of endpoint i is ri . All the pillars perpendicular to the x-y plane, and the corresponding endpoint is on the centerline of it. We call two pillars are adjacent if and only if two corresponding endpoints are adjacent. For any two adjacent pillars, one must be tangent externally to another, otherwise it will violate the aesthetics of Ancient ACM Civilization. If two pillars are not adjacent, then there are no constraints, even if they overlap each other.

Note that ri must not be less than 0 since we cannot build a pillar with negative radius and pillars with zero radius are acceptable since those kind of pillars still exist in their neighbors.

You are given the coordinates of n endpoints. Your task is to find r1,r2,,rn which makes sum of base area of all pillars as minimum as possible.



For example, if the endpoints are at (0,0) , (11,0) , (27,12) , (5,12) , we can choose ( r1 , r2 , r3 , r4 ) = ( 3.75 , 7.25 , 12.75 , 9.25 ). The sum of base area equals to 3.752π+ 7.252π+12.752π+9.252π=988.816 . Note that we count the area of the overlapping parts multiple times.

If there are several possible to produce the minimum sum of base area, you may output any of them.
 

Input
The first line contains an integer t indicating the total number of test cases. The following lines describe a test case.

The first line of each case contains one positive integer n , the size of the closed path. Next n lines, each line consists of two integers (xi,yi) indicate the coordinate of the i -th endpoint.

1t100
3n104
|xi|,|yi|104
Distances between any two adjacent endpoints are positive integers.
 

Output
If such answer doesn't exist, then print on a single line "IMPOSSIBLE" (without the quotes). Otherwise, in the first line print the minimum sum of base area, and then print n lines, the i -th of them should contain a number ri , rounded to 2 digits after the decimal point.

If there are several possible ways to produce the minimum sum of base area, you may output any of them.
 

【链接】5531

【题意】N个点构成封闭图形,现按顺序给出N个点的坐标,要求以每个点为圆心作一个圆,同时相邻的两个点的圆须外切。求所有圆面积之和的最小值和此时各圆半价。

【分析】

相当于给定条件:

R1+R2=D1;

R2+R3=D2;

……

Rn-1+Rn=Dn-1;

Rn+R1=Dn;

求R1^2+R2^2+……+Rn^2的最小值。

可以发现,根据这些等式,可以将所有R都用R1表示,最后就是求一个关于R1的表达式的极值。

R1=R1;

R2=D1-R1;

R3=D2-D1+R1;

R4=D3-D2+D1-R1;

……

Ri^2=R1+2Xi*R1+Xi^2;

S=N*R1^2+2(X1+X2+……Xn)R1+(X1^2+X2^2+……Xn^2)=N*R1^2+2*b*R1+c,当R1=-b/N时,表达式取得最小值。

然后来考虑取值的一系列限制条件。

首先,所有R>=0,因此将所有Xi算出后就可以得到R1取值的上下界,设为top和low,因为表达式是二次函数,为取得范围内最小值,最后R1的取值只会是-b/N、top和low中的一个。

再来看,当N=3时:

R1=R1;

R2=D1-R1;

R3=D2-D1+R1;

R3=D3-R1;

根据最后两项,发现此时R1有唯一解:(D3-D2+D1)/2。
因此在这个情况下,只要考虑该解能否满足所有Ri>=0,若满足,输出答案,若有任意一个不满足,就输出IMPOSSIBLE。
当N=4时:
R1=R1;
R2=D1-R1;
R3=D2-D1+R1;
R4=D3-D2+D1-R1;
R4=D4-R1;
根据最后两项,发现方程组有解的条件为D3+D1=D4+D2。因此当不满足条件时输出IMPOSSIBLE,满足时再按上面的方法求解R1。

这题在现场赛的时候正确率很低,前几天模拟比赛的时候,俱乐部里的其他队伍wa的很厉害,我补题的时候也wa了好久,找题解时很多人都说精度很坑……
但是!
再次审视题目,我发现了一个关键的地方,那就是,这个图形的边长是整型,所有Di都是整型,也就是说,所有b、c、top、low,都是整型,根本不会有什么精度问题。
觉得是浮点数,大概是下意识的就这么想了。
题目还是很善良的,但是大家读题都太不仔细了。
把所有double改成long long直接就AC了。
另外很多人用三分来做…没有必要啊。

【AC代码】374ms
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
#define maxn 10005
#define INF 0x3f3f3f3f
#define eps 1e-10
const double pai=acos(-1);
typedef struct{
    int x,y;
}node;
node point[maxn];
long long dis[maxn];
double rad[maxn];
long long low,top,sum,s,b,c;
double ans,R;
int N;
void print(){
    printf("%.2lf\n",ans);
    for(int i=1;i<=N;i++){
        printf("%.2lf\n",rad[i]);
    }
}
void getR(){
    R=-b*1.0/N;
    if(R-top>eps){
        R=(double)top;
    }
    if(low-R>eps){
        R=(double)low;
    }
}
void getlimit(){
    low=0;top=INF;
    b=0;c=0;
    s=-dis[1];
    b+=s;c+=s*s;
    top=min(top,-s);
    for(int i=3;i<=N;i++){
        if(i%2==0){
            s-=dis[i-1];
            b+=s;c+=s*s;
            top=min(top,-s);
        }
        else{
            s+=dis[i-1];
            b+=s;c+=s*s;
            low=max(low,-s);
        }
    }
    s=-dis[N];
    top=min(top,-s);
}
void getans(){
    rad[1]=R;ans=R*R;
    for(int i=2;i<=N;i++){
        rad[i]=dis[i-1]-rad[i-1];
        ans+=rad[i]*rad[i];
        if(rad[i]<-eps){
            printf("IMPOSSIBLE\n");
            return ;
        }
    }
    ans*=pai;
    print();
}
int main(){
    int T;
    cin>>T;
    while(T--){
        scanf("%d",&N);
        for(int i=1;i<=N;i++){
            scanf("%d%d",&point[i].x,&point[i].y);
        }
        point[N+1]=point[1];
        for(int i=1;i<=N;i++){
            dis[i]=sqrt((point[i].x-point[i+1].x)*(point[i].x-point[i+1].x)+(point[i].y-point[i+1].y)*(point[i].y-point[i+1].y));
        }
        sum=0;
        for(int i=1;i<=N;i++){
            sum+=dis[i]*(i%2==0?-1:1);
        }
        if(N%2==0){
            if(sum!=0){
                printf("IMPOSSIBLE\n");
                continue;
            }
            else{
                getlimit();
                if(low>top){
                    printf("IMPOSSIBLE\n");
                    continue;
                }
                getR();
                getans();
            }
        }
        else{
            R=sum/2.0;
            if(R<-eps){
                printf("IMPOSSIBLE\n");
                continue;
            }
            else{
                getans();
            }
        }
    }
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值