Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function main(cond, omin, omax, k, k1) {
- let n = 0; //счетчик действий
- while(k>=2) {
- n++;
- if (omin===0) break;
- if (omin*k>=omax/k && omax/k>omin) {
- omax/=k;
- } else if (omin*k<omax && omax/k<=omin*k) {
- omin*=k;
- } else if (omax/k>omin*k) {
- omax/=k;
- omin*=k;
- } else {
- k/=2;
- k = Math.floor(k);
- continue;
- }
- if (cond(omin)) {
- omax = omin;
- omin /= k;
- k/=2;
- k = Math.floor(k);
- continue;
- } else if (!cond(omax)) {
- omax *= k;
- omin = omax/k;
- k/=2;
- k = Math.floor(k);
- continue;
- }
- }
- let d = Math.max(Math.floor((omax - omin)/k1), 1);
- while((omax-omin)!=1) {
- n++;
- if (omin+d>=omax-d && omax+d>omin) {
- omax-=d;
- } else if (omin+d<omax && omax-d<=omin+d) {
- omin+=d;
- } else if (omax-d>omin+d) {
- omax-=d;
- omin+=d;
- } else {
- d/=2;
- d = Math.floor(d);
- continue;
- }
- if (cond(omin)) {
- omax = omin;
- omin -= d;
- d/=2;
- d = Math.floor(d);
- continue;
- } else if (!cond(omax)) {
- omax += d;
- omin = omax - d;
- d/=2;
- d = Math.floor(d);
- continue;
- }
- }
- return [omax, n];
- }
- function testPerformance(k1,k2) {
- let sum = 0;
- let wrong = 0;
- const OPERATIONS = 1000000;
- for (let i=0; i<OPERATIONS; i++) {
- var c = Math.floor(Math.random()*1000000000)
- var v = main(function(x) {return x>=c;}, 1, 1000000000, k1,k2);
- if (c!==v[0]) wrong++;
- sum+=v[1];
- }
- return [sum/OPERATIONS, wrong];
- }
Advertisement
Add Comment
Please, Sign In to add comment