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

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

题目链接:hdu 4033 Regular Polygon

题目大意

已知正多边形中的一个内点到所有顶点的距离,求多边形的边长。

二分问题一般都存在含有一个未知量的方程(等式关系),通过二分未知量的范围实现查找,这道题目的几何关系就是:知道一边,内角和(围着内点)等于360度。根据三角不等式确定边的二分范围,二分查找边使得内角之和为2π即可。


说一下思路

笔者根据第一条边和最后一条边确定二分边的范围,边确定由余弦定理能确定所有内角,二分查找使得内角之和==2π,注意浮点数误差,用三态函数dcmp判等即可。


参考代码

/*====================================*|*        二分  +   计算几何          *|
\*====================================*/
/*Author:Hacker_vision*/
#include<bits/stdc++.h>
#define clr(k,v) memset(k,v,sizeof(k))
#define eps 1e-8
using namespace  std;

const int _max = 1e3 + 10;
const double PI = acos(-1);
int n;
double a[_max];
int dcmp(double x){//三态函数,避免精度误差
  if(fabs(x)<eps) return 0;else return x<0?-1:1;
}

double angle(double x){//根据余弦定理和正多边形n条边相等的特性,返回内角和
  double ans = 0;
  a[n] = a[0];
  for(int i = 0;i < n;++ i)
    ans += acos((a[i]*a[i]+a[i+1]*a[i+1]-x*x)/(2*a[i]*a[i+1]));
  return ans;
}

double bsearch(double L,double R){//二分查找边长
  double m;
  while(R - L >= eps){
    m = (L + R) / 2.0;//不能用位运算了QAQ
    if(dcmp(angle(m)-2*PI)==0) return m;//一条边一旦确定,内角和也就确定下来了
    if(angle(m) < 2*PI) L = m + eps;
    else R = m;
  }
  return -1;
}

int main(){
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  #endif // ONLINE_JUDGE
  int T;cin>>T;int cnt=1;
  while(T--){
    scanf("%d",&n);
    for(int i = 0;i < n; ++ i)
        scanf("%lf",a+i);
    double res = bsearch(fabs(a[0]-a[n-1]),a[0]+a[n-1]);
    printf("Case %d: ",cnt++);
    if(dcmp(res+1)==0) puts("impossible");
    else printf("%.3f\n",res);
  }
  return 0;
}

  • 加粗 Ctrl + B
  • 斜体 Ctrl + I
  • 引用 Ctrl + Q
  • 插入链接 Ctrl + L
  • 插入代码 Ctrl + K
  • 插入图片 Ctrl + G
  • 提升标题 Ctrl + H
  • 有序列表 Ctrl + O
  • 无序列表 Ctrl + U
  • 横线 Ctrl + R
  • 撤销 Ctrl + Z
  • 重做 Ctrl + Y

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 22:23:25

【二分+计算几何】hdu 4033 Regular Polygon的相关文章

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

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

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

【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

HDU 6055 Regular polygon (暴力)

题意,二维平面上给N个整数点,问能构成多少个不同的正多边形. 析:容易得知只有正四边形可以使得所有的顶点为整数点.所以只要枚举两个点,然后去查找另外两个点就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #inclu

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

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

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+(

Codeforces 8D Two Friends 三分+二分+计算几何

题目链接:点击打开链接 题意:点击打开链接 三分house到shop的距离,二分这条斜边到cinema的距离 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<set> #include<queue> #include<vector> #include<

hdu 4033 2011成都赛区网络赛 余弦定理+二分 **

二分边长,判断最后内角和是否为2pi,注意l与r的选取,保证能组成三角形 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 10000