Rebuild+三分

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ujn20161222/article/details/82930319

https://blog.csdn.net/LuRiCheng/article/details/70135430

当n%2==1

假如n=3

r1+r2=L1

r2+r3=L2

r3+r1=L3

L1-L2+L3=2*r1

所以r1有唯一解:r1=(L1-L2+L3)/2.0

r2=L1-r1

r3=L2-r2

.....

依次检查r1到r2是否<0

当n%2==0

假如n=4

r1+r2=L1

r2+r3=L2

r3+r4=L3

r4+r1=L4

L1-L2+L3-L4=0

只有满足L1+L3=L2+L4时才可能有解

不难发现 对任意ri都可以表示为ri=x+k*r1

r1=r1

r2=L1-r1

r3=L2-L1+r1

.....

而总的面积S=PI*(r1^2+r2^2+r3^2+.....)

于是得到一个关于r1的一元二次方程:S=a*r1^2+b*r1+c

当ri=x+r1 --> ri>=0 --> x+r1>=0 --> r1>=-x

当ri=x-r1 --> ri>=0 --> x-r1>=0 --> r1<=x

通过以上约束 , 求得r1的范围[L,R]

然后可以使用三分法求解使得S最小的r1

或者 使用一元二次方程性质 ax^2+bx+c=0时 ,当x=-b/(2a)时,取得最值

再判断每边是否<0即可

Rebuild

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


 

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 i−1 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.

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

Source

2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)

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

const int N=1e4+5;

struct Point{
	int x,y;
}P[N];
double Pi=acos(-1.0);

double d[N];
double r[N];

double dis(Point A, Point B){
	return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
int n;
double check(double R){
	r[0]=R;
	double S=R*R;
	for(int i=1;i<n;i++){
		S+=(d[i-1]-r[i-1])*(d[i-1]-r[i-1]);
		r[i]=d[i-1]-r[i-1];
	}
	return S;
}

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		
		for(int i=0;i<n;i++)
		scanf("%d%d",&P[i].x,&P[i].y);
		for(int i=0;i<n;i++){
			d[i]=dis(P[i],P[(i+1)%n]);
		}
		double dd=0;
		double ll=0,rr=1e9;
		for(int i=0;i<n;i++)
		if(i%2)dd-=d[i],ll=max(ll,dd);
		else dd+=d[i],rr=min(rr,dd);
		if(rr<ll-1e-8){
			puts("IMPOSSIBLE");
			continue;
		}
		if(n%2){
			printf("%.2f\n",check(dd/2)*Pi);
			for(int i=0;i<n;i++){
				printf("%.2f\n",r[i]);
			}
		}else{
			if(fabs(dd)>1e-8){
				puts("IMPOSSIBLE");
				continue;
			}
			int pp=200;
			while(pp--){
				double mid=(ll+rr)/2;
				double mmid=(mid+rr)/2;
				if(check(mid)>check(mmid)){
					ll=mid;
				}else
					rr=mmid;
			}
			printf("%.2f\n",check(ll)*Pi);
			for(int i=0;i<n;i++)
			printf("%.2f\n",r[i]);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ujn20161222/article/details/82930319
今日推荐