Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define NMax 100100
- #define Min(a, b) (a < b) ? a : b
- #define INF 0x3f3f3f3f
- using namespace std;
- 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];
- void Update (int nod, int st, int dr, int ARB[])
- {
- if(st == dr)
- {
- ARB[nod] = val;
- return;
- }
- int m = (st + dr) / 2;
- if(poz <= m) Update(2 * nod, st, m, ARB);
- else Update(2 * nod + 1,m + 1, dr, ARB);
- ARB[nod] = Min(ARB[2 * nod], ARB[2 * nod + 1]);
- }
- void Interog (int nod, int st, int dr, int ARB[])
- {
- if(start<=st && dr<=sfarsit)
- {
- if(Minim > ARB[nod]) {
- Minim = ARB[nod];
- }
- return;
- }
- int m = (st + dr) / 2;
- if(start <= m) Interog(2 * nod, st, m, ARB);
- if(m < sfarsit) Interog(2 * nod +1 ,m + 1, dr, ARB);
- }
- int main()
- {
- cin >> N;
- for (int i = 1; i <= N; i++) {
- cin >> x;
- val = x;
- if (i % 2 == 0) {
- poz = i / 2;
- Update(1, 1, N / 2, pare);
- } else {
- poz = i / 2 + 1;
- Update(1, 1, N / 2, impare);
- }
- app_on_pos[x] = poz;
- }
- for (int i = 1; i <= N / 2; i++) {
- Minim = INF;
- start = 1;
- sfarsit = N / 2;
- Interog(1, 1, N / 2, impare);
- Minim1 = Minim;
- Minim = INF;
- start = app_on_pos[Minim1];
- sfarsit = N / 2;
- Interog(1, 1, N / 2, pare);
- Minim2 = Minim;
- cout << Minim1 << ' ' << Minim2 << ' ';
- poz = app_on_pos[Minim1]; val = INF;
- Update(1, 1, N / 2, impare);
- poz = app_on_pos[Minim2]; val = INF;
- Update(1, 1, N / 2, pare);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement