Rebuild hdu-5531 (精度)

版权声明:如有错误,请指出,不胜感激。 https://blog.csdn.net/qq_36424540/article/details/82954145

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 i−1 are adjacent for 1<i≤n, 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.

1≤t≤100
3≤n≤104
|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

再一次被卡精度,1e-8 WA ,1e-12 TLE,  以后的精度题,我们就设置成1e-10吧,时间还是可以承受住的

那个 \pi 最后再去乘,可能会有精度损失

根据题意我们可以列出 

x1+x2=dis[1]

x2+x3=dis[2]

...

xn+x1=dis[n]

我们肯定都喜欢单变量,所以我们 把  x2,x3,...xn  全都换成  ?\pm x1

x1

x2=dis[1]-x1

x3=dis[2]-x2=dis[2]-dis[1]+x1

x4=dis[3]-x3=dis[3]-dis[2]+dis[1]-x1

...

x_{n}=dis[n-1]-x_{n-1}=dis[n-1]-dis[n-2]+dis[n-3]+...+?dis[1]+?x1

容易发现 当 n 是奇数的时候, 存在  x_{n}+x_{1}=dis[n], 也就是 xn=dis[n]-x1,  所以和 前面对应的方程联立,

可以直接解出来 x。

当n是偶数的时候,存在很多可能的解,三分找最小值就好了。

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)

const int N=1e4+10;
const double eps=1e-10;
const double PI=acos(-1);

struct Point{
	double x,y;
}p[N];
double dis[N],r[N];

double solve(int n,double x){
	r[0]=x;double res=0;
	rep(i,0,n){
		r[i+1]=dis[i]-r[i];
		res+=r[i]*r[i];
	}
	return res*PI;
}

int main(){
	//freopen("123.txt","r",stdin);
	//freopen("456.txt","w",stdout);
	int T;
	scanf("%d",&T);
	while(T--){
		int n;
		scanf("%d",&n);
		rep(i,0,n){
			scanf("%lf %lf",&p[i].x,&p[i].y);
		}

		p[n]=p[0];
		rep(i,0,n){
			//double d=;
			dis[i]=sqrt((p[i+1].x-p[i].x)*(p[i+1].x-p[i].x)+(p[i+1].y-p[i].y)*(p[i+1].y-p[i].y));
		}

		int ok=1;
		if(n&1){
			double sum=0,res=0;
			rep(i,0,n){
				if(i&1)
					sum=sum-dis[i];
				else
					sum=sum+dis[i];
				//printf("i:%d sum:%.2f dis:%.2f\n",i,sum,dis[i]);
			}
			r[0]=sum/2;
			//printf("sum:%.2f\n",sum);
			rep(i,0,n){
				r[i+1]=dis[i]-r[i];
				if(r[i]<0||r[i]-dis[i]>0)ok=0;
				res+=r[i]*r[i];
			}
			res*=PI;
			//if(fabs(r[n]-r[0])>eps)ok=0;

			if(!ok)printf("IMPOSSIBLE\n");
			else{
				printf("%.2f\n",res);
				rep(i,0,n){
					printf("%.2f\n",r[i]);
				}
			}
		}
		else{
			double L=0,R=1e18;

			double sum=0,sum1=0,sum2=0;
			rep(i,0,n){
				if(i&1){
					sum1+=dis[i];
					sum-=dis[i];
					L=max(L,sum);
				}
				else{
					sum2+=dis[i];
					sum+=dis[i];
					R=min(R,sum);
				}
				//printf("i:%d sum:%.2f dis:%.2f\n",i,sum,dis[i]);
			}
			//printf("L:%.2f R:%.2f\n",L,R);

			if(fabs(sum1-sum2)>eps||L-R>eps)ok=0;

			double ans=0;r[0]=L;
			rep(i,0,n){
				r[i+1]=dis[i]-r[i];
				//if(r[i]<0||r[i]-dis[i]>eps)ok=0;
				ans+=r[i]*r[i];
			}
			ans*=PI;
			//if(fabs(R-L)<eps&&fabs(r[n]-r[0])>eps)ok=0;

			if(!ok){
				printf("IMPOSSIBLE\n");
			}
			else{
				double mid1,mid2,res=L;
				while(R-L>eps){
					mid1=L+(R-L)/3;
					mid2=R-(R-L)/3;
					double ans1=solve(n,mid1),ans2=solve(n,mid2);
					if(ans1-ans2>eps){
					   L=mid1;
					   res=mid2;
					}
					else{
					   R=mid2;
					   res=mid1;
					}
				}
				//printf("L:%.2f R:%.2f\n",L,R);
				ans=solve(n,res);
				printf("%.2f\n",ans);
				rep(i,0,n){
					printf("%.2f\n",r[i]);
				}
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/82954145
今日推荐