hdu5531 Rebuild

Rebuild

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


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.
 

Sample Input
  
  
3 4 0 0 11 0 27 12 5 12 5 0 0 7 0 7 3 3 6 0 6 5 0 0 1 0 6 12 3 16 0 12
 

Sample Output
  
  
988.82 3.75 7.25 12.75 9.25 157.08 6.00 1.00 2.00 3.00 0.00 IMPOSSIBLE
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5551  5550  5549  5548  5547 
 
就是解方程以及2次函数求极限
#include<bits/stdc++.h>
using namespace std;
const int maxn=11000;
const int inf=0x7fffffff;
const int eps=1e-7;
const double PI=acos(-1);
int n,x[maxn];
void init(){
    scanf("%d",&n);
    int nx,ny,px,py,fx,fy;
    for(int i=1;i<=n;i++){
        scanf("%d%d",&nx,&ny);
        if(i==1)fx=nx,fy=ny;
        else x[i]=(int)sqrt(abs(nx-px)*abs(nx-px)+abs(ny-py)*abs(ny-py));
        px=nx;py=ny;
    }
    x[1]=(int)sqrt(abs(nx-fx)*abs(nx-fx)+abs(ny-fy)*abs(ny-fy));
}
double r[maxn];
void output(double v,double A,double B,double C){
    r[1]=v;
    for(int i=2;i<=n;i++)
        r[i]=x[i]-r[i-1];
    if(r[n]+r[1]>x[1]+eps||r[n]+r[1]<x[1]-eps){puts("IMPOSSIBLE");return;}
    printf("%.2lf\n",(A*v*v+B*v+C)*PI);
    for(int i=1;i<=n;i++)printf("%.2lf\n",r[i]);
}
void doit1(){
    int tmp=x[1],k=1;
    for(int i=2;i<=n;i++,k=-k)tmp+=k*x[i];
    double S=0;
    r[1]=tmp/2;if(r[1]<0){puts("IMPOSSIBLE");return;}
    S=r[1]*r[1];
    for(int i=2;i<=n;i++){
        r[i]=x[i]-r[i-1];
        if(r[i]<0){puts("IMPOSSIBLE");return;}
        S+=r[i]*r[i];
    }
    printf("%.2lf\n",S*PI);
    for(int i=1;i<=n;i++)printf("%.2lf\n",r[i]);
}
void doit2(){
    double minr=0,maxr=2000000;
    double A=n,B=0,C=0;
    double k=-1,b=0;
    for(int i=2;i<=n;i++,k=-k){
        b=-b+x[i];
        B+=2*k*b;
        C+=b*b;
        if(k==-1&&b<maxr)maxr=b;
        if(k==1&&-b>minr)minr=-b;
    }
    if(maxr<minr){puts("IMPOSSIBLE");return;}
    double v1=A*minr*minr+B*minr+C;
    double v2=A*maxr*maxr+B*maxr+C;
    double x=-B/2/A,v3=inf;
    if(x<=maxr+eps&&x>=minr-eps)v3=A*x*x+B*x+C;
    if(v1<=v2+eps&&v1<=v3+eps)output(minr,A,B,C);
    else if(v2<=v1+eps&&v2<=v3+eps)output(maxr,A,B,C);
    else output(x,A,B,C);
}
void work(){
    if(n&1)doit1();
    else doit2();
}
int main(){
    int t;scanf("%d",&t);
    while(t--)init(),work();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值