Advertisement
darrellp

Max XOR sum

Dec 24th, 2021
1,402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. Random rnd = new Random();
  2.  
  3. void Main()
  4. {
  5.     const int cTests = 100;
  6.     const int cRuns = 100;
  7.     const int cVals = 10;
  8.     var numFails = 0;
  9.    
  10.     for (int iRun = 0; iRun < cRuns; iRun++)
  11.     {
  12.         var a = new int[cVals];
  13.         var b = new int[cVals];
  14.         int max = -1;
  15.         var reordering = new int[cVals];
  16.  
  17.         for (int i = 0; i < cVals; i++)
  18.         {
  19.             a[i] = rnd.Next(10000);
  20.             b[i] = rnd.Next(10000);
  21.         }
  22.        
  23.         Console.WriteLine($"Starting run {iRun}");
  24.         for (int iTest = 0; iTest < cTests; iTest++)
  25.         {
  26.             Shuffle(b);
  27.             if (max < 0)
  28.             {
  29.                 (reordering, max) = FindEstimatedMax(a, b);
  30.             }
  31.             else
  32.             {
  33.                 var (newReordering, newMax) = FindEstimatedMax(a, b);
  34.                 if (newMax > max)
  35.                 {
  36.                     Console.WriteLine($"Oops!  Algorithm failed! oldMax = {max}, newMax = {newMax}");
  37.                     reordering = newReordering;
  38.                     max = newMax;
  39.                     numFails++;
  40.                 }
  41.             }
  42.         }
  43.     }
  44.     Console.WriteLine($"End of Tests. {numFails} failures.");
  45. }
  46.  
  47. void Shuffle(IList<int> b)
  48. {
  49.     for (int i = 0; i < b.Count - 1; i++)
  50.     {
  51.         var j = i + rnd.Next(b.Count - i);
  52.         (b[i], b[j]) = (b[j], b[i]);
  53.     }
  54. }
  55.  
  56. int XorSum(IList<int>a, IList<int>b)
  57. {
  58.     return a.Zip(b).Select(t => t.First ^ t.Second).Sum();
  59. }
  60.  
  61. (int[] reordering, int max) FindEstimatedMax(IList<int> a, IList<int> b)
  62. {
  63.     var bCopy = new int[a.Count];
  64.     var bestReordering = new int[a.Count];
  65.     var nSuccess = 0;
  66.     var curMax = -1;
  67.     Array.Copy(b.ToArray(), bCopy, b.Count);
  68.    
  69.     while (nSuccess < 100)
  70.     {
  71.         Shuffle(bCopy);
  72.         while (FindIncreasingTranspose(a, bCopy));
  73.         var newMax = XorSum(a, bCopy);
  74.         if (newMax > curMax)
  75.         {
  76.             nSuccess = 0;
  77.             curMax = newMax;
  78.             Array.Copy(bCopy, bestReordering, b.Count);
  79.         }
  80.         else
  81.         {
  82.             nSuccess++;
  83.         }
  84.     }
  85.     return (bestReordering, curMax);
  86. }
  87.  
  88. bool FindIncreasingTranspose(IList<int> a, IList<int> b)
  89. {
  90.     for (var i = 0; i < a.Count - 1; i++)
  91.     {
  92.         for (var j = i + 1; j < a.Count; j++)
  93.         {
  94.             if ((a[i] ^ b[j]) + (a[j] ^ b[i]) - (a[i] ^ b[i]) - (a[j] ^ b[j]) > 0)
  95.             {
  96.                 (b[i], b[j]) = (b[j], b[i]);
  97.                 return true;
  98.             }
  99.         }
  100.     }
  101.     return false;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement