Pagini recente » Cod sursa (job #2128835) | Cod sursa (job #389004) | Cod sursa (job #1469287) | Cod sursa (job #211358) | Cod sursa (job #1069094)
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<fstream>
#include<iomanip>
using namespace std;
const int MAX_N=50000+5;
const double MAX_PAS=800.0;
const double EPS=0.0000001;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
class PointXY {
public:
PointXY() { x = y = 0.0; }
PointXY(double xx, double yy) { x = xx; y = yy; }
inline double operator*(const PointXY &other) const {
return sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
}
inline void set(const PointXY &other, const double &delta_x, const double &delta_y) {
x = other.x + delta_x;
y = other.y + delta_y;
}
inline void read(ifstream &fin) {
fin >> x >> y;
}
inline void write(ofstream &fout) {
fout << setprecision(4) << fixed;
fout << x << " " << y << endl;
}
private:
double x,y;
};
int n;
PointXY points[MAX_N];
double cost(const PointXY &point) {
double answer=0.0;
for(int i = 1; i <= n; ++i) {
answer += point * points[i];
}
return answer;
}
int main() {
ifstream fin("adapost2.in");
ofstream fout("adapost2.out");
fin >> n;
for(int i = 1; i <= n; ++i) {
points[i].read(fin);
}
PointXY answer(0,0),next_answer;
double current_best_cost = cost(answer), current_cost;
for(double pas = MAX_PAS; pas >= EPS; pas *= 0.5) {
for(int d = 0; d < 4; ++d) {
next_answer.set(answer,pas * dx[d], pas * dy[d]);
current_cost=cost(next_answer);
if(current_cost < current_best_cost) {
current_best_cost = current_cost;
answer = next_answer;
}
}
}
answer.write(fout);
return 0;
}