Rebuild

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

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 endpointi−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
 
Source
 
 
 

【题意】:

给出一个N边形的顶点和边长,对于每个顶点作一圆,要求邻接顶点对应的两圆外切(不相邻顶点无要求)。找出一组Ri(R>=0)使得所有圆的面积和最小。

【解题思路】:

由于邻接点对应的圆要外切,则确定一个圆的半径r后,即可唯一确定所有圆的半径(Ri = di - Ri-1);设连接点 1 与 n 的边为 d1,以此类推。则必须满足 R1 + Rn = d1。根据此关系可以得到一个关于r与d的等式。

化简容易看出:

①当 n 为奇数时,可行的 r 可以唯一确定,r = d1+ (d2+…+d(2n)) - (d3+…+d(2n+1));

②当 n 为偶数时,必定满足 d1+d3+d5+… = d2+d4+d6+…,否则IMPOSSIBLE(仅为必要条件)

此时 S = r1^2 + … +rn^2  = ax^2+bx+c 为一个二次函数表达式。

用 r 来表示Ri,首先可计算出a b c. 再根据每 ri <= di 这个关系求出r的取值范围[low, high].(无范围则IMPOSSIBLE)

有了二次函数表达式和自变量范围,用三分法求出半径平方和取最小值时的 r .

根据①②求出的 r ,计算出所有的Ri,再依次判断每个Ri是否非负。

注意精度问题(坑点!):

由于本题只保留2位小数,计算面积和时,若用 S = Sum(pi*r*r)计算则会出现精度问题(具体解释不清、亲测会挂),须在括号外乘PI,即 S = pi * Sum(r*r).

现场这题过得不多,封榜时一口气打完交了2发WA,当时感觉思路非常清晰正确,不知道从哪调起,不停换机位到最后也没过。

当时怨念很深,把代码打印带了回来查个究竟。先是在某论坛看到有人说精度这个坑,后来HDU重现赛再打,发现一个重大且傻逼的错误,当时求出二次函数后,以为最低点(-b/2a)如果取不到则IMPOSSIBLE当时想了三分以为跟这个是一样的),但显然要根据 r 的取值范围用三分法来逼近最小值。

长春打铁很遗憾,第一次参加 ICPC 区域赛,既有遗憾也有教训。归根结底还是自己太弱,而且还不好好训练。明年就大三了,有了这次教训,接下来一年一定要好好训练好好总结,争取早日取得成绩。同时,stark 目前也还有各方面欠缺,希望能跟队友共勉,一起努力。

