HDU4033:Regular Polygon(二分+余弦定理)

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

题意:知道正多边形内一个点到各个顶点的距离,求出这个多边形的边长

思路:首先,可以将这个点看做圆心,那么正多边形就是这个圆的内接正多边形,通过这个点与各个顶点的连线,形成n个三角形,其角度之和为360,可以用余弦定理去求
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define pi acos(-1.0)
#define exp 1e-8
int main()
{
    int t,n,i,j,flag,cas = 1;
    double a[105];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        flag = 0;
        for(i = 0; i<n; i++)
            scanf("%lf",&a[i]);
        a[n] = a[0];
        double l = 0,r = 20000,mid;
        for(i = 1; i<=n; i++)
        {
            r = min(r,a[i]+a[i-1]);
            l = max(l,fabs(a[i]-a[i-1]));
        }
        while(r-l>exp)
        {
            mid = (l+r)/2;
            double s,sum=0;
            for(i = 1; i<=n; i++)
            {
                s = (a[i]*a[i]+a[i-1]*a[i-1]-mid*mid)/(2.0*a[i]*a[i-1]);
                sum+=acos(s);
            }
            if(fabs(sum-2*pi)<exp)
            {
                flag = 1;
                break;
            }
            else if(sum>2*pi)
                r = mid;
            else
                l = mid;
        }
        printf("Case %d: ",cas++);
        if(flag)
            printf("%.3f\n",mid);
        else
            printf("impossible\n");
    }

    return 0;
}

HDU4033:Regular Polygon(二分+余弦定理)

时间: 2024-10-08 02:11:10

HDU4033:Regular Polygon(二分+余弦定理)的相关文章

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 side

【二分+计算几何】hdu 4033 Regular Polygon

[二分+计算几何]hdu 4033 Regular Polygon 题目链接:hdu 4033 Regular Polygon 题目大意 已知正多边形中的一个内点到所有顶点的距离,求多边形的边长. 二分问题一般都存在含有一个未知量的方程(等式关系),通过二分未知量的范围实现查找,这道题目的几何关系就是:知道一边,内角和(围着内点)等于360度.根据三角不等式确定边的二分范围,二分查找边使得内角之和为2π即可. 说一下思路 笔者根据第一条边和最后一条边确定二分边的范围,边确定由余弦定理能确定所有内

HDU 6055 - Regular polygon

/* HDU 6055 - Regular polygon [ 分析,枚举 ] 题意: 给出 x,y 都在 [-100, +100] 范围内的 N 个整点,问组成的正多边形的数目是多少 N <= 500 分析: 分析可知,整点组成的正多边形只能是正方形 故枚举两个点,验证剩下两个点的位置 坑点: 由于点的范围是 [-100, +100],故经过计算得出的点的范围可能是 [-300,+300],注意越界 编码时长:46分钟(-1) */ #include <bits/stdc++.h> u

hdu6055 Regular polygon 脑洞几何 给定n个坐标(x,y)。x,y都是整数,求有多少个正多边形。因为点都是整数点,所以只可能是正四边形。

/** 题目:hdu6055 Regular polygon 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6055 题意:给定n个坐标(x,y).x,y都是整数,求有多少个正多边形.因为点都是整数点,所以只可能是正四边形. 思路: (x1,y2)(x2,y2)=>(x,y) = (x2-x1,y2-y1) 向量(x,y)逆时针旋转90度:(-y,x):那么可以得到垂直(x,y)的向量,并通过(x2,y2)获得以(x2,y2)为起点的向量终点(x2+(

HDU 6055 17多校 Regular polygon(计算几何)

Problem Description On a two-dimensional plane, give you n integer points. Your task is to figure out how many different regular polygon these points can make. Input The input file consists of several test cases. Each case the first line is a numbers

hdu 4033 Regular Polygon 计算几何 二分+余弦定理

题目链接 给一个n个顶点的正多边形, 给出多边形内部一个点到n个顶点的距离, 让你求出这个多边形的边长. 二分边长, 然后用余弦定理求出给出的相邻的两个边之间的夹角, 看所有的加起来是不是2Pi. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #includ

【2017多校训练2+计算几何+板】HDU 6055 Regular polygon

http://acm.hdu.edu.cn/showproblem.php?pid=6055 [题意] 给定n个格点,问有多少个正多边形 [思路] 因为是格点,只可能是正方形 枚举正方形的对角线,因为有两条对角线,最后答案要/2 也可以枚举正方形的边,因为有四条边,答案要/4 看当前对角线确定的正方形是否存在,用几何知识求出目标点的坐标,然后二分查找目标点是否存在 [Accepted] 1 #include <cstdio> 2 #include <cstring> 3 #incl

Codeforces gym102222 B.Rolling The Polygon 凸包/余弦定理

题意: 有一个不保证凸的多边形,让你滚一圈,计算某点滚出的轨迹多长. 题解: 求出凸包后,以每个点为转轴,转轴到定点的距离为半径,用余弦定理计算圆心角,计算弧长. #include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; int main() { int t,case1=0; cin>>t; while(t--) { cas

UVA12300-Smallest Regular Polygon

给出两点,求经过这两点的正n边形的最小面积 大白鼠上说要注意精度,我没觉得精度有什么影响,然后就过了 我的做法: 相当于这两点构成的线段是正n边形的最长弦 我的代码: #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include&l