Egor and an RPG game

One Saturday afternoon Egor was playing his favorite RPG game. While discovering new lands and territories, he came across the following sign:

Egor is a passionate player, but he is an algorithmician as well. That’s why he instantly spotted four common letters in two words on the sign above — if we permute the letters “R”, “E”, “G”, “O” from the first word, we can obtain the letters “O”, “G”, “R”, “E”. Egor got inspired by the sign and right away he came up with a problem about permutations.

You are given a permutation of length $$$n$$$. You have to split it into some non-empty subsequences so that each element of the permutation belongs to exactly one subsequence. Each subsequence must be monotonic — that is, either increasing or decreasing.

Sequence is called to be a subsequence if it can be derived from permutation by deleting some (possibly none) elements without changing the order of the remaining elements.

The number of subsequences should be small enough — let $$$f(n)$$$ be the minimum integer $$$k$$$ such that every permutation of length $$$n$$$ can be partitioned into at most $$$k$$$ monotonic subsequences.

You need to split the permutation into at most $$$f(n)$$$ monotonic subsequences.

Input

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases.

You can only use $$$t = 1$$$ in hacks.

Next, descriptions of $$$t$$$ test cases come, each of them in the following format.

The first line of a single test case contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the length of the permutation. The second line contains $$$n$$$ distinct integers $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) — the permutation itself.

The sum of the values of $$$n$$$ over all test cases doesn’t exceed $$$10^5$$$.

Output

For each test case print the answer in the following format:

In the first line print $$$k$$$ ($$$1 \leq k \leq f(n)$$$) — the number of the subsequences in the partition. In the next $$$k$$$ lines, print the descriptions of found subsequences. Each description should start with a number $$$l_i$$$ ($$$1 \leq l_i \leq n$$$) — the length of the corresponding subsequence, followed by $$$l_i$$$ integers — the values of this subsequence in the order in which they occur in the permutation.

Each subsequence you output must be either increasing or decreasing.

In case there are multiple possible answers, print any of them.

Example

input

3
4
4 3 1 2
6
4 5 6 1 3 2
10
1 2 3 4 5 6 7 8 9 10

output

2
3 4 3 1
1 2
3
2 4 1
2 5 6
2 3 2
1
10 1 2 3 4 5 6 7 8 9 10

Note

In the example, we can split:

  • $$$[4, 3, 1, 2]$$$ into $$$[4, 3, 1]$$$, $$$[2]$$$
  • $$$[4, 5, 6, 1, 3, 2]$$$ into $$$[4, 1]$$$, $$$[5, 6]$$$ and $$$[3, 2]$$$
  • $$$[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]$$$ into $$$[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]$$$

Surely, there are many more answers possible.

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#undef _GLIBCXX_DEBUG

#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  const int MAX = 100010;
  vector<int> pre(1, 0);
  for (int i = 1; (int) pre.size() < MAX; i++) {
    for (int j = 0; j < i + 1; j++) {
      pre.push_back(i);
    }
  }
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<int> p(n);
    vector<int> pos(n, -1);
    for (int i = 0; i < n; i++) {
      cin >> p[i];
      p[i]--;
      pos[p[i]] = i;
    }
    vector<int> alive(n, 1);
    vector<vector<int>> res;
    vector<int> dp(n);
    vector<int> pr(n);
    int left = n;
    while (left > 0) {
      int found = 0;
      for (int rot = 0; rot < 2; rot++) {
        vector<vector<int>> seq;
        vector<int> u;
        for (int i = 0; i < n; i++) {
          if (!alive[i]) {
            continue;
          }
          int x = p[i];
          auto it = (rot == 0 ? lower_bound(u.begin(), u.end(), x, less<int>()) : lower_bound(u.begin(), u.end(), x, greater<int>()));
          dp[i] = (int) (it - u.begin()) + 1;
          pr[i] = (it == u.begin() ? -1 : u[dp[i] - 2]);
          if (it == u.end()) {
            seq.emplace_back(1, x);
            u.push_back(x);
          } else {
            seq[(int) (it - u.begin())].push_back(x);
            *it = x;
          }
        }
        if (pre[left - (int) u.size()] + 1 <= pre[left]) {
          found = 1;
          vector<int> who;
          for (int i = 0; i < n; i++) {
            if (alive[i] && dp[i] == (int) u.size()) {
              int j = i;
              while (true) {
                who.push_back(p[j]);
                if (pr[j] == -1) {
                  break;
                }
                j = pos[pr[j]];
              }
              reverse(who.begin(), who.end());
              assert((int) who.size() == (int) u.size());
              break;
            }
          }
          res.push_back(who);
          for (int x : who) {
            alive[pos[x]] = 0;
            left--;
          }
          continue;
        }
        vector<int> order(u.size());
        iota(order.begin(), order.end(), 0);
        sort(order.begin(), order.end(), [&](int i, int j) {
          return (seq[i].size() > seq[j].size());
        });
//        debug(seq);
        int sum = 0;
        for (int i = 0; i < (int) u.size(); i++) {
          sum += (int) seq[order[i]].size();
          if (pre[left - sum] + (i + 1) <= pre[left]) {
            found = 1;
            for (int j = 0; j <= i; j++) {
              res.push_back(seq[order[j]]);
              for (int x : seq[order[j]]) {
                alive[pos[x]] = 0;
                left--;
              }
            }
            break;
          }
        }
        if (found) {
          break;
        }
      }
      if (!found) debug(p);
      assert(found);
    }
    cout << (int) res.size() << '\n';
    for (auto& v : res) {
      cout << (int) v.size();
      for (int i : v) {
        cout << " " << i + 1;
      }
      cout << '\n';
    }
  }
  return 0;
}

Be the first to comment

Leave a Reply

Your email address will not be published.


*