hdu 4033Regular Polygon(二分+余弦定理)

Regular Polygon

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 3274    Accepted Submission(s): 996

Problem Description

In a 2_D plane, there is a point strictly in a regular polygon with N sides. If you are given the distances between it and N vertexes of the regular polygon, can you calculate the length of reguler polygon's side? The distance is defined as dist(A, B) = sqrt( (Ax-Bx)*(Ax-Bx) + (Ay-By)*(Ay-By) ). And the distances are given counterclockwise.

Input

First a integer T (T≤ 50), indicates the number of test cases. Every test case begins with a integer N (3 ≤ N ≤ 100), which is the number of regular polygon's sides. In the second line are N float numbers, indicate the distance between the point and N vertexes of the regular polygon. All the distances are between (0, 10000), not inclusive.

Output

For the ith case, output one line “Case k: ” at first. Then for every test case, if there is such a regular polygon exist, output the side's length rounded to three digits after the decimal point, otherwise output “impossible”.

Sample Input

2 3 3.0 4.0 5.0 3 1.0 2.0 3.0

Sample Output

Case 1: 6.766 Case 2: impossible

Source

The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest

  已知一个点到正n边形的n个顶点的距离,求正n边形的边长。

思路:

       在已知的表达式中,求不出n边形的边长。但是依据两边之和大于第三边,两边之差小鱼第三边。可以得到这个边的范围.

   然后由于n边形的以任意一个点,连接到所有顶点,所有的夹角之和为360,所以只需要采取二分依次来判断,是否满足。

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #define pi acos(-1.0)
 6 #define esp 1e-8
 7 using namespace std;
 8 double aa[105];
 9 int main()
10 {
11   int cas,n;
12   double rr,ll;
13   scanf("%d",&cas);
14   for(int i=1;i<=cas;i++)
15   {
16       scanf("%d",&n);
17       for(int j=0;j<n;j++)
18         scanf("%lf",aa+j);
19     //确定上下边界
20      ll=20001,rr=0;
21     for(int j=0;j<n;j++)
22     {
23       rr=max(rr,aa[j]+aa[(j+1)%n]);
24       ll=min(ll,fabs(aa[j]-aa[(j+1)%n]));
25     }
26     double mid,sum,cosa;
27     printf("Case %d: ",i);
28     bool tag=0;
29     while(rr>esp+ll)
30     {
31       mid=ll+(rr-ll)/2;
32       sum=0;
33       for(int j=0;j<n;j++){
34           //oosr=a*a+b*b-mid*mid; 余弦定理求夹角,然后判断所有的夹角之和是否为360
35           cosa=(aa[j]*aa[j]+aa[(j+1)%n]*aa[(j+1)%n]-mid*mid)/(2.0*aa[j]*aa[(j+1)%n]);
36         sum+=acos(cosa);
37       }
38        if(fabs(sum-2*pi)<esp){
39               tag=1;
40            printf("%.3lf\n",mid);
41            break;
42        }
43        else
44          if(sum<2*pi)  ll=mid;
45          else
46              rr=mid;
47     }
48     if(tag==0)
49         printf("impossible\n");
50   }
51   return 0;
52 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏小樱的经验随笔

HDU 2502 月之数(二进制,规律)

月之数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/...

34740
来自专栏算法修养

HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))

Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submission...

30640
来自专栏ml

hdu---1024Max Sum Plus Plus(动态规划)

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/...

330100
来自专栏ml

hdu----(3068)最长回文(manacher)

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav...

30350
来自专栏小樱的经验随笔

BZOJ 3097: Hash Killer I【构造题,思维题】

3097: Hash Killer I Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special Judge Su...

21960
来自专栏ml

HDUOJ------Lovekey

Lovekey Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J...

29790
来自专栏数据结构与算法

POJ 2891 Strange Way to Express Integers

Description Elina is reading a book written by Rujia Liu, which introduces a ...

34870
来自专栏码匠的流水账

聊聊storm的reportError

storm-2.0.0/storm-client/src/jvm/org/apache/storm/task/IErrorReporter.java

16430
来自专栏ml

poj-------Common Subsequence(poj 1458)

Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submiss...

30450
来自专栏算法修养

PAT 1012 The Best Rank

1012. The Best Rank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B ...

31940

扫码关注云+社区

领取腾讯云代金券