orz Shinbokuow

## 前置芝士

\begin{aligned} Ans &=\sum_{i=1}^n{(kx_i-y_i+b)^2\over k^2+1}\\ &=\sum_{i=1}^n{k^2x_i^2+y_i^2+b^2-2kx_iy_i+2kbx_i-2by_i\over k^2+1}\\ &={1\over k^2+1}\left(k^2\sum_{i=1}^nx_i^2+\sum_{i=1}^ny_i^2+nb^2-2k\sum_{i=1}^nx_iy_i+2kb\sum_{i=1}^nx_i-2b\sum_{i=1}^ny_i\right) \end{aligned}

${\partial f\over \partial b}={1\over k^2+1}\left(2nb+2k\sum_{i=1}^nx_i-2\sum_{i=1}^ny_i\right)=0$

$\overline{x}={1\over n}\sum_{i=1}^nx_i,\overline{y}={1\over n}\sum_{i=1}^ny_i$

$b=\overline{y}-k\overline{x}$

$f(k,b)={Ak^2+Bk+C\over k^2+1}$

$A=\sum x_i^2-{(\sum x_i)^2\over n}$

$B={2\sum x_i\sum y_i\over n}-2\sum x_iy_i$

$C=\sum y_i^2-{(\sum y_i)^2\over n}$

$(A-f(k,b))k^2+Bk+C-f(k,b)=0$

$B^2-4(A-\alpha)(C-\alpha)\geq 0$

$-4\alpha^2+4(A+C)\alpha+B^2-4AC\geq 0$

\begin{aligned} {4\times -4\times (B^2-4AC)-16(A+C)^2\over -16} &=B^2-4AC+(A+C)^2\\ &=B^2+(A-C)^2\geq 0 \end{aligned}

## 题解

//minamoto
#include<bits/stdc++.h>
#define R register
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int N=105;const double eps=1e-10;
inline int sgn(R double x){return x<-eps?-1:x>eps;}
struct node{
double x,y;
inline node(){}
inline node(R double xx,R double yy):x(xx),y(yy){}
inline node operator +(const node &b)const{return node(x+b.x,y+b.y);}
inline node operator -(const node &b)const{return node(x-b.x,y-b.y);}
inline double operator *(const node &b)const{return x*b.y-y*b.x;}
inline node operator *(const double &b)const{return node(x*b,y*b);}
inline double operator ^(const node &b)const{return x*b.x+y*b.y;}
}p[N],v;
struct qwq{
node p;double d;bool in;
inline qwq(){}
inline qwq(R node pp,R double dd,R bool ii):p(pp),d(dd),in(ii){}
inline bool operator <(const qwq &b)const{return d<b.d;}
}st[N];
struct Line{
double x,y,xx,yy,xy;int sz;
inline void ins(R node p){x+=p.x,y+=p.y,xx+=p.x*p.x,yy+=p.y*p.y,xy+=p.x*p.y,++sz;};
inline void del(R node p){x-=p.x,y-=p.y,xx-=p.x*p.x,yy-=p.y*p.y,xy-=p.x*p.y,--sz;};
inline void clr(){x=y=xx=yy=xy=sz=0;}
double calc(){
if(!sz)return 0;
double xa=x/sz,ya=y/sz,A=xx-sz*xa*xa;
double B=2*xa*ya*sz-2*xy,C=yy-sz*ya*ya;
double a=4,b=-4*(A+C),c=4*A*C-B*B;
return (-b-sqrt(b*b-4*a*c))/(a*2);
}
}l1,l2;
double res=1e18;int n;
int main(){
//	freopen("testdata.in","r",stdin);
scanf("%d",&n);
fp(i,1,n)scanf("%lf%lf",&p[i].x,&p[i].y);
fp(a,1,n)fp(b,1,n)if(a!=b){
v=p[b]-p[a];
fp(i,1,n)st[i]=qwq(p[i],v^(p[i]-p[a]),v*(p[i]-p[a])>eps);
st[a].in=1;
sort(st+1,st+1+n),l1.clr(),l2.clr();
fp(i,1,n)st[i].in?l1.ins(st[i].p):l2.ins(st[i].p);
cmin(res,l1.calc()+l2.calc());
fp(i,1,n){
if(st[i].in)l1.del(st[i].p),l2.ins(st[i].p);
else l2.del(st[i].p),l1.ins(st[i].p);
cmin(res,l1.calc()+l2.calc());
}
}
printf("%.10lf\n",res/n);
return 0;
}

posted @ 2019-04-13 11:53  bztMinamoto  阅读(303)  评论(0编辑  收藏  举报
Live2D