USACO 6.3 Cowcycles
Originally by Don Gillies
[International readers should note that some words are puns on cows.]
Having made a fortune on Playbov magazine, Hugh Heifer has moved from his original field in the country to a fashionable yard in the suburbs. To visit fond pastoral memories, he wishes to cowmmute back to his old stomping grounds. Being environmentally minded, Hugh wishes to transport himself using his own power on a Cowcycle (a bicycle specially fitted for his neatly manicured hooves).
Hugh weighs over a ton; as such, getting smoothly up to speed on traditional cowcycle gear sets is a bit challenging. Changing among some of the widely spaced gear ratios causes exertion that's hard on Hugh's heart.
Help Hugh outfit his Cowcycle by choosing F (1 <= F <= 5) gears (sprockets) in the front and R (1 <= R <= 10) gears in the rear of his F*R speed cowcycle subject to these rules:
- The possible sizes (number of teeth) for the F front gears are specified.
- The possible sizes (number of teeth) for the R rear gears are specified.
- At any given gear setting, the gear ratio is the quotient of the number of teeth on the front gear and the number of teeth on the rear gear (i.e., number of front gear teeth divided by number of rear gear teeth)
- The largest gear ratio must be at least three times the smallest.
- The variance (see below) of the set of DIFFERENCES between successive (i.e., after sorting) gear ratios should be minimized.
Calculate the mean and variance of a set of differences (xi in this formula) by the following formulae:
$\bar{x}= \frac{1}{n}\sum_{1}^{n}x_{i}$
$F= \frac{1}{n}\sum_{1}^{n}\left (\bar{x}-x_{i} \right )^{2}$
Deduce and print the optimal sets of F front gears and R rear gears so that the variance is minimized (and the ratios span a factor of at least 3x).
PROGRAM NAME: cowcycle
INPUT FORMAT
The first line contains F and R, the numbers of front and rear gears. The second line contains four numbers: F1, F2 (25 <= F1 < F2 <= 80), R1, and R2 (5 <= R1 < R2 <= 40). All front gears from F1 through F2 are available; all rear gears from R1 through R2 are available. There will exist at least one legal set of gears.
SAMPLE INPUT (file cowcycle.in)
2 5 39 62 12 28
OUTPUT FORMAT
Display the number of teeth on the set of F chosen front gears, from smallest to largest, on the first line of output (separated by spaces). Display the number of teeth on the set of R chosen rear gears, from smallest to largest, on the second line of output. All gears have an integer number of teeth, of course.
If multiple optimal answers exist, output the answer with the smallest front gear set (smallest first gear, or smallest second gear if first gears match, etc.). Likewise, if all first gears match, output the answer with the smallest rear gear set (similar rules to the front gear set).
SAMPLE OUTPUT (file cowcycle.out)
39 53 12 13 15 23 27
Comment
The challenge in this problem is "reading the problem". Don't read further if you are working on that level of challenge. If the problem is just completely unclear to you, read in.
The problem wants you to find "an optimal set of gear ratios" such that the spacing between the ratios is most uniform. Consider the test case above:
2 5 39 62 12 28
This specifies two front gears from the set 39..62; five rear gears from the set 12..28. The program must examine all possible pairs of 62-39+1=24 front gears and all possible quintuples from 28-12+1=17 rear gears. Combinatorically, The total number of possibilities is (24 take 2) times (17 take 5), which is 24!/22!/2! x 17!/5!/12! which is 656,880 possibilities (I think).
For each of these possibilities, calculations like the following. This example considers in some sense the "first" case: front gears of 39 and 40, rear gears of 12, 13, 14, 15, and 16.
First, calculate all the possible ratios:
39/12 = 3.25000000000000000000 39/13 = 3.00000000000000000000 39/14 = 2.78571428571428571428 39/15 = 2.60000000000000000000 39/16 = 2.43750000000000000000 40/12 = 3.33333333333333333333 40/13 = 3.07692307692307692307 40/14 = 2.85714285714285714285 40/15 = 2.66666666666666666666 40/16 = 2.50000000000000000000
Then, sort them:
39/16 = 2.43750000000000000000 40/16 = 2.50000000000000000000 39/15 = 2.60000000000000000000 40/15 = 2.66666666666666666666 39/14 = 2.78571428571428571428 40/14 = 2.85714285714285714285 39/13 = 3.00000000000000000000 40/13 = 3.07692307692307692307 39/12 = 3.25000000000000000000 40/12 = 3.33333333333333333333
Then, calculate the absolute value of the differences:
2.43750000000000000000 - 2.50000000000000000000 = 0.06250000000000000000 2.50000000000000000000 - 2.60000000000000000000 = 0.10000000000000000000 2.60000000000000000000 - 2.66666666666666666666 = 0.06666666666666666666 2.66666666666666666666 - 2.78571428571428571428 = 0.11904761904761904762 2.78571428571428571428 - 2.85714285714285714285 = 0.07142857142857142857 2.85714285714285714285 - 3.00000000000000000000 = 0.14285714285714285715 3.00000000000000000000 - 3.07692307692307692307 = 0.07692307692307692307 3.07692307692307692307 - 3.25000000000000000000 = 0.17307692307692307693 3.25000000000000000000 - 3.33333333333333333333 = 0.08333333333333333333
Then, calculate the mean and variance of the set of numbers on the right, above. The mean is (I think): 0.0995370370370370370366666. The variance is approximately 0.00129798488416722.
Of course this set of gears is not valid, since it does not have a 3x span from highest gear to lowest.
Find the set of gears that minimizes the variance and has a 3x or greater span.
————————————————————————题解
Rob Kolstad
This problem is only tricky for generating the proper gears to check ratios and coding so that it doesn't run too long.
这个问题微妙在生成正确的齿轮组和去检查比率并以此敲代码所以它不会跑太长时间。
……这个复杂度我自己都算不对……
但是过了
可能是因为有了三倍约束从而使情况少了吧
6.3全是搜索……啊……
1 /* 2 ID: ivorysi 3 LANG: C++ 4 PROG: cowcycle 5 */ 6 #include <iostream> 7 #include <cstdio> 8 #include <cstring> 9 #include <queue> 10 #include <cmath> 11 #include <set> 12 #include <vector> 13 #include <algorithm> 14 #define siji(i,x,y) for(int i=(x);i<=(y);++i) 15 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j) 16 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i) 17 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j) 18 #define inf 0x5f5f5f5f 19 #define ivorysi 20 #define mo 97797977 21 #define hash 974711 22 #define base 47 23 #define fi first 24 #define se second 25 #define pii pair<int,int> 26 #define esp 1e-10 27 typedef long long ll; 28 using namespace std; 29 int f,r; 30 int f1,f2,r1,r2; 31 int numf[15],numr[15],ansf[15],ansr[15]; 32 double line[55],sub[55],ans; 33 void calc() { 34 if(numf[f]*numr[r] < numf[1]*numr[1]*3) return; 35 siji(i,1,f) { 36 siji(j,1,r) { 37 line[(i-1)*r+j]=(double)numf[i]/numr[j]; 38 } 39 } 40 sort(line+1,line+f*r+1); 41 double aver=0.0,cal=0.0; 42 siji(i,1,f*r-1) { 43 sub[i]=fabs(line[i+1]-line[i]); 44 aver+=sub[i]/(f*r-1); 45 } 46 siji(i,1,f*r-1) { 47 cal+=(sub[i]-aver)*(sub[i]-aver)/(f*r-1); 48 } 49 if(fabs(cal-ans)>esp && cal<ans) { 50 memcpy(ansf,numf,sizeof(ansf)); 51 memcpy(ansr,numr,sizeof(ansr)); 52 ans=cal; 53 } 54 } 55 void dfs2(int k,int prev) { 56 siji(i,prev,r2) { 57 numr[k]=i; 58 if(k==r) calc(); 59 else dfs2(k+1,i+1); 60 } 61 } 62 void dfs1(int k,int prev) { 63 siji(i,prev,f2) { 64 numf[k]=i; 65 if(k==f) dfs2(1,r1); 66 else dfs1(k+1,i+1); 67 } 68 } 69 void init() { 70 scanf("%d%d",&f,&r); 71 scanf("%d%d%d%d",&f1,&f2,&r1,&r2); 72 } 73 void solve() { 74 init(); 75 ans=10000000.0; 76 dfs1(1,f1); 77 siji(i,1,f) printf("%d%c",ansf[i]," \n"[i==f]); 78 siji(i,1,r) printf("%d%c",ansr[i]," \n"[i==r]); 79 } 80 int main(int argc, char const *argv[]) 81 { 82 #ifdef ivorysi 83 freopen("cowcycle.in","r",stdin); 84 freopen("cowcycle.out","w",stdout); 85 #else 86 freopen("f1.in","r",stdin); 87 #endif 88 solve(); 89 return 0; 90 }