Advertisement
Guest User

Untitled

a guest
May 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <cstdio>
  3. #include <cstdlib>
  4.  
  5. int particleAmount;
  6. int *particleValue;
  7. int queryAmount;
  8. int *queryValue;
  9.  
  10. int searchUntilLeftIndexFound(int *arr, int query, int size)
  11. {
  12.     int result = 0;
  13.     int p = size;
  14.     int l = 0;
  15.     int s;
  16.     while (true)
  17.     {
  18.         s = (l + p) / 2;
  19.         if (s == query)
  20.             p = s - 1;
  21.         else if (s < query)
  22.             l = s + 1;
  23.  
  24.         if (l + 1 == p)
  25.         {
  26.             if (l == query)
  27.                 result = l;
  28.             else
  29.                 result = p;
  30.             break;
  31.         }
  32.     }
  33.     return result;
  34. }
  35.  
  36. int searchUntilRightIndexFound(int *arr, int query, int size, int _s)
  37. {
  38.     int result = 0;
  39.     int p = size;
  40.     int l = _s;
  41.     int s;
  42.     while (true)
  43.     {
  44.         s = (l + p) / 2;
  45.         if (s == query)
  46.             l = s + 1;
  47.         else if (s > query)
  48.             p = s - 1;
  49.  
  50.         if (l + 1 == p)
  51.         {
  52.             if (p == query)
  53.                 result = p;
  54.             else
  55.                 result = l;
  56.             break;
  57.         }
  58.     }
  59.     return result;
  60. }
  61.  
  62. int binarySearch(int *arr, int query, int size)
  63. {
  64.     int result = 0;
  65.     int p = size;
  66.     int l = 0;
  67.     int s;
  68.     while (true)
  69.     {
  70.         s = (p + l)/2;
  71.         if (s > query)
  72.             p = s - 1;
  73.         else if (s < query)
  74.             l = s + 1;
  75.         else if (s == query)
  76.         {
  77.             int leftIndex = searchUntilLeftIndexFound(arr, query, size);
  78.             int rightIndex = searchUntilRightIndexFound(arr, query, size, s);
  79.             result = rightIndex - leftIndex + 1;
  80.             break;
  81.         }
  82.     }
  83.     return result;
  84. }
  85.  
  86. int main()
  87. {
  88.     // Get particle amount
  89.     scanf("%d", &particleAmount);
  90.  
  91.     // Create array containing particle value
  92.     particleValue = new int[particleAmount];
  93.  
  94.     // Get value of each particle TU WYPIERDALA NA SCANF
  95.     for (int i = 0; i < particleAmount; i++)
  96.         scanf("%d", particleValue[i]);
  97.    
  98.     // Get amount of queries
  99.     scanf("%d", &queryAmount);
  100.  
  101.     // Create array containing query value
  102.     queryValue = new int[queryAmount];
  103.  
  104.     // Get value of each query
  105.     for (int i = 0; i < queryAmount; i++)
  106.         scanf("%d", queryValue);
  107.  
  108.     // Print value of each query
  109.     for (int i = 0; i < queryAmount; i++)
  110.         printf("%d", binarySearch(particleValue, queryValue[i], particleAmount));
  111.     system("pause");
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement