fork download
  1. #include <cstring>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <iostream>
  7. #include <fstream>
  8. #include <queue>
  9.  
  10. #define rep(i, l, r) for(int i = l; i <= r; i++)
  11. #define down(i, l, r) for(int i = l; i >= r; i--)
  12. #define MS 12345
  13. #define MAX 1037471823
  14.  
  15. using namespace std;
  16.  
  17. int m[MS], n, l, r, c, k[MS], t[MS], a, b[MS], q;
  18.  
  19. void DFS(int x, int o)
  20. {
  21. if (x != 0) { if (o != 1) printf("%d ", m[x]); else printf("%d\n", m[x]); }
  22. o--; if (o == 0) return;
  23. rep(i, x+1, n) if (t[i] >= o && m[i] > m[x]) { DFS(i, o); return; }
  24. if (x == 0) printf("Impossible\n");
  25. }
  26.  
  27. int main()
  28. {
  29. scanf("%d", &n);
  30. rep(i, 1, n) scanf("%d", &m[i]); k[0] = MAX; c = 0;
  31. down(i, n, 1)
  32. {
  33. l = 0, r = c;
  34. while (r > l)
  35. {
  36. if (m[i] < k[(l+r+1)/2]) l = (l+r+1)/2; else r = (l+r+1)/2-1;
  37. }
  38. t[i] = l+1; k[t[i]] = max(k[t[i]], m[i]); if (l+1 > c) c++;
  39. }
  40. scanf("%d", &q); m[0] = -MAX;
  41. rep(i, 1, q)
  42. {
  43. scanf("%d", &a);
  44. DFS(0, a+1);
  45. }
  46. return 0;
  47. }
Success #stdin #stdout 0s 3492KB
stdin
20
94 38 46 27 66 18 34 95 86 27 34 69 18 95 67 96 86 56 98 99
10
1
2
3
4
5
6
7
8
9
10
stdout
94
94 95
94 95 96
94 95 96 98
94 95 96 98 99
38 46 66 95 96 98
38 46 66 95 96 98 99
38 46 66 86 95 96 98 99
Impossible
Impossible