Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.55 KB | None | 0 0
  1. long computeCase(Pair[] arr)
  2. {
  3.     arr.sort();
  4.     double k = 0.0;
  5.     long count = 1;
  6.  
  7.     while(true)
  8.     {
  9.         auto switches = iota(arr.length-1)
  10.                         .map!(i => tuple!("a", "b", "c", "d", "id")(arr[i].a, arr[i].b, arr[i+1].a, arr[i+1].b, i))
  11.                         .filter!(e => e.b != e.d)
  12.                         .map!(e => tuple!("f", "id")(cast(double)(e.c - e.a) / (e.b - e.d), e.id))
  13.                         .filter!(e => e.f > k);
  14.         if(switches.empty)
  15.             break;
  16.         auto bestSwitch = switches.minElement;
  17.         swap(arr[bestSwitch.id], arr[bestSwitch.id+1]);
  18.         k = bestSwitch.f;
  19.         count++;
  20.     }
  21.  
  22.     return count;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement