Cod sursa(job #2278005)

Utilizator Dragomiralexandru621@yahoo.comDragomir ionut alexandru [email protected] Data 7 noiembrie 2018 10:19:21
Problema Adapost 2 Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <iostream>
#include<fstream>
#include<math.h>
#include<string.h>
using namespace std;
ifstream f("adapost2.in") ;
ofstream g("adapost2.out") ;
 double ax[50001] , ay[50001] , x , y ,sum , sumx ;
 int n  ;
 int sx[] = { -1 , 1 , 0 , 0 } , sy[] = { 0 , 0 , -1 , 1 } ;
 double distanta( double bx , double by , double cx , double cy )
 {
     return (double)sqrt(( bx - cx ) * ( bx - cx ) + ( by - cy ) * ( by - cy )) ;
 }

 double suma( double bx , double by )
 {   double s = 0 ;
     for( int  i = 1 ; i <= n ; i ++ )
        s = s + distanta( bx  , by , ax[i] , ay[i] ) ;
     return s ;
 }

 int main(){
f >> n ;
for(int  i = 1 ; i <= n ; i ++ )
{
    f >> ax[i] >> ay[i] ;
    x = x + ax[i] ;
    y = y + ay[i] ;
}
x = x / n ;
y = y / n ;
sum = suma( x , y ) ;// g<< sum <<"\n";
double x1 = x , y1 = y ;
double l = 1000 ;
while ( l > 0.0001 ){
        int ok = 1 ;
    for(int  i = 0 ; i <= 3 ; i ++ )
    {
        x1 = x + l * sx[i] ;
        y1 = y + l * sy[i] ;
        sumx = suma( x1 , y1 ) ;
     //   g<< sumx << " \n";
        if ( sumx < sum )
        {
            sum = sumx ;
            x = x1 ;
            y = y1 ;
            ok = 0 ;
          // g<< x << " " << y << "\n" ;
        }
    }
   if( ok )
    l = l / 2.0 ;

}
g<< x << " " << y ;
 }