CodeForces #1 C. Ancient Berland Circus(两种解法...
2016-07-17 16:18
417 查看
【题目大意】
给出一个正N多边形的三个点,问其最小可能面积是多少。【题解一】
首先给出CodeForce官方题解。在这,我也学习了。因为接下来的题解二代码我实在没有通过,虽然思路很清晰,但个别数据的gcd会计算崩溃。如果有人能帮我解决一下不胜感激。点可以正N多边形的顶点,当且仅当每一对点的极坐标角度之差(从多边形中心)是2的倍数*π/ N。所有的点都应该在圆中心多边形。我们可以找到多边形的中心/圆心(但我们要避免这样一个问题,即作为一个和弦(比如说,(x1,y1)-(x2,y2))在两次从中心更大的角度,而不是从其他角度的cricle(x3,y3)]。有很多方法可以定位圆心,我使用的方法是建立中点垂直段(x1,y1)-(x2,y2)和(x2,y2)-(x3,y3)形成y
= kx + b和发现他们的十字路口。公式y =kx+ b的缺点是,它不能使用如果线平行于y,可能的解决方案是所有点随机旋转角(使用公式x”= x * cos(a)-y * sin(a),y = y * cos(a)+ x * sin()),直到没有水平线。
中心的坐标是已知后,我们使用的量化函数atan2,它返回在右象限角:a[i]=atan2(y[i]-ycenter, x[i]-xcenter)
正多边形的面积随N的增加而增加,所以有可能只是在N遍历所有可能的值按升序,退出循环首先发现N是满足条件的。
如何满足条件?使用sin(x)是使它容易:sin(x)= 0,当x是2*π的倍数。因此,分属于常规正N多边形。sin( N * (a[i]-a[j]) /2 )=0。因为有限精度算法,fabs(
sin( N *(a[i]-a[j]) /2 ) ) < eps。
【题解一代码】
#ifdef O_O #include "a.h" #else #include <cstdio> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstring> #include <cstdlib> #include <cmath> #include <ctime> #endif #define PB push_back #define MP make_pair typedef __int64 LL; #define ALL(x)x.begin(),x.end() #define FOR(a,b)for(int a=0;a<b;a++) #define FORR(a,b,c)for(int a=b;a<=c;a++) #define FOR1(a,b)for(int a=1;a<=b;a++) #define CLR(a,b)memset(a,b,sizeof(a)) #define CPY(a,b)memcpy(a,b,sizeof(a)) #define tr(i,c)for(typeof((c).begin())i=(c).begin();i!=(c).end();i++) #define M_PI acos(-1.0) using namespace std; LL n,m,a; void rot(double &a,double &b,double cs,double sn) { double t = a*cs - b*sn; b = b*cs + a*sn; a = t; } int main() { double x1,x2,x3,y1,y2,y3; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; double ma,mb,p1,p2; do {<span style="white-space:pre"> </span>//旋转使之不水平 double f = rand()%311/100.0; double c = cos(f), s= sin(f); rot(x1,y1,c,s); rot(x2,y2,c,s); rot(x3,y3,c,s); if(y1==y2 || y2==y3) continue; ma = -(x2-x1)/(y2-y1); mb = -(x3-x2)/(y3-y2); if(ma==mb) continue; goto ok; } while(1); ok: p1 = (x1+x2)/2; p2 = (x3+x2)/2; double q1 = (y1+y2)/2, q2 = (y3+y2)/2; double xc = (-q1+q2 + p1*ma - p2*mb)/(ma-mb); double yc = ma*(xc-p1) + q1; double s1 = hypot(x1-x2,y1-y2), s2 = hypot(x2-x3,y2-y3), s3 = hypot(x3-x1,y3-y1);//边长 double r1 = hypot(x1-xc,y1-yc), r2 = hypot(x2-xc,y2-yc), r3 = hypot(x3-xc,y3-yc);//半径 double f1 = atan2(x1-xc,y1-yc), f2 = atan2(x2-xc,y2-yc), f3 = atan2(x3-xc,y3-yc);//角 double q = 180/M_PI; FORR(vv,3,100) { if(abs(sin( (f1-f3)*vv/2.0 ))>1e-4) continue; if(abs(sin( (f1-f2)*vv/2.0 ))>1e-4) continue; if(abs(sin( (f2-f3)*vv/2.0 ))>1e-4) continue; printf("%.8lf\n",vv * r1*r1 * 0.5 * sin(2*M_PI / vv)); return 0; } return 1; }
【题解二】
网上比较清晰地思路1.根据三点求3边a,b,c
2. 先求外接圆半径(一定存在)?
海伦公式 + 正弦定理 得
R = a * b * c / (4 * S) S = sqrt(q *
(q - a) * (q - b) * (q -c)); q = (a + b + c) / 2;
3. 求3个角?
余弦定理。三角求最大公约数就是 正多边形 每一份 最小的角度。
4. 求面积?
正弦定理。 S = R * R * sin( angle ) / 2 * N (N = 2 * pi / angle);
【题解二代码】
#include <stdio.h> #include <math.h> #define feq(a, b) (fabs((a) - (b)) < 1e-2) #define pi acos(-1.0) double GetR(const double d[]) { double q = (d[0] + d[1] + d[2]) / 2; double S = sqrt (q * (q - d[0]) * (q - d[1]) * (q - d[2])); return d[0] * d[1] * d[2] / (4 * S); } double fgcd(double a, double b){ if (feq(a, 0)) return b; if (feq(b, 0)) return a; return fgcd(b, fmod(a, b)); } int main() { double x[3], y[3], d[3]; for (int i = 0; i < 3; i++) scanf("%lf%lf", &x[i], &y[i]); for (int i = 0; i < 3; i++) d[i] = sqrt(pow((x[i] - x[(i + 1) % 3]), 2) + pow((y[i] - y[(i + 1) % 3]), 2)); double R = GetR(d); double angle[4]; for (int i = 0; i < 2; i++) angle[i] = acos(1 - d[i] * d[i] / (2 * R * R)); angle[2] = 2 * pi - angle[0] - angle[1]; angle[3] = fgcd(angle[0], fgcd(angle[1], angle[2])); printf("%.8lf\n", R * R * sin(angle[3]) / 2 * (2 * pi / angle[3]) ); }Notice:
CF测试给出下一组数据无法通过测试。具体我也不清楚怎么回事....
31.312532 151.532355 182.646053 56.534075 15.953947 127.065925
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C++的template模板中class与typename关键字的区别分析
- C与C++之间相互调用实例方法讲解