[CSP-S模拟测试]:Walker(数学)

题目传送门(内部题86)


输入格式

第一行$n$
接下来$n$行,每行四个浮点数,分别表示变换前的坐标和变换后的坐标


输出格式

第一行浮点数$\theta$以弧度制表示
第二行浮点数$scale$
第三行两个浮点数$d_x,d_y$
我们将用$SPJ$以$10^{-3}$的绝对误差来判断变换结果是否正确,建议输出$10$位小数以上。


样例

样例输入1:

5
0 0 -1 1
0 1 -2 1
1 0 -1 2
1 1 0 0
2 1 1 0

样例输出1:

1.5707963268
1
-1 1

样例输入2:

5
0 0 1 1
0 1 1 2
1 0 2 1
1 1 0 0
2 1 1 0

样例输出2:

0
1
1 1


数据范围与提示

对于$30\%$的数据,保证存在可行的答案$\theta=0,scale=1$
对于另$30\%$的数据,$n\leqslant 500$
对于所有数据$n\leqslant 100,000$,坐标属于$[-10^9,10^9]$


题解

正解是随机化额……

不过还是有正确性的。

如果我们知道两组$(x_i,y_i)$,那么就可以解出$\theta,scale,d_x,d_y$了,然后将解出来的这四个值带入检验即可。

因为至少有一半是正确的,所以每次可以解出的概率就是$\frac{1}{4}$,解不出来的概率就是$\frac{3}{4}$,那么解$k$次,解不出来的概率就是$\frac{1}{4}^k$,$k=50$时概率已经$<10^{-5}$了。

对于官方题解里说的高斯消元,因为就两组,没有必要。

实在不行多交几遍(其实我就脸黑了……)

时间复杂度:$\Theta($玄学$)$。

期望得分:$100$分。

实际得分:$0\sim 100$分(脸黑别怪我)


代码时刻

#include<bits/stdc++.h>
using namespace std;
const double eps=1e-5;
struct rec{double x0,y0,x2,y2;}e[100001];
pair<double,double> d[100001];
int n;
double dx,dy,Cos,Sin,scale,theta;
void solve(int x,int y)
{
	double d1=e[x].x0-e[y].x0;
	double d2=e[x].y0-e[y].y0;
	double d3=e[x].x2-e[y].x2;
	double d4=e[x].y2-e[y].y2;
	double COS=(d1*d3+d2*d4)/(d1*d1+d2*d2);
	double SIN=(d4-d2*COS)/d1;
	theta=atan(SIN/COS);
	dx=e[x].x2-e[x].x0*COS+e[x].y0*SIN;
	dy=e[x].y2-e[x].x0*SIN-e[x].y0*COS;
	Cos=cos(theta);
	Sin=sin(theta);
	scale=SIN/Sin;
	if(scale<0.0)
	{
		theta+=M_PI;
		Cos=cos(theta);
		Sin=sin(theta);
		scale=SIN/Sin;
	}
}
bool judge()
{
	int sum=0;
	for(int i=1;i<=n;i++)
	{
		double nowx=e[i].x0*Cos-e[i].y0*Sin;
		double nowy=e[i].x0*Sin+e[i].y0*Cos;
		nowx=nowx*scale+dx;nowy=nowy*scale+dy;
		if(fabs(nowx-e[i].x2)<eps&&abs(nowy-e[i].y2)<eps)sum++;
	}
	return (2*sum>=n);
}
int main()
{
	srand(time(NULL));
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%lf%lf%lf%lf",&e[i].x0,&e[i].y0,&e[i].x2,&e[i].y2);
		d[i]=make_pair(e[i].x2-e[i].x0,e[i].y2-e[i].y0);
	}
	sort(d+1,d+n+1);
	int sum=0;
	for(int i=1;i<=n;i++)
	{
		if(fabs(d[i].first-d[i-1].first)>eps||fabs(d[i].second-d[i-1].second)>eps)sum=0;
		sum++;
		if(2*sum>=n)
		{
			puts("0\n1");
			printf("%.10lf %.10lf",d[i].first,d[i].second);
			return 0;
		}
	}
	while(1)
	{
		int x,y;
		do
		{
			x=rand()%n+1;
			y=rand()%n+1;
		}while(x==y);
		solve(x,y);
		if(judge())
		{
			printf("%.10lf\n%.10lf\n%.10lf %.10lf",theta,scale,dx,dy);
			return 0;
		}
	}
	return 0;
}

rp++

猜你喜欢

转载自www.cnblogs.com/wzc521/p/11736969.html