BZOJ2508: 简单题

纯的数学题

http://www.cnblogs.com/clrs97/p/4403197.html


可以把那个式子推出来

然后我一开始脑抽的去配方  结果搞傻掉了。。。

后来发现对于一个确定的x 要使ans最小则y为一个定量  

那么就是把y化成x在带入就好了‘

Claris讲的很详细啊(说句不要脸的话其实我就是这么想的

#include<cstdio>
#include<cmath>
#define N 120010
int n,q,Oper,i,m;
double X1,X2,Y1,Y2;
double a,b,c,d;
double Aa[N],Bb[N],Cc[N],Ab[N],Ac[N],Bc[N];
double Suma,Sum2b,Sum3c,Sumb,Sumc,Sum2c,Ans;
const
   double eps=1e-8;

char hc;
inline void read(int &a)
{
	a=0;do hc=getchar();while(hc<'0'||hc>'9');
      while(hc<='9'&&hc>='0')a=(a<<3)+(a<<1)+hc-'0',hc=getchar();	
}
inline double abs(double a){return a<0?-a:a;}
inline bool isZero(double x){return abs(x)<eps;}
inline double solve(double a,double b,double c)
{
  if(isZero(a))return c;
  double x=-b/(2*a);
  return a*x*x+b*x+c;
}
int main()
{
  read(q);
  while(q--)
  {
    read(Oper);
	if(Oper==0)
	{
      scanf("%lf%lf%lf%lf",&X1,&Y1,&X2,&Y2);
      if(isZero(X1-X2))
	       a=1,b=0,c=-X1;
	  else a=(Y2-Y1)/(X2-X1),b=-1,c=Y1-a*X1;
      d=a*a+b*b;
      Aa[++n]=a*a/d,Bb[n]=b*b/d,Cc[n]=c*c/d,
	    Ab[n]=a*b/d,Ac[n]=a*c/d,Bc[n]=b*c/d;
      Suma+=Aa[n],Sum2b+=Bb[n],Sum3c+=Cc[n],
	   Sumb+=Ab[n],Sumc+=Ac[n],Sum2c+=Bc[n];
      m++;
    }
    else if(Oper==1)
	{
      read(i);
      Suma-=Aa[i],Sum2b-=Bb[i],Sum3c-=Cc[i],
	  Sumb-=Ab[i],Sumc-=Ac[i],Sum2c-=Bc[i];
      m--;
    }
    if(Oper==2)
	{
      if(!m){puts("0.00");continue;}
      if(isZero(Sum2b))
	        a=b=0;
	  else a=-Sumb/Sum2b,b=-Sum2c/Sum2b;
      Ans=solve(Suma+2*a*Sumb+a*a*Sum2b,2*(b*Sumb+Sumc+a*b*Sum2b+a*Sum2c),b*b*Sum2b+2*b*Sum2c+Sum3c);
      if(isZero(Ans))Ans=0;
      printf("%.2f\n",Ans);
    }
  }
  return 0;
}


你可能感兴趣的:(bzoj)