Advertisement
Guest User

Minimum Unobtainable Sum with Statistical Elimination

a guest
Jan 12th, 2014
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.Threading.Tasks;
  7. using System.Threading;
  8.  
  9. namespace MinimumUnobtainableSum
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Stopwatch s = Stopwatch.StartNew();
  16.             Random r = new Random();
  17.             int N = 100000; // r.Next(100000);
  18.             int M = 100000; // r.Next(100000);
  19.  
  20.             int[] S = new int[N];
  21.             bool oneExists = false;
  22.             for (int i = 0; i < N; i++)
  23.             {
  24.                 S[i] = r.Next(1000000000);
  25.                 if (S[i] == 1)
  26.                     oneExists = true;
  27.             }
  28.  
  29.             if (!oneExists)
  30.             {
  31.                 //all results 1
  32.                 s.Stop();
  33.                 Console.WriteLine("N: {0}, M: {1}, time: {2}", N, M, s.Elapsed.ToString());
  34.                 Console.ReadKey();
  35.                 return;
  36.             }
  37.  
  38.             int[,] Queries = new int[M, 2];
  39.             for (int i = 0; i < M; i++)
  40.             {
  41.                 Queries[i, 0] = r.Next(N - 1);
  42.                 Queries[i, 1] = r.Next(N - 1);
  43.                 if (Queries[i, 0] > Queries[i, 1])
  44.                 {
  45.                     int temp = Queries[i, 1];
  46.                     Queries[i, 1] = Queries[i, 0];
  47.                     Queries[i, 0] = temp;
  48.                 }
  49.             }
  50.  
  51.             int[] results = new int[M];
  52.  
  53.  
  54.             var result = Parallel.For(0, M, i =>
  55.             {
  56.                 var d = Queries[i, 1] - Queries[i, 0];
  57.                 if (d <= 0)
  58.                     return;
  59.                 int[] subS = new int[d + 1];
  60.                 bool one_exists = false;
  61.                 for (int k = 0, j = Queries[i, 0]; j <= Queries[i, 1]; j++, k++)
  62.                 {
  63.                     subS[k] = S[j];
  64.                     if (subS[k] == 1)
  65.                         one_exists = true;
  66.                 }
  67.  
  68.                 if (!one_exists)
  69.                 {
  70.                     results[i] = 1;
  71.                     return;
  72.                 }
  73.  
  74.                 subS = subS.OrderBy(x => x).ToArray();
  75.  
  76.                 results[i] = Calculate(subS);
  77.             });
  78.  
  79.             s.Stop();
  80.             Console.WriteLine("N: {0}, M: {1}, time: {2}", N, M, s.Elapsed.ToString());
  81.             Console.ReadKey();
  82.         }
  83.  
  84.         static int Calculate(int[] S)
  85.         {
  86.             int max = 0;
  87.             for (int i = 0; i < S.Length; i++)
  88.             {
  89.                 if (S[i] <= max + 1)
  90.                     max = max + S[i];
  91.                 else
  92.                     return max + 1;
  93.             }
  94.             return max + 1;
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement