|
|
back to boardI know, this problem has a simple O(1) solution, but may be you would be interested... someone said about for(1..999^2), there is it. so nowtime i think - is there some tests which will be failed with this solution? on TIMUS tests it's got AC... #include <stdio.h> #define SUM 1000 #define DELTA 1 int main() { float f1,f2,f3, i,j, k1,k2,k3, t1,t2,t3, m,mx=0; scanf("%f%f%f",&k1,&k2,&k3); for(f1=0;f1<=100;f1++) for(f2=0;f2<=100-f1;f2++) { f3=(100-f1-f2); t1 = SUM * (f1/100) * k1; t2 = SUM * (f2/100) * k2; t3 = SUM * (f3/100) * k3; m=t1; if(t2<m) m=t2; if(t3<m) m=t3; if(m>mx) { mx=m; i=f1; j=f2; } }
for(f1=i-DELTA;f1<=i+DELTA;f1+=0.001) for(f2=j-DELTA;f2<=j+DELTA;f2+=0.001) { f3=(100-f1-f2); t1 = SUM * (f1/100) * k1; t2 = SUM * (f2/100) * k2; t3 = SUM * (f3/100) * k3; m=t1; if(t2<m) m=t2; if(t3<m) m=t3; if(m>mx) mx=m; } printf("%0.0f",mx); return 0; } Edited by author 16.12.2007 21:04 Re: I know, this problem has a simple O(1) solution, but may be you would be interested... Posted by Egor 27 Nov 2016 18:43 And with binary search ;) bool good(double k[3], double x0) { double x1 = k[0] / k[1] * x0; double x2 = k[0] / k[2] * x0; return x0 + x1 + x2 <= 1000.0; } int main() { double k[3]; for (int i = 0; i < 3; i++) cin >> k[i]; sort(k, k + 3); double l = 0, r = 1000; while (r - l >= 0.001) { double m = l + (r - l) / 2; if (good(k, m)) l = m; else r = m; } cout << int(l * k[0] + 0.5); } |
|
|