View Single Post
  #35  
Old 07-19-2008, 09:26 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
PHP Code:
//#CLIENTSIDE
function onWeaponFired() {
  
// Bad match array
  
temp.bad = {
             {
91,50},
             {
60,230},
             {
132,399},
             {
52,293},
             {
22,25},
             {
83,293},
             {
83,82}
             };
  
// Generate students array
  
temp.students = new[400];
  for (
temp.0temp.400temp.i++)
    
temp.students[temp.i] = temp.i;



  
// START of exercise
  
temp.start timevar2;

  
// Shuffle students array
  
temp.temp.students.size();
  for (
temp.0temp.temp.1temp.i++) {
    
temp.temp.random(0,32767) / (32767 / (temp.temp.i) + 1);
    
temp.temp.students[temp.j];
    
temp.students[temp.j] = temp.students[temp.i];
    
temp.students[temp.i] = temp.t;
  }
  
  
// Heapsort the bad match array
  
temp.bad.sortascending();
  
  
temp.pairs = new[50];
  
temp.0;
  while (
temp.50) {
    
temp.pair = {temp.students[temp.x], temp.students[temp.x+1]};

    
// Binary search for the current pair in the bad pairs
    
if (binarysearcharray(temp.badtemp.pair) == -1) {
      
temp.pairs[temp.i] = temp.pair;
      
temp.x++;
    }
    
temp.x++;
    
temp.i++;
  }
  
  
  
// END of exercise
  
temp.time timevar2 temp.start;
    
  
player.chat temp.pairs;
  echo (
temp.time);
}

// Modified binary search
function binarysearcharray(temp.atemp.v) {
  
temp.low 0;
  
temp.high temp.a.size()-1;
  while (
temp.low <= temp.high) {
    
temp.mid = (temp.low temp.high) /2;
    if (
temp.a[temp.mid][0] > temp.v[0] || temp.a[temp.mid][0] == temp.v[0] && temp.a[temp.mid][1] > temp.v[1]) 
      
temp.high temp.mid 1;
    else if (
temp.a[temp.mid][0] < temp.v[0] || temp.a[temp.mid][0] == temp.v[0] && temp.a[temp.mid][1] < temp.v[1])
      
temp.low temp.mid +1;
    else
      return 
temp.mid;
  }
  return -
1;

Average running time of 0.00326736083, but it's a rather small "bad pair" set.

Should run in O(n log n log m) where n is the number of pairs and m is the number of bad pairs.

Last edited by WhiteDragon; 07-20-2008 at 09:20 PM.. Reason: Slight mistake
Reply With Quote