UVA - 11277 Cyclic Polygons(二分)

题意:已知圆的内接多边形的各个边长,求多边形的面积。

分析:

1、因为是圆的内接多边形,将多边形的每个顶点与圆心相连,多边形的面积就等于被分隔成的各三角形之和。

2、根据海伦公式,任意一个三角形的面积为:double p = (2 * r + a[i]) / 2,S = sqrt(p * (p - r) * (p - r) * (p - a[i])),a[i]为多边形某条边的长度,由此可以表示出多边形的面积。

3、对于任意一个三角形,设其为半径的两条边的夹角为α,则sin(α/2) = (a[i] / 2) / r,所以α = 2 * asin(a[i] / 2 / r)。

4、注意asin()函数的计算结果是弧度值,所以所有三角形的夹角和应与2比较大小。

5、二分确定半径大小,并通过夹角和验证。

(1)二分设置一个上限

(2)judge()函数判断对于每一个三角形是否符合两边之和大于第三边,如果2 * r小于或等于a[i],说明半径过小,所以应当l = mid + eps。

(3)如果judge()函数成立,将所有三角形的夹角和与2比较,若小于,说明半径过长,因此r = mid - eps;若大于,说明半径过短,因此l = mid + eps;若相等,则符合要求。

6、注意浮点数比较大小。

7、若n<=2,则不构成多边形,输出0.000。

8、若最长边大于或等于其他所有边之和,则构不成多边形,输出0.000,即ma 大于或等于 sum - ma。

9、因为计算过程中会损失精度,结果最好加上eps。

10、本题虽然结果保留小数点后三位,但是为保证精度,eps设置为1e-15,而半径的查找上限设置为1e15。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-15;
const int MAXN = 50 + 10;
const int MAXT = 10000 + 10;
using namespace std;
double a[MAXN];
int n;
int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a < b ? -1 : 1;
}
bool judge(double r){
    for(int i = 0; i < n; ++i){
        if(dcmp(2 * r, a[i]) != 1) return false;
    }
    return true;
}
int Equal(double r){
    double ans = 0;
    for(int i = 0; i < n; ++i){
        ans += 2 * asin(a[i] / 2 / r);
    }
    return dcmp(ans, 2 * pi);
}
double get_R(double l, double r){
    int tmp = 100;
    while(tmp--){
        double mid = l + (r - l) / 2;
        if(!judge(mid)) l = mid + eps;
        else{
            int w = Equal(mid);
            if(w == 1) l = mid + eps;
            else if(w == -1) r = mid - eps;
            else return mid;
        }
    }
    return l;
}
double solve(){
    double r = get_R(0, 1e15);
    double sum = 0.0;
    for(int i = 0; i < n; ++i){
        double p = (2 * r + a[i]) / 2;
        sum += sqrt(p * (p - r) * (p - r) * (p - a[i]));
    }
    return sum;
}
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        double ma = 0;
        double sum = 0;
        for(int i = 0; i < n; ++i){
            scanf("%lf", &a[i]);
            sum += a[i];
            if(dcmp(a[i], ma) == 1) ma = a[i];
        }
        if(n <= 2 || dcmp(2 * ma, sum) != -1){
            printf("0.000\n");
            continue;
        }
        printf("%.3lf\n", solve() + eps);
    }
    return 0;
}
时间: 2024-08-08 01:12:12

UVA - 11277 Cyclic Polygons(二分)的相关文章

uva 1356 - Bridge(积分+二分)

题目链接:uva 1356 - Bridge 题目大意:在一座长度为B的桥上建若干个塔,塔的间距不能超过D,塔的高度为H,塔之间的绳索形成全等的抛物线.绳索的总长度为L.问在建最少塔的情况下,绳索的最下段离地面的高度. 解题思路:贪心的思想求出最少情况下建立的塔数. 二分高度,然后用积分求出两塔之间绳索的长度. C++ 积分 #include <cstdio> #include <cstring> #include <cmath> #include <algori

UVA 12124 - Assemble(二分)

题目链接:点击打开链接 题意: 给n个组件来组装电脑, 每个组件有4个属性:种类.名称.价格.品质. 要求每种组件买一个, 求在不超过预算的情况下, 品质最低的品质尽量大. 思路:很显然, 二分最低品质, 然后判断是否可行, 属于二分找上界,在二分时有一个小技巧用来处理当区间相差1时的情况. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #i

UVA - 12338 Anti-Rhyme Pairs 二分+hash

题目链接:https://vjudge.net/problem/UVA-12338 题意: 给你n个串 问你任意两个串的最大公共前缀长度是多少 题解: 二分+hash 思路很明显,我最近用来写hash 很鸡肋 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define ls i<<1 #define rs ls

UVA 1149 Bin Packing 二分+贪心

A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samelength l and each item i has length li ≤ l. We look for a minimal number of bins q such that• each bin contains at most 2 items,• each item is packed in

UVa 1644 (筛素数 + 二分) Prime Gap

题意: 给出一个整数n,如果n是素数输出0,否则输出它后一个素数与前一个素数的差值. 分析: 首先用筛法把前十万个素数都筛出来,然后放到数组里.用二分找到不大于n的最大的素数的下标,如果这个素数等于n,则直接输出0,否则输出它后一个素数与它本身的差值. 1 #include <cstdio> 2 #include <cmath> 3 4 const int maxn = 1299709; 5 const int maxp = 100000; 6 bool vis[maxn + 10

UVA 12338 哈希+二分最长前缀

每个字符串都哈希之后,二个之间二分出最长前缀! 需要注意的是不能用数组存,会暴,用vector就行了. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<queue&g

UVa 714 - Copying Books 二分答案

题目链接:714 - Copying Books 解题思路 具体处理方法见代码 /************************************************************** Problem: User: youmi Language: C++ Result: Accepted Time: Memory: ****************************************************************/ //#pragma comm

UVA 11478(差分约束 + 二分)

题意: 给定一个有向图,每条边都有一个权值,每次你可以选择一个结点和一个整数的,把所有以v为终点的边的权值减去d, 把所有以v为起点的边的权值加上d 最后要让所有边的权的最小值非负且尽量大 代码 #include<stdio.h> #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<queue> using namespa

UVA - 1616 Caravan Robbers 二分+暴力转换

题目大意:给出n条线段,把每条线段变成原来线段的一条子线段,使得改变后的所有线段等长且不相交,输出最大长度 解题思路:这题要用long double确保精度,精度要求很高,接下来就是将这个long double的数转化为两个整数相除的结果了,有两种方法,学习到了 #include<cstdio> #include<algorithm> #include<cmath> using namespace std; #define maxn 100010 #define esp