标签:tween nta type pillar sqrt 转换 ons Plan gen
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5531
#include <bits/stdc++.h> #define met(a, b) memset(a, b, sizeof(a)) #define ll long long #define ull unsigned long long #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; typedef pair<int,int>P; const int maxn=100010; const double eps=1e-10; const double pi=acos(-1); P p[maxn]; double d[maxn],f[maxn]; double dist(P a,P b) { return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); } int main() { int T; cin>>T; while(T--){ int n; cin>>n; for(int i=1;i<=n;i++)cin>>p[i].first>>p[i].second; for(int i=1;i<=n;i++){ if(i==n)d[i]=dist(p[i],p[1]); else d[i]=dist(p[i],p[i+1]); } double maxx=0x3f3f3f3f,minn=0;//极值上下限 f[1]=0; for(int i = 2; i <=n ; i++) { f[i] = d[i-1] - f[i-1]; if(i%2==0 && f[i] < maxx)//若为偶数点,则该圆的半径只能减小这么多(即第一个圆的半径只能增大这么多),更新最大值下限 { maxx = f[i]; } if(i%2==1 && (-f[i]) > minn)//若为奇数点,且此时f[i]小与0,则必须第一个圆的半径更新为该值,更新最小值上限 { minn = -f[i]; } } if(minn >= maxx + eps )//无解 { printf("IMPOSSIBLE\n"); continue; } if(n%2==1){//奇数个点,有解则必有唯一解,否则无解 double x=0;//第一个圆的半径x=(d1-d2+d3-d4...)/2,唯一解. for(int i=1;i<=n;i++){ if(i%2==1)x+=d[i]; else x-=d[i]; } x/=2; if(x<=minn-eps||x>=maxx+eps){ cout<<"IMPOSSIBLE"<<endl; continue; } double area=0; for(int i=1;i<=n;i++){ if(i%2==1)area+=(f[i]+x)*(f[i]+x); else area+=(f[i]-x)*(f[i]-x); } area*=pi; printf("%.2f\n",area); for(int i=1;i<=n;i++){ if(i%2==1)printf("%.2f\n",f[i]+x); else printf("%.2f\n",f[i]-x); } } else{//偶数情况构造二次函数,y=a*x*x+b*x+c double now=0; for(int i=1;i<=n;i++){ if(i%2==1)now+=d[i]; else now-=d[i]; } if(fabs(now)>eps||minn-maxx>eps){ cout<<"IMPOSSIBLE"<<endl; continue; } double a=n; double b=0,c=0; for(int i=1;i<=n;i++){ if(i%2==1){ b+=2*f[i]; } else{ b-=2*f[i]; } c+=f[i]*f[i]; } double x=-b/(2*a); if(x<minn+eps)x=minn; if(x>maxx-eps)x=maxx; double area=a*x*x+b*x+c; area*=pi; printf("%.2f\n",area); for(int i=1;i<=n;i++){ if(i%2==1)printf("%.2f\n",f[i]+x); else printf("%.2f\n",f[i]-x); } } } return 0; }
E - Rebuild UVALive - 7187 (二次函数极值问题)
标签:tween nta type pillar sqrt 转换 ons Plan gen
原文地址:https://www.cnblogs.com/cherish-lin/p/11503242.html