Pagini recente » Cod sursa (job #2763496) | Cod sursa (job #1378986) | Cod sursa (job #1615873) | Borderou de evaluare (job #3245280) | Cod sursa (job #3155286)
#include <fstream>
#include <cmath>
using namespace std;
ifstream cin ("adapost2.in");
ofstream cout ("adapost2.out");
const short directie[2][4] = {{-1 , 0 , 1 , 0} , {0 , 1 , 0 , -1}};
double abscisa = 500 , ordonata = 500 , distanta_minima;
pair <double , double> coordonate[50001];
int numar_soldati;
double Distanta (pair <double , double> locatie)
{
if (locatie.first < 0.0 || locatie.second < 0.0 || locatie.first > 1000.0 || locatie.second > 1000.0)
return 1e19;
double distanta = 0;
for (int indice = 1 ; indice <= numar_soldati ; indice++)
distanta += sqrt((locatie.first - coordonate[indice].first) * (locatie.first - coordonate[indice].first) + (locatie.second - coordonate[indice].second) * (locatie.second - coordonate[indice].second));
return distanta;
}
void Cautare ()
{
double salt = 250;
while (salt >= 0.0001)
{
for (int indice = 0 ; indice < 4 ; indice++)
{
const double _abscisa = abscisa + salt * directie[0][indice] , _ordonata = ordonata + salt * directie[1][indice] , _distanta = Distanta(make_pair(_abscisa , _ordonata));
if (_distanta < distanta_minima) { abscisa = _abscisa; ordonata = _ordonata; distanta_minima = _distanta; }
}
salt /= 2;
}
}
int main ()
{
cin >> numar_soldati;
for (int indice = 1 ; indice <= numar_soldati ; indice++)
cin >> coordonate[indice].first >> coordonate[indice].second;
distanta_minima = Distanta(make_pair(abscisa , ordonata));
Cautare();
cout << fixed << abscisa << ' ' << ordonata;
cout.close(); cin.close();
return 0;
}