题目大意就是求n边形最小外接正方形
被卡了好久:听说是结论题:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<cmath>
#include<map>
#include<string>
#include<queue>
#include<functional>
using namespace std;
int n,t;
int a,b,c,d;double sum=0;
#define pi acos(-1.0)
int main()
{
scanf("%d",&n);
while(n--)
{
scanf("%d",&a);
sum=1/sin(pi/(2*a))*cos(pi/(4*a));
printf("%.7lf\n",sum);
}
}
下面来看大佬的二分解法
我们知道正方形最大是L多边形内部半径。我们可以二分一下这个正方形可以缩小最大是多少
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <deque>
#include <cmath>
#include <iomanip>
#include <cctype>
#define endl '\n'
#define IOS std::ios::sync_with_stdio(0);
#define FILE freopen("..//data_generator//in.txt","r",stdin),freopen("res.txt","w",stdout)
#define FI freopen("..//data_generator//in.txt","r",stdin)
#define FO freopen("res.txt","w",stdout)
#define md make_pair
#define pb push_back
#define mp make_pair
#define seteps(N) fixed << setprecision(N)
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
inline ll qmul(ll a, ll b, ll m) {
ll res = 0;
while(b) {
if(b & 1) res = (res + a) % m;
a = (a << 1) % m;
b = b >> 1;
}
return res;
}
inline ll qpow(ll a, ll b, ll m) {
ll res = 1;
while(b) {
if(b & 1) res = (res * a) % m;
a = (a * a) % m;
b = b >> 1;
}
return res;
}
inline ll inv(ll x, ll q) {
return qpow(x, q - 2, q);
}
using namespace std;
/*-----------------------------------------------------------------*/
#define INF 0x3f3f3f3f
const int N = 3e6 + 10;
const double eps = 1e-10;
char s[N];
const long double PI = 3.14159265358979323846;
int main() {
IOS;
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
long double o = PI / n;
long double L = (long double)0.5 / sin(o / 2);
//求出多边形的内部半径
long double l = 0, r = L - cos(o / 2) * L;
//二分正方形可以缩短的长度最多是delta
//通过画图我们可以发现一定会有一个点落在差不多就是这两条边放得下,对应的多边形也就放得下了
while(r - l > eps) {
long double deL = (l + r) / 2;
long double deo = acos((L - deL) / L);//返回弧度制
if(L * cos(o / 2 - deo) <= L - deL) l = deL;//你就求出与这点相邻的边的另一个点是否在正方形。
else r = deL;
}
cout << seteps(10) << 2 * (L - l) << endl;
}
}