private void Scan() { N = int.Parse(Console.ReadLine()); var line = Console.ReadLine().Split(' '); Pq = new PQ(); I[] kk = new I[N]; I[] gg = new I[N]; for (int i = 0; i < N; i++) { int a = int.Parse(line[i]); if (i % 2 == 1) { kk[i] = new I(a, i); gg[i] = new I(int.MaxValue, -1); } else { gg[i] = new I(a, i); kk[i] = new I(int.MaxValue, -1); } } G = new SegTree(gg); K = new SegTree(kk); Pq.Push(new S(0, N, G.Q(0, N))); }
public void Solve() { Scan(); Anser = new int[N]; int cnt = 0; while (Pq.Length > 0) { S top = Pq.Pop(); int left = top.Left; int right = top.Right; I min = top.Min; I rightMin; if (left % 2 == 0) { rightMin = K.Q(min.Index, right); S a = new S(left, min.Index, G.Q(left, min.Index)); S b = new S(min.Index + 1, rightMin.Index, K.Q((min.Index + 1), rightMin.Index)); S c = new S(rightMin.Index + 1, right, G.Q((rightMin.Index + 1), right)); Pq.Push(a); Pq.Push(b); Pq.Push(c); } else { rightMin = G.Q(min.Index, right); S a = new S(left, min.Index, K.Q(left, min.Index)); S b = new S(min.Index + 1, rightMin.Index, G.Q((min.Index + 1), rightMin.Index)); S c = new S(rightMin.Index + 1, right, K.Q((rightMin.Index + 1), right)); Pq.Push(a); Pq.Push(b); Pq.Push(c); } Anser[cnt] = min.Value; Anser[cnt + 1] = rightMin.Value; cnt += 2; } Console.WriteLine(string.Join(" ", Anser)); }