Upd 15.11.6 合肥赛前总结下长春得失,希望合肥站加油!(默默感谢队友wqp让出这个机会).

 
 
 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #define eps 1e-8
  7. #define PI acos(-1.0)
  8. #define LL long long
  9. #define maxn 100100
  10. #define IN freopen("in.txt","r",stdin);
  11. using namespace std;
  12.  
  13. struct Point {
  14. double x,y;
  15. };
  16.  
  17. int sign(double x) {
  18. if(fabs(x)<eps) return ;
  19. return x<? -:;
  20. }
  21.  
  22. double Distance(Point p1,Point p2) {
  23. return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
  24. }
  25.  
  26. int n;
  27. Point p[maxn];
  28. double d[maxn];
  29.  
  30. int check(double r)
  31. {
  32. if(sign(r) < ) return ;
  33.  
  34. for(int i= ;i<=n; i++) {
  35. r = d[i]-r;
  36. if(sign(r) < ) return ;
  37. }
  38. return ;
  39. }
  40.  
  41. /*三分法求面积最小时的r*/
  42. double a,b,c;
  43. double cal(double x) {
  44. return a*x*x + b*x + c;
  45. }
  46.  
  47. double solve(double low, double high)
  48. {
  49. while(high-low > eps){
  50. double mid = (high+low)/2.0;
  51. double midmid = (mid+high)/2.0;
  52. if(sign(cal(midmid)-cal(mid)) > ) high = midmid;
  53. else low = mid;
  54. }
  55.  
  56. return low;
  57. }
  58.  
  59. int main(int argc, char const *argv[])
  60. {
  61. //IN;
  62.  
  63. int t;scanf("%d",&t);
  64. while(t--)
  65. {
  66. double r = ;
  67. memset(p, , sizeof(p));
  68. memset(d, , sizeof(d));
  69. int flag = ;
  70.  
  71. scanf("%d", &n);
  72. for(int i= ;i<=n; i++)
  73. scanf("%lf %lf", &p[i].x, &p[i].y);
  74. d[]=Distance(p[], p[n]);
  75. for(int i=; i<=n; i++)
  76. d[i] = Distance(p[i], p[i-]);
  77.  
  78. if(n% != ){
  79. r = d[];
  80. for(int i=; i<=n; i++){
  81. if(i% == ) r += d[i];
  82. else r -= d[i];
  83. }
  84. r /= 2.0;
  85. flag = check(r);
  86. }
  87.  
  88. else{
  89. double tmp = ;
  90. for(int i=; i<=n; i++){
  91. if(i% != ) tmp += d[i];
  92. else tmp -= d[i];
  93. }
  94. if(sign(tmp) != ) {flag=; goto end;}
  95.  
  96. /*半径平方和的二次表达式*/
  97. a=1.0; b=; c=;
  98. tmp = ;
  99. for(int i=; i<=n; i++){
  100. a += 1.0;
  101. tmp = d[i]-tmp;
  102. c += tmp*tmp;
  103. if(i% == ) b -= 2.0*tmp;
  104. else b += 2.0*tmp;
  105. }
  106.  
  107. /*
  108. 错误地以为最低点为唯一取值点,若最低点不符合则无解.
  109. r=(-b)/(2.0*a);
  110. if(sign(r)<0) r=0;
  111. flag = check(r);
  112. */
  113.  
  114. /*检查每条边计算三分时r的可行范围*/
  115. double low=, high = d[];
  116. tmp = ;
  117. for(int i=; i<=n; i++){
  118. if(i% == ){
  119. tmp += d[i];
  120. high = min(high, tmp);
  121. }
  122. else{
  123. tmp -= d[i];
  124. low = max(low, tmp);
  125. }
  126. }
  127.  
  128. if(low > high) flag = ;
  129. else{
  130. r = solve(low, high);
  131. flag = check(r);
  132. }
  133. }
  134.  
  135. end:
  136. if(!flag) printf("IMPOSSIBLE\n");
  137. else{
  138. double R[maxn];
  139. R[]=r;
  140. double temp = ;
  141. for(int i=; i<=n; i++){
  142. temp = d[i]-R[i-];
  143. R[i] = temp;
  144. }
  145. double ans = ;
  146. for(int i=; i<=n; i++){
  147. //ans += R[i]*R[i]*PI;精度问题
  148. ans += R[i]*R[i];
  149. }
  150.  
  151. printf("%.2lf\n", ans*PI);
  152. for(int i=; i<=n; i++){
  153. printf("%.2lf\n", R[i]);
  154. }
  155. }
  156.  
  157. }
  158.  
  159. return ;
  160. }

