Scapegoat
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <algorithm> 8 using namespace std; 9 typedef long long ll; 10 const ll mod=1e9+7; 11 const int maxn=2e5+10; 12 #define dug printf("************\n") 13 struct node///预判下一步把一个人加入哪,它的作用更大,则把预判比较大的加入一个人 14 { 15 int id;///编号 16 double cnt;///人数 17 double val;///预判总方差 18 bool operator < (const node &num) const { 19 return val<num.val; 20 } 21 }; 22 23 int n,m; 24 double a[maxn]; 25 double avg; 26 priority_queue<node>q; 27 int mp[maxn]; 28 29 int main() 30 { 31 int t; 32 scanf("%d",&t); 33 for(int cas=1; cas<=t; cas++ ) 34 { 35 while(!q.empty()) q.pop(); 36 scanf("%d%d",&n,&m); 37 avg=0; 38 for(int i=0; i<n; i++) 39 { 40 scanf("%lf",&a[i]); 41 mp[i]=1; 42 avg+=a[i]; 43 } 44 avg/=(double)m; 45 for(int i=0; i<n;i++) 46 { 47 double x=mp[i]*(a[i]-avg)*(a[i]-avg)-2*(a[i]/2-avg)*(a[i]/2-avg); 48 q.push(node{i,mp[i],x}); 49 } 50 for(int i=0; i<m-n; i++) 51 { 52 node now=q.top(); 53 q.pop(); 54 mp[now.id]++; 55 double x=mp[now.id]*(a[now.id]/mp[now.id]-avg)*(a[now.id]/mp[now.id]-avg)-(mp[now.id]+1)*(a[now.id]/(mp[now.id]+1)-avg)*(a[now.id]/(mp[now.id]+1)-avg); 56 q.push(node{now.id,mp[now.id],x}); 57 } 58 double ans=0; 59 for(int i=0; i<n; i++) ans+=1.0*mp[i]*(a[i]/mp[i]-avg)*(a[i]/mp[i]-avg); 60 ans/=(double)m; 61 printf("Case #%d: %.12lf\n",cas,ans); 62 } 63 return 0; 64 }