1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| const int maxn=333; int n,m,a[maxn],x[maxn],y[maxn],z[maxn],v[maxn]; int choice[maxn]; LL ans=1e18,sum; int num=0; LL sqr(const LL &x){return x*x;} void doit() { long double T=1e12,q=0.999; while (T>1e-6) { rep(times,1,20) { LL sumt=sum; int r1=rnd()%n,r2=rnd()%n; if (r1==r2) continue; sumt-=sqr(z[choice[r1]]+v[choice[r1]]*r1); sumt-=sqr(z[choice[r2]]+v[choice[r2]]*r2); swap(choice[r1],choice[r2]); sumt+=sqr(z[choice[r1]]+v[choice[r1]]*r1); sumt+=sqr(z[choice[r2]]+v[choice[r2]]*r2); if (sumt<sum) { sum=sumt; ans=min(ans,sum); } else if (exp((sum-sumt)/T)*RAND_MAX>((unsigned int)rnd())%RAND_MAX) sum=sumt; else swap(choice[r1],choice[r2]); } if (T>1e-3) T*=q; else T*=0.7; } } bool cmp(const int &x,const int &y){return v[x]>v[y];} bool cmp2(const int &x,const int &y){return z[x]>z[y];} int main() { #ifdef DEBUG freopen("a.in","r",stdin); #endif srand(time(0)^19260817); read(n); rep(i,0,n-1) {read(x[i],y[i]);read(z[i],v[i]);} LL sum_static=0; rep(i,0,n-1) sum_static+=sqr(x[i])+sqr(y[i]); int t=1;
while (t--) { rep(i,0,n-1) { choice[i]=i; sum+=sqr(z[i]+v[i]*i); } ans=min(ans,sum); doit(); }
t=1; while (t--) { sum=0; sort(choice,choice+n,cmp); rep(i,0,n-1) sum+=sqr(z[choice[i]]+v[choice[i]]*i); ans=min(ans,sum); doit(); }
t=1; while (t--) { sum=0; sort(choice,choice+n,cmp2); rep(i,0,n-1) sum+=sqr(z[choice[i]]+v[choice[i]]*i); ans=min(ans,sum); doit(); }
t=1; while (t--) { sum=0; random_shuffle(choice,choice+n); rep(i,0,n-1) sum+=sqr(z[choice[i]]+v[choice[i]]*i); ans=min(ans,sum); doit(); }
cout<<ans+sum_static<<endl; return 0; }
|