HDU 5531 Rebuild (2015长春现场赛,计算几何+三分法)的更多相关文章

  1. HDU 5512 Pagodas (2015沈阳现场赛,找规律+gcd)

    Pagodas Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  2. HDU 5510 Bazinga (2015沈阳现场赛,子串判断)

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  3. hdu 5441 Travel (2015长春网赛)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题目大意是给一个n个城市(点)m条路线(边)的双向的路线图,每条路线有时间值(带权图),然后q个询问,每个 ...

  4. hdu 5441 (2015长春网络赛E题 带权并查集 )

    n个结点,m条边,权值是 从u到v所花的时间 ,每次询问会给一个时间,权值比 询问值小的边就可以走 从u到v 和从v到u算不同的两次 输出有多少种不同的走法(大概是这个意思吧)先把边的权值 从小到大排 ...

  5. hdu 4813(2013长春现场赛A题)

    把一个字符串分成N个字符串 每个字符串长度为m Sample Input12 5 // n mklmbbileay Sample Outputklmbbileay # include <iost ...

  6. HDU 5531 Rebuild

    2015 ACM/ICPC 长春现场赛 E题 三分. 如果节点个数是奇数,那么直接列方程可以求解,因为,如果第一个圆半径变大,必然导致最后一个圆的半径变大, 所以,节点是奇数的时候,要么无解,要么只有 ...

  7. HDU 4816 Bathysphere (2013长春现场赛D题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 2013长春区域赛的D题. 很简单的几何题,就是给了一条折线. 然后一个矩形窗去截取一部分,求最 ...

  8. HDU 4764 Stone (2013长春网络赛,水博弈)

    Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. 2015长春 HDU 5531 Rebuild

    题意:n个顶点组成的多边形能否形成正多边形? #include <cstdio> #include <cstring> #include <cmath> #incl ...

随机推荐

  1. Android Dialogs(6)Dialog类使用示例:用系统theme和用自定义的theme

    使用dialog时有很多 方法,其中一个就是直接 使用基类Dialog,可用来作一个没有按钮的非模态提示框,它可以直接从系统的主题构造也可从自定义的主题构造. 基本步骤: a,构造 b,调用dialo ...

  2. Log4j具体使用实例

    首先,下载log4j.jar架包(网上很多,随便下载一个就可以了), 第一步:新建java项目,Testlog4j,再在src中建立com.Testlog4j包,再建一个testlog4j.java文 ...

  3. RAPIDXML 中文手册,根据官方文档完整翻译!

    简介:这个号称是最快的DOM模型XML分析器,在使用它之前我都是用TinyXML的,因为它小巧和容易上手,但真正在项目中使用时才发现如果分析一个比较大的XML时TinyXML还是表现一般,所以我们决定 ...

  4. "=="和equals方法的区别

    .==和equal .栈内存和对内存 单独把一个东西说清楚,然后再说清楚另一个,这样,它们的区别自然就出来了,混在一起说,则很难说清楚) ==操作符专门用来比较两个变量的值是否相等,也就是用于比较变量 ...

  5. UVa 1103 (利用连通块来判断字符) Ancient Messages

    本题就是灵活运用DFS来求连通块来求解的. 题意: 给出一幅黑白图像,每行相邻的四个点压缩成一个十六进制的字符.然后还有题中图示的6中古老的字符,按字母表顺序输出这些字符的标号. 分析: 首先图像是被 ...

  6. IIS6配置Asp.net MVC运行环境

    Windows server 2003 + IIS6 搭建Asp.net MVC运行环境 1.安装.Net Framework4.0. 下载地址: http://www.microsoft.com/z ...

  7. 自己遇到的Android虚拟机出现的错误及解决方法【不断更新】

    2012.11.9 第一个: [2012-11-09 13:15:14 - Tesa] Android Launch! [2012-11-09 13:15:14 - Tesa] The connect ...

  8. ios-cocos2d游戏开发基础-CCLayer和Touch事件-开发笔记

    有时候在同一个场景里你需要多个CCLayer.你可以参照以下代码生成这样的场景 +(id) scene { CCScene* scene = [CCScene node]; CCLayer* back ...

  9. acdream 1210 Chinese Girls' Amusement (打表找规律)

    题意:有n个女孩围成一个圈从第1号女孩开始有一个球,可以往编号大的抛去(像传绣球一样绕着环来传),每次必须抛给左边第k个人,比如1号会抛给1+k号女孩.给出女孩的人数,如果他们都每个人都想要碰到球一次 ...

  10. POJ 1146 ID Codes (UVA146)

    // 求下一个排列// 如果已经是最后一个排列// 就输出 No Successor// stl 或 自己写个 生成排列 我测试了下 两个速率是一样的.只是代码长度不同 /* #include < ...