Задача "Другая реальность" из 90 раунда.
Написал код на c++, проверил на тестах - всё нормально. Отправил на проверку - оказалось, что компилатор сервера не выполняет что-то (видимо, какой-то член функцию) так, как оно должно быть.
Есть подозрения, что дело в том, что я не делаю проверку на меньше епсилон, но ведь это же не обязательно, или я что-то путаю?
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
using namespace std;
#define eps 1e-9
class vec /// vectors, coefficients of the planes, complaining polus, points.
{
public:
double a, b, c;
vec() {}
vec(double x, double y, double z){a = x; b = y; c = z;}
double abs()
{
return a*a + b*b + c*c;
}
double dist(const vec &r)
{
return sqrt((r.a-a)*(r.a-a) + (r.b-b)*(r.b-b) + (r.c-c)*(r.c-c));
}
vec &operator = (const vec &r) {a = r.a; b = r.b; c = r.c;}
friend vec &operator * (const vec &v, double x);
friend vec &operator / (const vec &v, double x) ;
friend vec &operator + (const vec &v, const vec &r);
};
vec &operator * (const vec &v, double x) {vec r = vec(v.a*x, v.b*x, v.c*x); return r;}
vec &operator / (const vec &v, double x) {vec r = vec(v.a/x, v.b/x, v.c/x); return r;}
vec &operator + (const vec &v, const vec &r)
{
vec sum = vec(v.a + r.a, v.b + r.b, v.c + r.c); return sum;
}
typedef vector <vec> vp;
///-------------------------------------------------------------
double operator * (const vec &v1, const vec &v2)
{
return v1.a*v2.a + v1.b*v2.b + v1.c*v2.c;
}
///-------------------------------------------------------------
std::istream &operator >> (istream &in, vec &r)
{
in >> r.a; in >> r.b; in >> r.c;
}
///-------------------------------------------------------------
std::ostream &operator << (ostream &out, vec &r)
{
out << r.a << " " << r.b << " " << r.c << endl;
}
///-------------------------------------------------------------
vec proectionAtPlane(vec &plane, const vec &point)
{
/**
Ax + By + Cz = 0 & x = x_0 + t * A & y = y_0 + t * B & z = z_0 + t * C
{(x_0, y_0, z_0) - point to be proected,
(x, y, z) - proection of point}
=> A(x_0 + t * A) + B(y_0 + t * B) + C(z_0 + t * C) = 0
=> t = -plane(point)/(n^2)
**/
vec prP, pl, po;
double t;
pl = plane;
po = point;
t = -(plane * point)/(plane.abs());
prP = point + plane * t;
#include <fstream>
#include <cmath>
#include <vector>
using namespace std;
#define eps 1e-9
class vec /// vectors, coefficients of the planes, complaining polus, points.
{
public:
double a, b, c;
vec() {}
vec(double x, double y, double z){a = x; b = y; c = z;}
double abs()
{
return a*a + b*b + c*c;
}
double dist(const vec &r)
{
return sqrt((r.a-a)*(r.a-a) + (r.b-b)*(r.b-b) + (r.c-c)*(r.c-c));
}
vec &operator = (const vec &r) {a = r.a; b = r.b; c = r.c;}
friend vec &operator * (const vec &v, double x);
friend vec &operator / (const vec &v, double x) ;
friend vec &operator + (const vec &v, const vec &r);
};
vec &operator * (const vec &v, double x) {vec r = vec(v.a*x, v.b*x, v.c*x); return r;}
vec &operator / (const vec &v, double x) {vec r = vec(v.a/x, v.b/x, v.c/x); return r;}
vec &operator + (const vec &v, const vec &r)
{
vec sum = vec(v.a + r.a, v.b + r.b, v.c + r.c); return sum;
}
typedef vector <vec> vp;
///-------------------------------------------------------------
double operator * (const vec &v1, const vec &v2)
{
return v1.a*v2.a + v1.b*v2.b + v1.c*v2.c;
}
///-------------------------------------------------------------
std::istream &operator >> (istream &in, vec &r)
{
in >> r.a; in >> r.b; in >> r.c;
}
///-------------------------------------------------------------
std::ostream &operator << (ostream &out, vec &r)
{
out << r.a << " " << r.b << " " << r.c << endl;
}
///-------------------------------------------------------------
vec proectionAtPlane(vec &plane, const vec &point)
{
/**
Ax + By + Cz = 0 & x = x_0 + t * A & y = y_0 + t * B & z = z_0 + t * C
{(x_0, y_0, z_0) - point to be proected,
(x, y, z) - proection of point}
=> A(x_0 + t * A) + B(y_0 + t * B) + C(z_0 + t * C) = 0
=> t = -plane(point)/(n^2)
**/
vec prP, pl, po;
double t;
pl = plane;
po = point;
t = -(plane * point)/(plane.abs());
prP = point + plane * t;
return prP;
}
///-------------------------------------------------------------
vp planes, points, pointsInPlane;
int n, m;
///-------------------------------------------------------------
void get(vp &g, int count)
{
for(int i = 0; i < count; ++i)
{
cin >> g[i];
}
}
double payment(int num)
{
int i;
double r, max = -1;
vec centr(0, 0, 0);
for(i = 0; i < n; ++i)
pointsInPlane[i] = proectionAtPlane(planes[num], points[i]);
for(i = 0; i < n; ++i)
centr = centr + pointsInPlane[i];
centr = centr / n;
for(i = 0; i < n; ++i)
{
r = centr.dist(pointsInPlane[i]);
if(r > max)
{
max = r;
}
}
return max;
}
int main()
{
setlocale(LC_ALL, "Russian");
cin >> n >> m;
planes.reserve(m);
points.reserve(n);
pointsInPlane.reserve(n);
get(points, n);
get(planes, m);
cout.precision(8);
for(int i = 0; i < m; ++i)
{
cout << payment(i) << endl;
}
return 0;
}
}
///-------------------------------------------------------------
vp planes, points, pointsInPlane;
int n, m;
///-------------------------------------------------------------
void get(vp &g, int count)
{
for(int i = 0; i < count; ++i)
{
cin >> g[i];
}
}
double payment(int num)
{
int i;
double r, max = -1;
vec centr(0, 0, 0);
for(i = 0; i < n; ++i)
pointsInPlane[i] = proectionAtPlane(planes[num], points[i]);
for(i = 0; i < n; ++i)
centr = centr + pointsInPlane[i];
centr = centr / n;
for(i = 0; i < n; ++i)
{
r = centr.dist(pointsInPlane[i]);
if(r > max)
{
max = r;
}
}
return max;
}
int main()
{
setlocale(LC_ALL, "Russian");
cin >> n >> m;
planes.reserve(m);
points.reserve(n);
pointsInPlane.reserve(n);
get(points, n);
get(planes, m);
cout.precision(8);
for(int i = 0; i < m; ++i)
{
cout << payment(i) << endl;
}
return 0;
}
Компилятор у меня GNU gcc