开始想复杂了...纠结了很久..后来发现数据范围很小阿...枚举所有情况也就那么多...所以就两层递归暴力枚举..结果就过了....话说这题主要还是题意要理解清楚...这里的mean和variance中的Xi..是指排好序的F[i]/R[i]的前后相差的数...其实样例下面的说明也体现了...
Program:
/* ID: zzyzzy12 LANG: C++ TASK: cowcycle */ #include<iostream> #include<istream> #include<stdio.h> #include<string.h> #include<math.h> #include<stack> #include<map> #include<algorithm> #include<queue> #define oo 1000000000 #define ll long long #define pi (atan(2)+atan(0.5))*2 using namespace std; int F,R,F1,F2,R1,R2,f[6],r[11]; int ansF[6],ansR[11]; double MinData,mean,x[1001],t; void judge() { int i,j,num=0; for (i=1;i<=F;i++) for (j=1;j<=R;j++) x[++num]=f[i]*1.0/r[j]; sort(x+1,x+1+num); num--; for (i=1;i<=num;i++) x[i]=x[i+1]-x[i]; mean=0; for (i=1;i<=num;i++) mean+=x[i]; mean/=num; t=0; for (i=1;i<=num;i++) t+=(x[i]-mean)*(x[i]-mean); t/=num; if (t<MinData) { MinData=t; for (i=1;i<=F;i++) ansF[i]=f[i]; for (i=1;i<=R;i++) ansR[i]=r[i]; } return; } void DFS2(int p,int h) { if (p>R) { if (f[F]*r[R]<3*f[1]*r[1]) return; judge(); return; } for (int i=h;i<=R2;i++) { r[p]=i; DFS2(p+1,i+1); } } void DFS1(int p,int h) { if (p>F) { DFS2(1,R1); return; } for (int i=h;i<=F2;i++) { f[p]=i; DFS1(p+1,i+1); } } int main() { freopen("cowcycle.in","r",stdin); freopen("cowcycle.out","w",stdout); scanf("%d%d%d%d%d%d",&F,&R,&F1,&F2,&R1,&R2); int i,j; MinData=oo; DFS1(1,F1); printf("%d",ansF[1]); for (i=2;i<=F;i++) printf(" %d",ansF[i]); printf("\n%d",ansR[1]); for (i=2;i<=R;i++) printf(" %d",ansR[i]); printf("\n"); return 0; }