Pagini recente » Cod sursa (job #1745726) | Cod sursa (job #1553702) | Cod sursa (job #2113525) | Cod sursa (job #1801646) | Cod sursa (job #1846014)
#include <iostream>
#include <cmath>
#include <map>
using namespace std;
const int Q=50007;
int n;
struct point
{
double x,y;
}v[Q];
std::map<double, map<double,double> > prec;
double dist(double a, double b)
{
if(prec[a][b]!=0)
return prec[a][b];
double rez=0;
for(int i=1; i<=n; i++)
{
rez+=sqrt((a-v[i].x)*(a-v[i].x) + (b-v[i].y)*(b-v[i].y));
}
prec[a][b]=rez;
return rez;
}
double aux,dist_act,gx,gy,pas;
void bra1()
{
int sgn=rand()&1;
sgn*=2;
sgn--;
aux=dist(gx,gy+sgn*pas);
if(aux<dist_act)
{
gy+=sgn*pas;
dist_act=aux;
}
else
{
aux=dist(gx,gy-sgn*pas);
if(aux<dist_act)
{
gy-=sgn*pas;
dist_act=aux;
}
}
}
void bra2()
{
int sgn=rand()&1;
sgn*=2;
sgn--;
aux=dist(gx+pas*sgn,gy);
if(aux<dist_act)
{
gx+=pas*sgn;
dist_act=aux;
}
else
{
aux=dist(gx-sgn*pas,gy);
if(aux<dist_act)
{
gx-=sgn*pas;
dist_act=aux;
}
}
}
int main()
{
freopen("adapost2.in","r",stdin);
freopen("adapost2.out","w",stdout);
cin>>n;
gx=0,gy=0;
for(int i=1; i<=n; i++)
{
cin>>v[i].x>>v[i].y;
gx+=v[i].x;
gy+=v[i].y;
}
gx/=n;
gy/=n;
pas=1<<5;
aux,dist_act=dist(gx,gy);
for(int tmp=1; tmp<=40; tmp++)
{
if(rand()&1)
{
bra1();
bra2();
}
else
{
bra2();
bra1();
}
if(tmp%2==0)
pas/=2;
}
printf("%.6lf %.6lf\n",gx,gy);
return 0;
}