Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int main() {
  6. ifstream fin("binsearch.in");
  7. ofstream fout("binsearch.out");
  8. int n, m;
  9.  
  10. cin >> n;//array size
  11. int* a = new int[n];
  12. for (int i = 0; i < n; ++i)
  13. cin >> a[i];
  14.  
  15. cin >> m;//requests count
  16. int* rq = new int[m];
  17. for (int i = 0; i < m; ++i)
  18. cin >> rq[i]; //requests
  19.  
  20. int k = 0, count_rq1 = 0, count_rq2 = 0, h = 0;
  21.  
  22. for (int i = 0; i < n, k < m; ++i)
  23. {
  24. //if (k == m) break;//if all requests done end "for"
  25.  
  26. int x = rq[k];//current request
  27.  
  28. if (x != a[i])
  29. {
  30. int error = -1;
  31. cout << error << " " << error << endl;
  32. ++k;
  33. }
  34. if (x == a[i])//if found request in array
  35. {
  36. count_rq2 = i;
  37. ++h;
  38.  
  39. if (a[i + 1] != x)//if next element != request than will find a new request
  40. {
  41. ++k;
  42. h -= 1;
  43. count_rq1 = count_rq2 - h + 1;
  44. count_rq2 += 1;
  45. cout << count_rq1 << " " << count_rq2 << endl;
  46. h = 0;
  47. }
  48. }
  49.  
  50.  
  51. }
  52. delete(a);
  53. delete(rq);
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement