Advertisement
gilzean

Untitled

May 22nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define NMax 100100
  3. #define Min(a, b) (a < b) ? a : b
  4. #define INF 0x3f3f3f3f
  5.  
  6. using namespace std;
  7.  
  8. int N, M, pare[4*NMax], impare[4 * NMax], i, Minim1, Minim2, poz, val, start, sfarsit, tip, a, b, x, Minim, app_on_pos[NMax];
  9. void Update (int nod, int st, int dr, int ARB[])
  10. {
  11. if(st == dr)
  12. {
  13. ARB[nod] = val;
  14. return;
  15. }
  16. int m = (st + dr) / 2;
  17. if(poz <= m) Update(2 * nod, st, m, ARB);
  18. else Update(2 * nod + 1,m + 1, dr, ARB);
  19. ARB[nod] = Min(ARB[2 * nod], ARB[2 * nod + 1]);
  20. }
  21. void Interog (int nod, int st, int dr, int ARB[])
  22. {
  23. if(start<=st && dr<=sfarsit)
  24. {
  25. if(Minim > ARB[nod]) {
  26. Minim = ARB[nod];
  27. }
  28. return;
  29. }
  30. int m = (st + dr) / 2;
  31. if(start <= m) Interog(2 * nod, st, m, ARB);
  32. if(m < sfarsit) Interog(2 * nod +1 ,m + 1, dr, ARB);
  33. }
  34. int main()
  35. {
  36. cin >> N;
  37.  
  38. for (int i = 1; i <= N; i++) {
  39. cin >> x;
  40. val = x;
  41. if (i % 2 == 0) {
  42. poz = i / 2;
  43. Update(1, 1, N / 2, pare);
  44. } else {
  45. poz = i / 2 + 1;
  46. Update(1, 1, N / 2, impare);
  47. }
  48. app_on_pos[x] = poz;
  49. }
  50.  
  51. for (int i = 1; i <= N / 2; i++) {
  52. Minim = INF;
  53. start = 1;
  54. sfarsit = N / 2;
  55.  
  56. Interog(1, 1, N / 2, impare);
  57. Minim1 = Minim;
  58.  
  59. Minim = INF;
  60. start = app_on_pos[Minim1];
  61. sfarsit = N / 2;
  62. Interog(1, 1, N / 2, pare);
  63. Minim2 = Minim;
  64.  
  65. cout << Minim1 << ' ' << Minim2 << ' ';
  66.  
  67. poz = app_on_pos[Minim1]; val = INF;
  68. Update(1, 1, N / 2, impare);
  69.  
  70. poz = app_on_pos[Minim2]; val = INF;
  71. Update(1, 1, N / 2, pare);
  72. }
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement