name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int fail() { printf("-1\n"); return 0; } const int MN = 2e5 + 100; const int MS = MN; int N, S, a[MN], v[MN], g[MN], V, f; using i2 = array<int, 2>; i2 b[MN]; using vi2 = vector<i2>; vi2 w[MN]; bool u[MN]; using vi = vector<int>; vi c[MN]; int C; void dfs(int n, int p = -1) { u[n] = true; for (; not w[n].empty();) { i2 x = w[n].back(); w[n].pop_back(); dfs(x[1], x[0]); } if (p != -1) c[C].push_back(p); } void solve(int n) { c[C].clear(); dfs(n); assert(not c[C].empty()); C++; } void cmb(vi* s, vi* e) { int z = 0; for (vi* t = s; t != e; t++) z += t->size(); printf("%d\n", z); for (vi* t = s; t != e; t++) for (int i = t->size() - 1; i >= 0; i--) printf("%d ", t->at(i) + 1); printf("\n%ld\n", e - s); for (vi* t = e;;) { if (t == s) break; t--; printf("%d ", t->back() + 1); } printf("\n"); } int main() { scanf("%d%d", &N, &S); for (int i = 0; i < N; i++) scanf("%d", a + i), v[i] = a[i]; sort(v, v + N); V = -1; for (int i = 0; i < N; i++) { if (not i or v[i] != v[i - 1]) b[++V][0] = i; v[V] = v[i], g[i] = V; b[V][1] = i + 1; } V++; f = 0; for (int i = 0; i < N; i++) f += i < b[a[i] = lower_bound(v, v + V, a[i]) - v][0] or b[a[i]][1] <= i; if (f > S) return fail(); for (int i = 0; i < N; i++) if (a[i] != g[i]) w[g[i]].push_back({i, a[i]}); for (int i = 0; i < V; i++) u[i] = false; C = 0; for (int i = 0; i < V; i++) if (not u[i] and not w[i].empty()) solve(i); int x = min(S - f, C); if (x < 2) x = 0; printf("%d\n", C + (x ? 2 - x : 0)); if (x) cmb(c, c + x); for (int i = x; i < C; i++) { printf("%lu\n", c[i].size()); for (int j = c[i].size() - 1; j >= 0; j--) printf("%d ", c[i][j] + 1); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxN = 202000; int n, S, ufs[maxN], Id[maxN], vis[maxN]; pair<int, int> A[maxN]; vector<int> Ring[maxN]; int find(int x); void dfs(int rcnt, int u); int main() { scanf("%d%d", &n, &S); for (int i = 1; i <= n; i++) scanf("%d", &A[i].first), A[i].second = ufs[i] = i; sort(&A[1], &A[n + 1]); for (int i = 1; i <= n; i++) Id[A[i].second] = i; for (int i = 1; i <= n; i++) if (A[i].first == A[Id[i]].first && i != Id[i]) { int x = A[i].second, y = Id[i]; A[y].second = x; Id[x] = y; Id[i] = A[i].second = i; } int sum = 0, rcnt = 0; for (int i = 1; i <= n; i++) ufs[find(i)] = find(Id[i]); for (int i = 1, lst = 0; i <= n; i++) if (Id[i] != i) { ++sum; if (lst && A[lst].first == A[i].first && find(A[lst].second) != find(A[i].second)) { ufs[find(A[lst].second)] = find(A[i].second); swap(Id[A[lst].second], Id[A[i].second]); } lst = i; } for (int i = 1; i <= n; i++) if (Id[i] != i && !vis[i]) dfs(++rcnt, i); if (sum > S) { puts("-1"); return 0; } int trn = min(S - sum, rcnt); if (trn <= 2) { printf("%d\n", rcnt); for (int i = 1, sz; i <= rcnt; i++) { printf("%d\n", sz = Ring[i].size()); for (int j = 0; j < sz; j++) printf("%d ", Ring[i][j]); printf("\n"); } } else { printf("%d\n", rcnt - (trn - 2)); for (int i = 1, sz; i <= rcnt - trn; i++) { printf("%d\n", sz = Ring[i].size()); for (int j = 0; j < sz; j++) printf("%d ", Ring[i][j]); printf("\n"); } int sum = 0; for (int i = rcnt - trn + 1; i <= rcnt; i++) sum += Ring[i].size(); printf("%d\n", sum); for (int i = rcnt - trn + 1; i <= rcnt; i++) for (int j = 0, sz = Ring[i].size(); j < sz; j++) printf("%d ", Ring[i][j]); printf("\n"); printf("%d\n", trn); for (int i = rcnt; i >= rcnt - trn + 1; i--) printf("%d ", Ring[i][0]); printf("\n"); } return 0; } int find(int x) { if (ufs[x] != x) ufs[x] = find(ufs[x]); return ufs[x]; } void dfs(int rcnt, int u) { if (vis[u]) return; Ring[rcnt].push_back(u); vis[u] = 1; dfs(rcnt, Id[u]); return; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct node { int id, x; } a[202020], b[202020]; int n, S, doe, cnt; int fa[202020], size[202020], r[202020], id[202020]; vector<int> col[202020]; bool br[202020]; bool cmp_id(node u, node w) { return u.id < w.id; } bool cmp_x(node u, node w) { return u.x < w.x; } int find(int x) { if (fa[x] == x) return x; fa[x] = find(fa[x]); return fa[x]; } void merge(int x, int y) { x = find(x); y = find(y); if (x != y) { fa[y] = x; size[x] += size[y]; } } void init() { sort(a + 1, a + 1 + n, cmp_x); for (int i = 1; i <= n; ++i) b[i] = a[i]; b[1].x = 1; for (int i = 2; i <= n; ++i) { if (a[i].x == a[i - 1].x) b[i].x = b[i - 1].x; else b[i].x = b[i - 1].x + 1; } for (int i = 1; i <= n; ++i) a[i].x = b[i].x, id[a[i].id] = i; sort(a + 1, a + 1 + n, cmp_id); } void together(int k) { int len = col[k].size(), x = col[k][0]; for (int i = 1; i < len; ++i) { int y = col[k][i]; if (find(x) != find(y)) { swap(r[x], r[y]); merge(x, y); } x = y; } } void write(int x) { if (br[x]) return; int y = x; while (1) { printf("%d ", y); --doe; y = r[y]; if (y == x) return; } } int main() { scanf("%d%d", &n, &S); for (int i = 1; i <= n; ++i) fa[i] = i, r[i] = i, size[i] = 1; for (int i = 1; i <= n; ++i) scanf("%d", &a[i].x), a[i].id = i; init(); for (int i = 1; i <= n; ++i) if (a[i].x == b[i].x && a[i].id != b[i].id) { int x = i, y = id[i]; swap(id[b[x].id], id[b[y].id]); swap(b[x], b[y]); } for (int i = 1; i <= n; ++i) if (a[i].x != b[i].x) { ++doe; int x = a[i].id, y = b[i].id; r[y] = x; merge(x, y); } for (int i = 1; i <= n; ++i) if (size[find(i)] != 1) col[a[i].x].push_back(i); for (int i = 1; i <= n; ++i) if (col[i].size() > 1) together(i); for (int i = 1; i <= n; ++i) if (find(i) == i && size[i] != 1) ++cnt; if (doe > S) { printf("-1\n"); return 0; } if (cnt == 0) { printf("0\n"); return 0; } if (cnt == 1) { printf("1\n"); printf("%d\n", doe); for (int i = 1; i <= n; ++i) if (find(i) == i && size[i] != 1) write(i); printf("\n"); return 0; } if (doe + cnt <= S) { if (cnt == 1) printf("1\n"); else printf("2\n"); } else { if (S - doe == 1) printf("%d\n", cnt); else if (doe == S) printf("%d\n", cnt); else printf("%d\n", (doe + cnt - S) + 2); } int kkk = doe + cnt - S; for (int i = 1; i <= n && kkk > 0; ++i) if (find(i) == i && size[i] != 1) { printf("%d\n", size[i]); write(i); printf("\n"); br[i] = 1; --kkk; --cnt; } if (cnt == 0) return 0; printf("%d\n", doe); for (int i = 1; i <= n; ++i) if (find(i) == i && br[i] == 0 && size[i] != 1) write(i); printf("\n"); if (cnt == 1) return 0; printf("%d\n", cnt); for (int i = n; i >= 1; --i) if (find(i) == i && br[i] == 0 && size[i] != 1) printf("%d ", i); printf("\n"); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using pii = pair<int, int>; int n, s; int a[200010], b[200010]; int p[200010]; bool used[200010]; int fth[200010]; vector<vector<int>> cycles; map<int, vector<int>> v, pos; int root(int x) { return fth[x] == -1 ? x : fth[x] = root(fth[x]); } bool join(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if ((x + y) & 1) fth[x] = y; else fth[y] = x; return true; } void getMinPerm() { int i; for (i = 1; i <= n; ++i) b[i] = a[i]; sort(b + 1, b + n + 1); for (i = 1; i <= n; ++i) if (a[i] != b[i]) v[b[i]].push_back(i); for (i = 1; i <= n; ++i) { if (a[i] == b[i]) p[i] = i; else { p[i] = v[a[i]].back(); v[a[i]].pop_back(); } } for (i = 1; i <= n; ++i) join(i, p[i]); for (i = 1; i <= n; ++i) if (a[i] != b[i]) pos[a[i]].push_back(i); for (const auto &item : pos) { for (auto val : item.second) if (join(p[val], p[item.second[0]])) { swap(p[val], p[item.second[0]]); } } } void solve() { int i, t; for (t = 0, i = 1; i <= n; ++i) { if (p[i] == i) continue; if (used[i]) continue; cycles.push_back(vector<int>()); for (int j = i; !used[j]; used[j] = true, j = p[j]) cycles.back().push_back(j); t += cycles.back().size(); } int m = cycles.size(); int x = max(m + t - s, 0); int y = m - x; if (t > s) return void(cout << "-1\n"); cout << (x + min(y, 2)) << '\n'; if (y == 1) ++x; while (x--) { cout << cycles.back().size() << '\n'; for (auto val : cycles.back()) cout << val << ' '; cout << '\n'; cycles.pop_back(); } if (cycles.empty()) return; int sum = 0; for (i = cycles.size() - 1; i >= 0; --i) sum += cycles[i].size(); cout << '\n'; cout << sum << '\n'; for (const auto &vec : cycles) { for (auto val : vec) cout << val << ' '; } cout << '\n'; cout << cycles.size() << '\n'; for (i = cycles.size() - 1; i >= 0; --i) cout << cycles[i][0] << ' '; cout << '\n'; } int main() { ios_base::sync_with_stdio(false); int i; memset(fth, -1, sizeof fth); cin >> n >> s; for (i = 1; i <= n; ++i) cin >> a[i]; getMinPerm(); solve(); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int p[200005], rk[200005]; vector<int> g[200005]; int m; int a[200005]; int b[200005]; int uq[200005]; bool used[200005]; vector<int> cycles[200005]; void dfs(int u) { while (!g[u].empty()) { int i = g[u].back(); g[u].pop_back(); dfs(a[i]); cycles[m].push_back(i); } } int main() { int n, s; cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } sort(b, b + n); int k = n; for (int i = 0; i < n; i++) uq[i] = b[i]; k = unique(uq, uq + n) - uq; for (int i = 0; i < n; i++) { a[i] = lower_bound(uq, uq + k, a[i]) - uq; b[i] = lower_bound(uq, uq + k, b[i]) - uq; } for (int i = 0; i < n; i++) { if (a[i] == b[i]) continue; g[b[i]].push_back(i); } for (int i = 0; i < k; i++) { dfs(i); if (!cycles[m].empty()) m++; } for (int i = 0; i < m; i++) { s -= cycles[i].size(); } if (s < 0) { cout << "-1"; return 0; } for (int i = 0; i < n; i++) p[i] = i; for (int id = 0; id < m; id++) { for (int i = 0; i < (int)cycles[id].size(); i++) { int v = cycles[id][i], u = cycles[id][(i + 1) % (int)cycles[id].size()]; p[u] = v; } } s = min(s, m); if (s > 1) { cout << 2 + m - s << endl; cout << s << endl; for (int i = 0; i < s; i++) { cout << cycles[i][0] + 1 << " "; } cout << endl; for (int i = s - 1; i > 0; i--) swap(p[cycles[i][0]], p[cycles[i - 1][0]]); } else cout << m << endl; for (int i = 0; i < n; i++) { if (used[i]) continue; if (p[i] == i) { used[i] = 1; continue; } vector<int> cur; int x = i; while (!used[x]) { cur.push_back(x); used[x] = 1; x = p[x]; } printf("%d\n", (int)cur.size()); for (int z : cur) printf("%d ", z + 1); printf("\n"); } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using int64 = long long int; struct Edge { int from; int to; int to_index; }; class Graph { public: explicit Graph(const int n) : edges_(n) {} void AddEdge(Edge e) { edges_[e.from].push_back(e); } vector<vector<int>> FindCycles() { current_edge_.assign(edges_.size(), 0); vector<vector<int>> result; for (size_t i = 0; i < edges_.size(); ++i) { Dfs(i); if (!cycle_.empty()) { result.push_back(move(cycle_)); cycle_.clear(); } } return result; } private: void Dfs(const int node) { while (current_edge_[node] < edges_[node].size()) { const Edge& e = edges_[node][current_edge_[node]++]; Dfs(e.to); cycle_.push_back(e.to_index); } } vector<vector<Edge>> edges_; vector<size_t> current_edge_; vector<int> cycle_; }; void solve() { int n, s; scanf("%d %d", &n, &s); vector<pair<int, int>> a(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i].first); a[i].second = i; } sort(a.begin(), a.end()); vector<int> group(n); int current_group = -1; for (int i = 0; i < n; ++i) { if (i == 0 || a[i].first != a[i - 1].first) { ++current_group; } group[i] = current_group; } Graph graph(current_group + 1); for (int i = 0; i < n; ++i) { const int gi = group[i]; const int gj = group[a[i].second]; if (gi != gj) { graph.AddEdge({gi, gj, a[i].second}); } } vector<vector<int>> cycles = graph.FindCycles(); int cycles_size = 0; for (const vector<int>& cycle : cycles) { cycles_size += cycle.size(); } if (s < cycles_size) { cout << -1 << "\n"; return; } if (s >= cycles_size + 2 && cycles.size() >= 2) { const int num_cycles = cycles.size(); const int cycles_to_merge = min(s - cycles_size, num_cycles); vector<int> merged_cycle; for (int i = num_cycles - cycles_to_merge; i < num_cycles; ++i) { merged_cycle.insert(merged_cycle.end(), cycles[i].begin(), cycles[i].end()); } vector<int> additional_cycle; for (int i = num_cycles - 1; i >= num_cycles - cycles_to_merge; --i) { additional_cycle.push_back(cycles[i][0]); } cycles.resize(cycles.size() - cycles_to_merge); cycles.push_back(move(merged_cycle)); cycles.push_back(move(additional_cycle)); } cout << cycles.size() << "\n"; for (const auto& cycle : cycles) { cout << cycle.size() << "\n"; for (const int node : cycle) { cout << (node + 1) << " "; } cout << "\n"; } } int main() { solve(); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200100; int b[N], no[N], n0, n, sum, cnt = 0, to[N], vis[N], ran[N]; vector<int> vec[N]; struct nd { int id, vl; } a[N]; struct edge { int to, id; }; vector<edge> e[N]; bool cmp(nd x, nd y) { return x.vl < y.vl; } void init() { int i; for (i = 1; i <= n0; i++) if (b[i] == a[i].vl) no[i] = 1; n = 0; for (i = 1; i <= n0; i++) if (!no[i]) { a[++n].id = n; a[n].vl = a[i].vl; to[n] = i; } } void go0() { int i, l; printf("1\n"); l = vec[1].size(); printf("%d\n", l); for (i = 0; i < l; i++) printf("%d ", to[vec[1][i]]); printf("\n"); } void go1() { int i, j, l; printf("%d\n", cnt); for (i = 1; i <= cnt; i++) { l = vec[i].size(); printf("%d\n", l); for (j = 0; j < l; j++) printf("%d ", to[vec[i][j]]); printf("\n"); } } void go2() { int i, j, l; printf("2\n"); printf("%d\n", n); for (i = 1; i <= cnt; i++) { l = vec[i].size(); for (j = 0; j < l; j++) printf("%d ", to[vec[i][j]]); } printf("\n"); printf("%d\n", cnt); for (i = cnt; i; i--) printf("%d ", to[vec[i][0]]); printf("\n"); } void go3() { int i, j, l; printf("%d\n", cnt - (sum - n) + 2); l = 0; for (i = 1; i <= sum - n; i++) l += vec[i].size(); printf("%d\n", l); for (i = 1; i <= sum - n; i++) { l = vec[i].size(); for (j = 0; j < l; j++) printf("%d ", to[vec[i][j]]); } printf("\n"); printf("%d\n", sum - n); for (i = sum - n; i; i--) printf("%d ", to[vec[i][0]]); printf("\n"); for (i = sum - n + 1; i <= cnt; i++) { l = vec[i].size(); printf("%d\n", l); for (j = 0; j < l; j++) printf("%d ", to[vec[i][j]]); printf("\n"); } } void euler(int x) { edge i; vis[x] = 1; while (!e[x].empty()) { i = e[x][e[x].size() - 1]; e[x].pop_back(); euler(i.to); vec[cnt].push_back(i.id); } } int main() { int i, j, k, l; scanf("%d%d", &n0, &sum); for (i = 1; i <= n0; i++) { scanf("%d", &a[i].vl); b[i] = a[i].vl; } sort(b + 1, b + n0 + 1); init(); if (!n) { printf("0\n"); return 0; } if (sum < n) { printf("-1\n"); return 0; } sort(a + 1, a + n + 1, cmp); int num = 0; for (i = 1; i <= n; i = j + 1) { num++; j = i; while ((j < n) && (a[j + 1].vl == a[i].vl)) j++; for (k = i; k <= j; k++) ran[k] = num; } for (i = 1; i <= n; i++) e[ran[a[i].id]].push_back((edge){ran[i], a[i].id}); for (i = 1; i <= num; i++) if (!vis[i]) { ++cnt; euler(i); } for (i = 1; i <= cnt; i++) { for (j = 0; j < vec[i].size(); j++) b[j] = vec[i][j]; reverse(b, b + vec[i].size()); for (j = 0; j < vec[i].size(); j++) vec[i][j] = b[j]; } if (cnt == 1) go0(); else if (sum - n <= 2) go1(); else if (n + cnt <= sum) go2(); else go3(); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long read() { long long x = 0, F = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') F = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * F; } int n, cnt, s, F, tmp; int a[500000 + 5], b[500000 + 5], val[500000 + 5]; int head[500000 + 5], ecnt; struct edge { int to, nxt, vis; } e[500000 + 5]; vector<int> P[500000 + 5]; void link(int u, int v) { e[++ecnt] = (edge){v, head[u]}, head[u] = ecnt; } void dfs(int x) { for (int &i = head[x]; i;) { int v = e[i].to; i = e[i].nxt; dfs(a[v]); P[cnt].push_back(v); } } void Print(int id) { printf("%d\n", P[id].size()); for (int i = P[id].size() - 1; i >= 0; i--) printf("%d ", P[id][i]); puts(""); } int main() { n = read(), s = read(); for (int i = 1; i <= n; i++) val[i] = a[i] = read(); sort(val + 1, val + n + 1); int pn = unique(val + 1, val + n + 1) - val - 1; for (int i = 1; i <= n; i++) b[i] = a[i] = lower_bound(val + 1, val + pn + 1, a[i]) - val; sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) if (b[i] != a[i]) link(b[i], i), s--, F = 1; if (!F) { puts("0"); return 0; } else if (s < 0) { puts("-1"); return 0; } for (int i = 1; i <= pn; i++) if (head[i]) cnt++, dfs(i); if (cnt == 1) { puts("1"); Print(1); return 0; } s = min(s, cnt); if (s > 1) printf("%d\n", cnt - s + 2); else printf("%d\n", cnt); for (int i = cnt; i > s; i--) Print(i); if (s) { int tot = 0; for (int i = 1; i <= s; i++) tot += P[i].size(); printf("%d\n", tot); for (int i = s; i >= 1; i--) for (int j = P[i].size() - 1; j >= 0; j--) printf("%d ", P[i][j]); puts(""); if (s > 1) { printf("%d\n", s); for (int i = 1; i <= s; i++) printf("%d ", P[i][P[i].size() - 1]); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, true : false; } template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, true : false; } int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, s; int a[200005], f[200005], to[200005]; pair<int, int> b[200005]; bool vis[200005]; vector<vector<int> > ans; int getf(int x) { return x == f[x] ? x : f[x] = getf(f[x]); } int main() { n = readint(); s = readint(); for (int i = 1; i <= n; i++) a[i] = readint(), b[i] = make_pair(a[i], i); sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) to[b[i].second] = i; for (int i = 1; i <= n; i++) while (i != to[i] && a[to[i]] == b[to[i]].first) swap(b[to[i]].second, b[to[to[i]]].second), swap(to[to[i]], to[i]); for (int i = 1; i <= n; i++) f[i] = i; int all = n, cnt = 0; for (int i = 1; i <= n; i++) { if (i == to[i]) all--; if (vis[i] || i == to[i]) continue; vis[i] = 1; cnt++; for (int j = to[i]; j != i; j = to[j]) vis[j] = 1, f[j] = i; } int lst = 0; for (int i = 1; i <= n; i++) { if (b[i].second == i) continue; if (!lst || b[i].first != b[lst].first) { lst = i; continue; } if (getf(b[lst].second) != getf(b[i].second)) { f[f[b[i].second]] = f[b[lst].second]; swap(to[b[i].second], to[b[lst].second]); swap(b[i].second, b[lst].second); cnt--; } } if (all > s) return printf("-1\n"), 0; int tmp = s - all, num = 0; vector<int> now(0), rem(0); for (int i = 1; i <= n; i++) { if (i == to[i]) continue; if (i != f[i]) continue; now.push_back(i); for (int j = to[i]; j != i; j = to[j]) now.push_back(j); rem.push_back(i); num++; if (num >= tmp) { ans.push_back(now); reverse(rem.begin(), rem.end()); if (rem.size() > 1) ans.push_back(rem); now.clear(), rem.clear(); } } if (now.size()) ans.push_back(now); reverse(rem.begin(), rem.end()); if (rem.size() > 1) ans.push_back(rem); printf("%d\n", ans.size()); for (auto v : ans) { printf("%d\n", v.size()); for (auto x : v) printf("%d ", x); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T, class U> void ckmin(T &a, U b) { if (a > b) a = b; } template <class T, class U> void ckmax(T &a, U b) { if (a < b) a = b; } const int MAXN = 400013; int N, S, M, ans, n; int val[MAXN], arr[MAXN], sorted[MAXN]; vector<int> moves[MAXN]; vector<int> cyc[MAXN]; bitset<MAXN> vis; vector<int> compress; int freq[MAXN]; pair<int, int> range[MAXN]; vector<int> edge[MAXN]; vector<int> tour; int ind[MAXN]; int indexof(vector<int> &v, int x) { return upper_bound((v).begin(), (v).end(), x) - v.begin() - 1; } void dfs(int u) { while (ind[u] < ((int)(edge[u]).size())) { dfs(edge[u][ind[u]++]); } tour.push_back(u); } void solve() { int k = min(M, S - n); if (k <= 2) { ans = M; for (auto i = (0); i < (M); i++) { moves[i] = cyc[i]; } } else { ans = M - k + 2; for (auto i = (0); i < (M - k); i++) { moves[i] = cyc[i]; } for (auto i = (M - k); i < (M); i++) { moves[M - k].insert(moves[M - k].end(), (cyc[i]).begin(), (cyc[i]).end()); moves[M - k + 1].push_back(cyc[i][0]); } reverse((moves[M - k + 1]).begin(), (moves[M - k + 1]).end()); } } int32_t main() { cout << fixed << setprecision(12); cerr << fixed << setprecision(4); ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> S; for (auto i = (0); i < (N); i++) { cin >> val[i]; compress.push_back(val[i]); } sort((compress).begin(), (compress).end()); compress.erase(unique((compress).begin(), (compress).end()), compress.end()); for (auto i = (0); i < (N); i++) { val[i] = indexof(compress, val[i]); sorted[i] = val[i]; } sort(sorted, sorted + N); for (auto i = (0); i < (N); i++) { if (val[i] == sorted[i]) { vis[i] = true; arr[i] = i; continue; } edge[i].push_back(val[i] + N); edge[sorted[i] + N].push_back(i); } for (auto i = (0); i < (N); i++) { if (vis[i]) continue; tour.clear(); dfs(i); reverse((tour).begin(), (tour).end()); for (int j = 0; j + 2 < ((int)(tour).size()); j += 2) { int u = tour[j]; int v = tour[j + 2]; arr[u] = v; } tour.clear(); } vis.reset(); for (auto i = (0); i < (N); i++) { if (vis[i]) continue; if (arr[i] == i) { continue; } cyc[M].push_back(i); do { vis[cyc[M].back()] = true; cyc[M].push_back(arr[cyc[M].back()]); } while (cyc[M].back() != i); cyc[M].pop_back(); M++; } for (auto i = (0); i < (M); i++) { n += ((int)(cyc[i]).size()); } if (S < n) { cout << "-1\n"; return 0; } solve(); assert(ans == min(M, max(2, 2 + M - S + n))); cout << ans << '\n'; for (auto i = (0); i < (ans); i++) { cout << ((int)(moves[i]).size()) << '\n'; for (int x : moves[i]) { cout << x + 1 << " \n"[x == moves[i].back()]; } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; map<int, int> MP; struct node { int a, b; inline node() {} inline node(int a, int b) : a(a), b(b) {} bool operator<(node x) const { return a < x.a; } } nod[N]; int a[N], nxt[N], fa[N], rt[N], sz[N], b[N]; int find(int v) { return fa[v] == 0 ? v : fa[v] = find(fa[v]); } int main() { int n, m, i, t = 0, x, y, sum; scanf("%d%d", &n, &m); for (i = 1; i <= n; ++i) scanf("%d", &a[i]); for (i = 1; i <= n; ++i) b[i] = a[i]; sort(b + 1, b + n + 1); for (i = 1; i <= n; ++i) if (a[i] != b[i]) { a[++t] = a[i]; b[t] = i; nod[t] = node(a[t], t); } if (t > m) { printf("-1"); return 0; } if (t == 0) { printf("0"); return 0; } sort(nod + 1, nod + (n = t) + 1); for (i = 1; i <= n; ++i) nxt[nod[i].b] = i; for (i = 1; i <= n; ++i) if (fa[i] == 0) for (x = nxt[i]; x != i; x = nxt[x]) fa[x] = i; for (i = 1; i <= n; ++i) { t = MP[a[i]]; if (t != 0) { x = find(i); y = find(t); if (x != y) { fa[x] = y; swap(nxt[i], nxt[t]); } } MP[a[i]] = i; } t = 0; for (i = 1; i <= n; ++i) if (fa[i] == 0) rt[++t] = i; for (i = 1; i <= n; ++i) ++sz[find(i)]; if (t == 1 || m <= n + 2) { printf("%d", t); for (i = 1; i <= t; ++i) { x = rt[i]; printf("\n%d\n%d", sz[x], b[x]); for (y = nxt[x]; y != x; y = nxt[y]) printf(" %d", b[y]); } } else { m = min(m - n, t); sum = 0; for (i = 1; i <= m; ++i) sum += sz[rt[i]]; printf("%d\n%d\n", t - m + 2, sum); for (i = 1; i <= m; ++i) { x = rt[i]; printf("%d ", b[x]); for (y = nxt[x]; y != x; y = nxt[y]) printf("%d ", b[y]); } printf("\n%d\n", m); for (i = m; i >= 1; --i) printf("%d ", b[rt[i]]); for (i = m + 1; i <= t; ++i) { x = rt[i]; printf("\n%d\n%d", sz[x], b[x]); for (y = nxt[x]; y != x; y = nxt[y]) printf(" %d", b[y]); } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using int64 = long long int; struct Edge { int from; int to; int to_index; }; class Graph { public: explicit Graph(const int n) : edges_(n) {} void AddEdge(Edge e) { edges_[e.from].push_back(e); } vector<vector<int>> FindCycles() { vector<vector<int>> result; for (size_t i = 0; i < edges_.size(); ++i) { Dfs(i); if (!cycle_.empty()) { result.push_back(move(cycle_)); cycle_.clear(); } } return result; } private: void Dfs(const int node) { while (!edges_[node].empty()) { const Edge e = edges_[node].back(); edges_[node].pop_back(); Dfs(e.to); cycle_.push_back(e.to_index); } } vector<vector<Edge>> edges_; vector<int> cycle_; }; void solve() { int n, s; scanf("%d %d", &n, &s); vector<pair<int, int>> a(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i].first); a[i].second = i; } sort(a.begin(), a.end()); vector<int> group(n); int current_group = -1; for (int i = 0; i < n; ++i) { if (i == 0 || a[i].first != a[i - 1].first) { ++current_group; } group[i] = current_group; } Graph graph(current_group + 1); for (int i = 0; i < n; ++i) { const int gi = group[i]; const int gj = group[a[i].second]; if (gi != gj) { graph.AddEdge({gi, gj, a[i].second}); } } vector<vector<int>> cycles = graph.FindCycles(); int cycles_size = 0; for (const vector<int>& cycle : cycles) { cycles_size += cycle.size(); } if (s < cycles_size) { cout << -1 << "\n"; return; } if (s >= cycles_size + 2 && cycles.size() >= 2) { const int num_cycles = cycles.size(); const int cycles_to_merge = min(s - cycles_size, num_cycles); vector<int> merged_cycle; for (int i = num_cycles - cycles_to_merge; i < num_cycles; ++i) { merged_cycle.insert(merged_cycle.end(), cycles[i].begin(), cycles[i].end()); } vector<int> additional_cycle; for (int i = num_cycles - 1; i >= num_cycles - cycles_to_merge; --i) { additional_cycle.push_back(cycles[i][0]); } cycles.resize(cycles.size() - cycles_to_merge); cycles.push_back(move(merged_cycle)); cycles.push_back(move(additional_cycle)); } cout << cycles.size() << "\n"; for (const auto& cycle : cycles) { cout << cycle.size() << "\n"; for (const int node : cycle) { cout << (node + 1) << " "; } cout << "\n"; } } int main() { solve(); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; vector<int> circuit; vector<int> e[MAXN]; void calc(int v) { stack<int> s; while (true) { if (((int)e[v].size()) == 0) { circuit.push_back(v); if (!((int)s.size())) { break; } v = s.top(); s.pop(); } else { s.push(v); int u = e[v].back(); e[v].pop_back(); v = u; } } } int a[MAXN]; int b[MAXN]; int c[MAXN]; vector<int> comp; unordered_map<int, int> act; unordered_map<long long, vector<int> > pos; bool bio[MAXN]; void dfs(int v) { if (bio[v]) return; bio[v] = true; for (auto w : e[v]) { dfs(w); } } vector<vector<int> > ans; vector<vector<int> > sol; int s; void solve(vector<int> &start) { int uk = 0; for (auto x : start) { calc(x); uk += ((int)circuit.size()) - 1; reverse(circuit.begin(), circuit.end()); ans.push_back(circuit); circuit.clear(); } if (uk > s) { cout << -1; exit(0); } for (auto v : ans) { vector<int> nv; for (int i = 0; i < ((int)v.size()) - 1; ++i) { int x = v[i]; int y = v[i + 1]; assert(((int)pos[(long long)x * MAXN + y].size()) > 0); nv.push_back(pos[(long long)x * MAXN + y].back()); pos[(long long)x * MAXN + y].pop_back(); } sol.push_back(nv); } } int main() { int n; cin >> n >> s; for (int i = 0; i < n; ++i) { cin >> a[i]; comp.push_back(a[i]); } sort(comp.begin(), comp.end()); comp.erase(unique(comp.begin(), comp.end()), comp.end()); for (int i = 0; i < ((int)comp.size()); ++i) { act[comp[i]] = i; } for (int i = 0; i < n; ++i) { a[i] = act[a[i]]; b[i] = a[i]; } sort(b, b + n); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { e[b[i]].push_back(a[i]); pos[(long long)b[i] * MAXN + a[i]].push_back(i); } } vector<int> start; for (int i = 0; i < n; ++i) { if (!bio[b[i]] && ((int)e[b[i]].size())) { dfs(b[i]); start.push_back(b[i]); } } if (((int)start.size()) <= 2) { solve(start); } else { int uk = 0; for (auto x : start) { calc(x); uk += ((int)circuit.size()) - 1; circuit.clear(); } if (uk > s) { cout << -1; return 0; } for (int i = 0; i < n; ++i) { e[i].clear(); } for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { e[b[i]].push_back(a[i]); } } int can = s - uk; can = min(((int)start.size()), can); vector<int> out; for (int i = 0; i < can; ++i) { int x = start[((int)start.size()) - i - 1]; out.push_back(pos[(long long)x * MAXN + e[x].back()].back()); } if (((int)out.size()) > 1) { sol.push_back(out); } for (int i = 0; i < can - 1; ++i) start.pop_back(); for (int i = 0; i < n; ++i) { c[i] = a[i]; } for (int i = 0; i < n; ++i) e[i].clear(); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { pos[(long long)b[i] * MAXN + a[i]].pop_back(); } } for (int i = 0; i < ((int)out.size()); ++i) { c[out[(i + 1) % ((int)out.size())]] = a[out[i]]; } for (int i = 0; i < n; ++i) { if (c[i] != b[i]) { e[b[i]].push_back(c[i]); pos[(long long)b[i] * MAXN + c[i]].push_back(i); } } solve(start); } cout << ((int)sol.size()) << endl; for (auto v : sol) { cout << ((int)v.size()) << "\n"; for (auto x : v) cout << x + 1 << " "; cout << "\n"; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, s, a[200010]; map<int, int> mp1; map<int, vector<int> > mp2; vector<vector<int> > ans; void dfs(int u) { while (!mp2[u].empty()) { int i = mp2[u].back(); mp2[u].pop_back(); dfs(a[i]); ans.back().push_back(i); } mp2.erase(u); } int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) scanf("%d", a + i), ++mp1[a[i]]; int j = 1; for (auto it = mp1.begin(); it != mp1.end(); j += it->second, ++it) for (int i = j; i < j + it->second; i++) if (a[i] != it->first) mp2[it->first].push_back(i); while (!mp2.empty()) { ans.push_back(vector<int>()); dfs(mp2.begin()->first); reverse(ans.back().begin(), ans.back().end()); s -= ans.back().size(); } if (s < 0) return puts("-1"), 0; int b = min(s, (int)ans.size()), d = 0; printf("%d\n", ans.size() - (b >= 3 ? b - 2 : 0)); if (b >= 3) { for (int i = 0; i < b; i++) d += ans[i].size(); printf("%d\n", d); for (int i = 0; i < b; i++) for (auto c : ans[i]) printf("%d ", c); printf("\n%d\n", b); for (int i = b - 1; i + 1; i--) printf("%d ", ans[i][0]); printf("\n"); } for (int i = (b >= 3 ? b : 0); i < ans.size(); i++) { printf("%d\n", ans[i].size()); for (auto c : ans[i]) printf("%d ", c); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, S; int a[N], p[N], q[N]; vector<vector<int>> cir; int uf[N]; int find(int x) { return (uf[x] == x) ? (x) : (uf[x] = find(uf[x])); } void adjust() { for (int i = 1; i <= n; i++) { uf[i] = i; } pair<int, int>* b = new pair<int, int>[(n + 1)]; for (int i = 1; i <= n; i++) b[i] = pair<int, int>(a[i], i); sort(b + 1, b + n + 1); for (int i = 1, j = 1; i <= n; i++, i = j) { while (j <= n && b[i].first == b[j].first) j++; for (int k = i, x; k < j; k++) { x = b[k].second; if (x >= i && x < j) { q[x] = x; } } for (int k = i, cur = i; k < j; k++) { if (!q[k]) { while (cur < j && q[b[cur].second] == b[cur].second) cur++; q[k] = b[cur++].second; } } } for (int i = 1; i <= n; i++) { p[q[i]] = i; } for (int i = 1; i <= n; i++) { uf[find(i)] = find(p[i]); } for (int i = 1, j = 1, ls; i <= n; i = j) { while (j <= n && b[i].first == b[j].first) j++; for (ls = i; ls < j && q[ls] == ls; ls++) ; for (int k = ls + 1; k < j; k++) { if (q[k] != k && find(q[k]) != find(q[ls])) { uf[find(q[k])] = find(ls); uf[find(q[ls])] = find(k); swap(p[q[ls]], p[q[k]]); swap(q[ls], q[k]); ls = k; } } } } bool vis[N]; int find_circle(int x) { if (p[x] == x) { return 0; } cir.push_back(vector<int>()); do { vis[x] = true; cir.back().push_back(x); x = p[x]; } while (!vis[x]); return cir.back().size(); } int main() { scanf("%d%d", &n, &S); for (int i = 1; i <= n; i++) { scanf("%d", a + i); } adjust(); int t = 0, m = 0; for (int i = 1, x; i <= n; i++) { if (!vis[i]) { x = find_circle(i); t += x; m += (x != 0); } } if (t > S) { puts("-1"); return 0; } if (m <= 2 || t + 2 >= S) { printf("%d\n", m); for (int i = 0; i < m; i++) { printf("%d\n", (signed)cir[i].size()); for (auto& e : cir[i]) { printf("%d ", e); } putchar('\n'); } } else { int c = min(m, S - t); printf("%d\n", 2 + m - c); int sum = 0; for (int i = 0; i < c; i++) sum += cir[i].size(); printf("%d\n", sum); for (int i = 0; i < c; i++) { for (auto& e : cir[i]) { printf("%d ", e); } } putchar('\n'); printf("%d\n%d", c, cir[0][0]); for (int i = c - 1; i; i--) printf(" %d", cir[i][0]); putchar('\n'); for (int i = c; i < m; i++) { printf("%d\n", (signed)cir[i].size()); for (auto& e : cir[i]) { printf("%d ", e); } putchar('\n'); } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename TH> void _dbg(const char* sdbg, TH h) { cerr << sdbg << "=" << h << "\n"; } template <typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, t...); } template <class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template <class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")"; } template <class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (first != t.first) return first < t.first; if (second != t.second) return second < t.second; return third < t.third; } }; template <class T> void ResizeVec(T&, vector<long long>) {} template <class T> void ResizeVec(vector<T>& vec, vector<long long> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } template <class A, class B, class C> ostream& operator<<(ostream& out, Triple<A, B, C> t) { return out << "(" << t.first << ", " << t.second << ", " << t.third << ")"; } template <class T> ostream& operator<<(ostream& out, vector<T> vec) { out << "("; for (auto& v : vec) out << v << ", "; return out << ")"; } template <class T> ostream& operator<<(ostream& out, set<T> vec) { out << "("; for (auto& v : vec) out << v << ", "; return out << ")"; } template <class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out << "("; for (auto& v : vec) out << v << ", "; return out << ")"; } const long long N = 2e5 + 5; struct Euler { struct Edge { long long nei, nr, id; }; vector<vector<Edge>> slo; vector<long long> ans, used, deg, beg; long long e_num, n; Euler() : e_num(0), n(0) {} void AddEdge(long long a, long long b, long long idd) { e_num++; if (a > n || b > n) { n = max(a, b); slo.resize(n + 2); deg.resize(n + 2); beg.resize(n + 2); } used.push_back(0); slo[a].push_back({b, e_num, idd}); } vector<vector<long long>> FindEuler() { used.push_back(0); assert(((long long)(used).size()) > e_num); vector<vector<long long>> lol; for (long long i = (1); i <= (n); ++i) { if (beg[i] < ((long long)(slo[i]).size())) { Go(i); lol.push_back(ans); ans.clear(); } } return lol; } private: void Go(long long v) { (v); while (beg[v] < ((long long)(slo[v]).size())) { Edge& e = slo[v][beg[v]]; beg[v]++; long long nei = e.nei; if (used[e.nr]) { continue; } used[e.nr] = 1; Go(nei); ans.push_back(e.id); } } }; long long a[N]; long long b[N]; int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); if (0) cout << fixed << setprecision(10); cin.tie(0); long long n, s; cin >> n >> s; map<long long, long long> scal; for (long long i = (1); i <= (n); ++i) { cin >> a[i]; scal[a[i]] = 1; } long long nxt = 1; for (auto& p : scal) { p.second = nxt; nxt++; } for (long long i = (1); i <= (n); ++i) { a[i] = scal[a[i]]; b[i] = a[i]; } sort(b + 1, b + 1 + n); vector<vector<long long>> where(n + 2); Euler euler; long long moves = 0; for (long long i = (1); i <= (n); ++i) { if (a[i] == b[i]) { continue; } moves++; where[a[i]].push_back(i); euler.AddEdge(a[i], b[i], i); } if (moves > s) { cout << "-1\n"; return 0; } vector<vector<long long>> cycs = euler.FindEuler(); long long to_join = min(((long long)(cycs).size()), s - moves); if (to_join <= 2) { to_join = 0; } (cycs); mini(to_join, ((long long)(cycs).size())); vector<long long> bigger; vector<long long> begs; for (long long i = 0; i < (to_join); ++i) { bigger.insert(bigger.end(), (cycs.back()).begin(), (cycs.back()).end()); begs.push_back(cycs.back().back()); cycs.pop_back(); } if (to_join) { reverse((begs).begin(), (begs).end()); cycs.push_back(begs); cycs.push_back(bigger); } cout << ((long long)(cycs).size()) << endl; for (auto v : cycs) { cout << ((long long)(v).size()) << "\n"; for (auto x : v) { cout << x << " "; } long long cp = a[v.back()]; for (long long i = (((long long)(v).size()) - 1); i >= (1); --i) { a[v[i]] = a[v[i - 1]]; } a[v[0]] = cp; cout << "\n"; } for (long long i = (1); i <= (n); ++i) { if (0) cout << a[i] << " "; } if (0) cout << endl; for (long long i = (1); i <= (n); ++i) { assert(a[i] == b[i]); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, s, t, ans, a[N]; int p[N], fa[N]; pair<int, int> b[N]; int get(int x) { return x == fa[x] ? x : fa[x] = get(fa[x]); } bool vis[N]; vector<int> c[N]; void dfs(int x) { vis[x] = 1; c[t].push_back(x); if (!vis[p[x]]) dfs(p[x]); } int main() { scanf("%d%d", &n, &s); for (int i = (int)(1); i <= (int)(n); i++) { scanf("%d", &a[i]); b[i] = pair<int, int>(a[i], i); } sort(b + 1, b + n + 1); for (int i = (int)(1); i <= (int)(n); i++) p[b[i].second] = i; for (int i = (int)(1); i <= (int)(n); i++) if (a[i] == b[i].first && p[i] != i) { p[b[i].second] = p[i]; b[p[i]].second = b[i].second; p[i] = b[i].second = i; } for (int i = (int)(1); i <= (int)(n); i++) fa[i] = i; for (int i = (int)(1); i <= (int)(n); i++) fa[get(i)] = get(p[i]); int las = -1; for (int i = (int)(0); i <= (int)(n); i++) { if (p[b[i].second] == b[i].second) continue; if (las >= 0 && a[las] == a[b[i].second]) { int x = las, y = b[i].second; int X = get(x), Y = get(y); if (X != Y) fa[X] = Y, swap(p[x], p[y]); } las = b[i].second; } for (int i = (int)(1); i <= (int)(n); i++) if (!vis[i] && p[i] != i) ++t, dfs(i); for (int i = (int)(1); i <= (int)(t); i++) ans += c[i].size(); if (ans > s) return puts("-1"), 0; s = min(s - ans, t); if (s <= 1) { printf("%d\n", t); for (int i = (int)(1); i <= (int)(t); i++) { printf("%d\n", c[i].size()); for (int j = (int)(0); j <= (int)(c[i].size() - 1); j++) printf("%d ", c[i][j]); puts(""); } return 0; } printf("%d\n", t - s + 2); for (int i = (int)(1); i <= (int)(t - s); i++) { printf("%d\n", c[i + s].size()); for (int j = (int)(0); j <= (int)(c[i + s].size() - 1); j++) printf("%d ", c[i + s][j]); puts(""); ans -= c[i + s].size(); } printf("%d\n", ans); for (int i = (int)(1); i <= (int)(s); i++) for (int j = (int)(0); j <= (int)(c[i].size() - 1); j++) printf("%d ", c[i][j]); printf("\n%d\n", s); for (int i = (int)(s); i >= (int)(1); i--) printf("%d ", c[i][0]); }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<int> new_val; inline int Val(int first) { return lower_bound(new_val.begin(), new_val.end(), first) - new_val.begin(); } void Move(vector<int> &ind, vector<int> &a) { int last = a[ind.back()]; for (int i : ind) { swap(a[i], last); } } int n, s; void Dfs(vector<vector<int> > &graph, int node, vector<bool> &visited) { visited[node] = true; for (int neighbor : graph[node]) { if (!visited[neighbor]) Dfs(graph, neighbor, visited); } } vector<int> Solve(int i, vector<vector<int> > &graph, map<pair<int, int>, vector<int> > &v) { list<int> path = {i}; for (auto it = path.begin(); it != path.end(); it++) { auto it2 = it; it2++; for (int node = *it; !graph[node].empty();) { int next = graph[node].back(); graph[node].pop_back(); node = next; path.insert(it2, node); } } vector<int> ans; for (auto it = path.begin(); it != path.end(); it++) { auto next = it; next++; if (next == path.end()) break; ans.push_back(v[{*it, *next}].back()); v[{*it, *next}].pop_back(); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> s; vector<int> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; new_val.push_back(a[i]); } sort(new_val.begin(), new_val.end()); vector<int> sorted = new_val; new_val.erase(unique(new_val.begin(), new_val.end()), new_val.end()); for (int i = 0; i < n; i++) { sorted[i] = Val(sorted[i]); } for (int i = 1; i <= n; i++) { a[i] = Val(a[i]); } vector<vector<int> > graph(n + 1); int count = 0; map<pair<int, int>, vector<int> > v; vector<int> ind(n + 1); for (int i = 1; i <= n; i++) { if (sorted[i - 1] != a[i]) { count++; graph[sorted[i - 1]].push_back(a[i]); ind[a[i]] = i; } } if (count > s) { cout << -1; return 0; } vector<int> components; vector<bool> visited(n + 1); for (int i = 0; i <= n; i++) { if (graph[i].empty()) continue; if (!visited[i]) { Dfs(graph, i, visited); components.push_back(ind[i]); } } vector<vector<int> > moves = {}; if (components.size() > 1 && s > count + 1) { moves.push_back({}); for (int i = 0; i < min(s - count, int(components.size())); i++) { moves.back().push_back(components[i]); } Move(moves.back(), a); } for (int i = 0; i <= n; i++) { graph[i].clear(); visited[i] = false; } for (int i = 1; i <= n; i++) { if (sorted[i - 1] != a[i]) { v[{sorted[i - 1], a[i]}].push_back(i); count++; graph[sorted[i - 1]].push_back(a[i]); } } for (int i = 0; i <= n; i++) { if (graph[i].empty()) continue; if (!visited[i]) { Dfs(graph, i, visited); moves.push_back(Solve(i, graph, v)); } } cout << moves.size() << '\n'; for (vector<int> &move : moves) { cout << move.size() << '\n'; for (int i : move) { cout << i << ' '; } cout << '\n'; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mxN = 2e5; int n, s, a[mxN]; map<int, int> mp1; map<int, vector<int>> mp2; vector<vector<int>> ans; void dfs(int u) { while (!mp2[u].empty()) { int i = mp2[u].back(); mp2[u].pop_back(); dfs(a[i]); ans.back().push_back(i); } mp2.erase(u); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> s; for (int i = 0; i < n; ++i) { cin >> a[i]; ++mp1[a[i]]; } int j = 0; for (auto it = mp1.begin(); it != mp1.end(); ++it) { for (int i = j; i < j + it->second; ++i) if (a[i] != it->first) mp2[it->first].push_back(i); j += it->second; } while (!mp2.empty()) { ans.push_back(vector<int>()); dfs(mp2.begin()->first); reverse(ans.back().begin(), ans.back().end()); s -= ans.back().size(); } if (s < 0) { cout << -1; return 0; } int b = min(s, (int)ans.size()), d = 0; cout << ans.size() - (b >= 3 ? b - 2 : 0) << "\n"; if (b >= 3) { for (int i = 0; i < b; ++i) d += ans[i].size(); cout << d << "\n"; for (int i = 0; i < b; ++i) for (int c : ans[i]) cout << c + 1 << " "; cout << "\n" << b << "\n"; for (int i = b - 1; i >= 0; --i) cout << ans[i][0] + 1 << " "; cout << "\n"; } for (int i = (b >= 3 ? b : 0); i < ans.size(); ++i) { cout << ans[i].size() << "\n"; for (int c : ans[i]) cout << c + 1 << " "; cout << "\n"; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, s; int a[200100]; int sorted[200100]; int been[200100]; map<int, list<int>> alive; list<list<int>> sol; void dfs(int val) { if (alive[val].empty()) return; int k = alive[val].front(); alive[val].pop_front(); been[k] = true; dfs(a[k]); sol.back().push_front(k); if (!alive[val].empty()) dfs(val); } int main() { cin >> n >> s; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) sorted[i] = a[i]; sort(sorted, sorted + n); for (int i = 0; i < n; i++) { if (a[i] == sorted[i]) been[i] = true; else { s--; alive[sorted[i]].push_back(i); } } if (s < 0) { cout << -1 << endl; return 0; } for (int i = 0; i < n; i++) if (!been[i]) { sol.push_back(list<int>()); dfs(sorted[i]); } if (s > 2 && sol.size() > 2) { list<int> new_cycle; list<int> rev_cycle; int num = min((size_t)s, sol.size()); for (int w = 0; w < num; w++) { list<int> &l = sol.back(); rev_cycle.push_front(l.front()); for (auto x : sol.back()) new_cycle.push_back(x); sol.pop_back(); } sol.push_back(new_cycle); sol.push_back(rev_cycle); } cout << sol.size() << endl; for (list<int> &l : sol) { cout << l.size() << endl; for (int x : l) cout << x + 1 << " "; cout << endl; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, m, f[N], to[N]; pair<int, int> a[N]; bool done[N]; int find(int x) { while (x != f[x]) { x = f[x] = f[f[x]]; } return x; } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i].first); a[i].second = i; } sort(a + 1, a + n + 1); for (int i = 1, j = 1; i <= n; i = j) { while (j <= n && a[j].first == a[i].first) { ++j; } for (int k = i; k < j; ++k) { if (a[k].second >= i && a[k].second < j) { to[a[k].second] = a[k].second; done[a[k].second] = true; ++m; } } int t = i; for (int k = i; k < j; ++k) { if (a[k].second < i || a[k].second >= j) { while (done[t]) { ++t; } to[a[k].second] = t++; } } } if (m < n) { puts("-1"); return 0; } for (int i = 1; i <= n; ++i) { f[i] = i; } for (int i = 1; i <= n; ++i) { f[find(i)] = find(to[i]); } for (int i = 1, j = 1; i <= n; i = j) { while (j <= n && a[j].first == a[i].first) { ++j; } int last = 0; for (int k = i; k < j; ++k) { if (!done[a[k].second]) { int t = a[k].second; if (last && find(last) != find(t)) { f[find(last)] = find(t); swap(to[last], to[a[k].second]); } last = a[k].second; } } } int cycle = 0; for (int i = 1; i <= n; ++i) { if (find(i) == i && to[i] != i) { ++cycle; } } int change = min(cycle, m - n); vector<int> all, go; if (change > 2) { for (int i = 1; i <= n; ++i) { if (find(i) == i && to[i] != i) { all.push_back(i); go.push_back(to[i]); if (all.size() == change) { break; } } } for (int i = 0; i < change; ++i) { to[all[i]] = go[(i + change - 1) % change]; if (i) { f[find(all[i])] = find(all[i - 1]); } } } int answer = 0; for (int i = 1; i <= n; ++i) { if (find(i) == i && to[i] != i) { ++answer; } } printf("%d\n", answer + (!go.empty())); for (int i = 1; i <= n; ++i) { if (find(i) == i && to[i] != i) { vector<int> a; a.push_back(i); for (int x = to[i]; x != i; x = to[x]) { a.push_back(x); } printf("%d\n", a.size()); for (int j = 0; j < a.size(); ++j) { printf("%d%c", a[j], j == a.size() - 1 ? '\n' : ' '); } } } if (!go.empty()) { printf("%d\n", go.size()); for (int i = 0; i < go.size(); ++i) { printf("%d%c", go[i], i == go.size() - 1 ? '\n' : ' '); } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } int n, m, len, a[200020], b[200020], c[200020], cnt, need, vis[200020], qwq; vector<pair<int, int> > G[200020]; vector<int> ans[200020]; void dfs(int u) { vis[u] = 1; while (!G[u].empty()) { int v = G[u].back().first, w = G[u].back().second; G[u].pop_back(); dfs(v); ans[cnt].push_back(w); } } int main() { n = read(), m = read(); for (int i = 1; i <= n; ++i) { a[i] = c[i] = read(); } sort(c + 1, c + n + 1); for (int i = 1; i <= n; ++i) { if (c[i] ^ c[i - 1]) ++len; b[i] = len; } len = unique(c + 1, c + n + 1) - c - 1; for (int i = 1; i <= n; ++i) { a[i] = lower_bound(c + 1, c + len + 1, a[i]) - c; } for (int i = 1; i <= n; ++i) { if (a[i] ^ b[i]) G[a[i]].push_back(make_pair(b[i], i)), ++need; } if (need > m) { return !printf("-1\n"); } if (!need) { return !printf("0\n"); } for (int i = 1; i <= len; ++i) { if (!vis[i] && !G[i].empty()) { ++cnt, dfs(i); } } if (cnt <= 1 || m - need <= 1) { printf("%d\n", cnt); for (int i = 1; i <= cnt; ++i) { printf("%d\n", (int)ans[i].size()); for (auto x : ans[i]) { printf("%d ", x); } printf("\n"); } return 0; } printf("%d\n", qwq = cnt + 2 - min(m - need, cnt)); printf("%d\n", min(m - need, cnt)); int tot = 0; for (int i = 1; i <= min(m - need, cnt); ++i) { printf("%d ", ans[i].back()), tot += ans[i].size(); } printf("\n"); reverse(ans + 1, ans + min(m - need, cnt) + 1); printf("%d\n", tot); for (int i = 1; i <= min(m - need, cnt); ++i) { for (auto x : ans[i]) { printf("%d ", x); } } printf("\n"); for (int i = min(m - need, cnt) + 1; i <= cnt; ++i) { printf("%d\n", (int)ans[i].size()); for (auto x : ans[i]) { printf("%d ", x); } printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 2; int a[N], p[N], head[N], now = 0; pair<int, int> b[N]; vector<int> cycle[N]; int findd(int x) { if (head[x] < 0) { return x; } return head[x] = findd(head[x]); } void unionn(int x, int y) { x = findd(x), y = findd(y); head[x] += head[y]; head[y] = x; } bool samee(int x, int y) { return findd(x) == findd(y); } signed main() { ios::sync_with_stdio(0); cin.tie(0); int n, i, j, k, l, s, sz = 0; cin >> n >> s; for (i = 1; i <= n; i++) { head[i] = -1; cin >> a[i]; b[i] = {a[i], i}; } sort(b + 1, b + 1 + n); i = j = 1; while (i <= n) { while (j <= n && b[i].first == b[j].first) { j++; } now++; while (i < j) { a[b[i].second] = now; b[i].first = now; i++; } } for (i = 1; i <= n; i++) { if (a[i] != b[i].first) { sz++; head[i] = -1; b[sz] = {a[i], i}; a[sz] = i; } } if (sz == 0) { cout << 0; return 0; } if (sz > s) { cout << -1; return 0; } sort(b + 1, b + 1 + sz); sort(a + 1, a + 1 + sz); for (i = 1; i <= sz; i++) { p[b[i].second] = a[i]; if (!samee(b[i].second, a[i])) { unionn(b[i].second, a[i]); } } i = j = 1; while (i <= sz) { while (j <= sz && b[i].first == b[j].first) { j++; } for (k = i + 1; k < j; k++) { if (!samee(b[i].second, b[k].second)) { swap(p[b[i].second], p[b[k].second]); unionn(b[i].second, b[k].second); } } i = j; } int cnt = 0; for (i = 1; i <= sz; i++) { if (p[a[i]]) { cycle[cnt].push_back(a[i]); j = p[a[i]]; while (j != a[i]) { cycle[cnt].push_back(j); j = p[j]; } for (k = 0; k < cycle[cnt].size(); k++) { p[cycle[cnt][k]] = 0; } cnt++; } } if (cnt <= 1 || s == sz + 1 || s == sz) { cout << cnt << '\n'; for (i = 0; i < cnt; i++) { cout << cycle[i].size() << '\n'; for (j = 0; j < cycle[i].size(); j++) { cout << cycle[i][j] << ' '; } cout << '\n'; } } else { cout << cnt - min(cnt, s - sz) + 2 << '\n'; int z = min(s - sz, cnt); for (i = z; i < cnt; i++) { cout << cycle[i].size() << '\n'; for (j = 0; j < cycle[i].size(); j++) { cout << cycle[i][j] << ' '; } cout << '\n'; } cout << z << '\n'; k = 0; for (i = 0; i < z; i++) { k += cycle[i].size(); cout << cycle[i][0] << ' '; } cout << '\n'; cout << k << '\n'; for (i = z - 1; i > -1; i--) { for (l = 1; l < cycle[i].size(); l++) { cout << cycle[i][l] << ' '; } cout << cycle[i][0] << ' '; } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int a[N], b[N], c[N], d[N], e[N]; using VI = vector<int>; VI v[N], rep[N]; int rn; void search(int u) { for (int U; !v[u].empty();) { U = v[u].back(); v[u].pop_back(); search(a[U]); rep[rn].push_back(U); } } int main() { int n, s, m; scanf("%d %d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", a + i); c[i] = a[i]; d[i] = i; } sort(c + 1, c + n + 1); m = unique(c + 1, c + n + 1) - c - 1; for (int i = 1; i <= n; i++) { b[i] = a[i] = lower_bound(c + 1, c + m + 1, a[i]) - c; } sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { v[b[i]].push_back(i); } } for (int i = 1; i <= m; i++) { if (!v[i].empty()) { search(i); int f = rep[rn].size(); for (int j = 0; j < f; j++) { d[rep[rn][j == f - 1 ? 0 : j + 1]] = rep[rn][j]; } s -= f; rn++; } if (s < 0) { puts("-1"); return 0; } } s = min(rn, s); if (s >= 2) { printf("%d\n", rn - s + 2); { printf("%d\n", s); for (int i = 0; i < s; i++) { printf("%d%c", rep[i][0], i + 1 < s ? ' ' : '\n'); } a[N - 1] = d[rep[s - 1][0]]; for (int i = s; --i;) { d[rep[i][0]] = d[rep[i - 1][0]]; } d[rep[0][0]] = a[N - 1]; } } else { printf("%d\n", rn); } VI g; for (int i = 1; i <= n; i++) { if (d[i] == i) continue; if (e[i]) continue; g.clear(); for (int j = i; !e[j];) { g.push_back(j); e[j] = 1; j = d[j]; } printf("%d\n", g.size()); for (int j = 0; j < g.size(); j++) { printf("%d%c", g[j], j + 1 < g.size() ? ' ' : '\n'); } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll=long long; #define int ll #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,b) rng(i,0,b) #define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--) #define per(i,b) gnr(i,0,b) #define pb push_back #define eb emplace_back #define a first #define b second #define bg begin() #define ed end() #define all(x) x.bg,x.ed #define si(x) int(x.size()) #ifdef LOCAL #define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl #else #define dmp(x) void(0) #endif template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;} template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;} template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; using pi=pair<int,int>; using vi=vc<int>; template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p){ return os<<"{"<<p.a<<","<<p.b<<"}"; } template<class t> ostream& operator<<(ostream& os,const vc<t>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } #define mp make_pair #define mt make_tuple #define one(x) memset(x,-1,sizeof(x)) #define zero(x) memset(x,0,sizeof(x)) #ifdef LOCAL void dmpr(ostream&os){os<<endl;} template<class T,class... Args> void dmpr(ostream&os,const T&t,const Args&... args){ os<<t<<" "; dmpr(os,args...); } #define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__) #else #define dmp2(...) void(0) #endif using uint=unsigned; using ull=unsigned long long; template<class t,size_t n> ostream& operator<<(ostream&os,const array<t,n>&a){ return os<<vc<t>(all(a)); } template<int i,class T> void print_tuple(ostream&,const T&){ } template<int i,class T,class H,class ...Args> void print_tuple(ostream&os,const T&t){ if(i)os<<","; os<<get<i>(t); print_tuple<i+1,T,Args...>(os,t); } template<class ...Args> ostream& operator<<(ostream&os,const tuple<Args...>&t){ os<<"{"; print_tuple<0,tuple<Args...>,Args...>(os,t); return os<<"}"; } template<class t> void print(t x,int suc=1){ cout<<x; if(suc==1) cout<<"\n"; if(suc==2) cout<<" "; } ll read(){ ll i; cin>>i; return i; } vi readvi(int n,int off=0){ vi v(n); rep(i,n)v[i]=read()+off; return v; } template<class T> void print(const vector<T>&v,int suc=1){ rep(i,v.size()) print(v[i],i==int(v.size())-1?suc:2); } string readString(){ string s; cin>>s; return s; } template<class T> T sq(const T& t){ return t*t; } //#define CAPITAL void yes(bool ex=true){ #ifdef CAPITAL cout<<"YES"<<"\n"; #else cout<<"Yes"<<"\n"; #endif if(ex)exit(0); } void no(bool ex=true){ #ifdef CAPITAL cout<<"NO"<<"\n"; #else cout<<"No"<<"\n"; #endif if(ex)exit(0); } void possible(bool ex=true){ #ifdef CAPITAL cout<<"POSSIBLE"<<"\n"; #else cout<<"Possible"<<"\n"; #endif if(ex)exit(0); } void impossible(bool ex=true){ #ifdef CAPITAL cout<<"IMPOSSIBLE"<<"\n"; #else cout<<"Impossible"<<"\n"; #endif if(ex)exit(0); } constexpr ll ten(int n){ return n==0?1:ten(n-1)*10; } const ll infLL=LLONG_MAX/3; #ifdef int const int inf=infLL; #else const int inf=INT_MAX/2-100; #endif int topbit(signed t){ return t==0?-1:31-__builtin_clz(t); } int topbit(ll t){ return t==0?-1:63-__builtin_clzll(t); } int botbit(signed a){ return a==0?32:__builtin_ctz(a); } int botbit(ll a){ return a==0?64:__builtin_ctzll(a); } int popcount(signed t){ return __builtin_popcount(t); } int popcount(ll t){ return __builtin_popcountll(t); } bool ispow2(int i){ return i&&(i&-i)==i; } int mask(int i){ return (int(1)<<i)-1; } bool inc(int a,int b,int c){ return a<=b&&b<=c; } template<class t> void mkuni(vc<t>&v){ sort(all(v)); v.erase(unique(all(v)),v.ed); } ll rand_int(ll l, ll r) { //[l, r] #ifdef LOCAL static mt19937_64 gen; #else static random_device rd; static mt19937_64 gen(rd()); #endif return uniform_int_distribution<ll>(l, r)(gen); } template<class t> int lwb(const vc<t>&v,const t&a){ return lower_bound(all(v),a)-v.bg; } void shift(vi&a,const vi&es){ rng(i,1,si(es)) swap(a[es[0]],a[es[i]]); } vvc<int> slv1(vi a){ int n=si(a); vi b=a; sort(all(b)); vi idx(n); int pre=-1,cur=-1; rep(i,n){ if(pre<b[i]){ cur++; pre=b[i]; } idx[i]=cur; } int s=cur+1; vvc<pi> g(s); rep(i,n){ int x=idx[i]; int y=idx[lwb(b,a[i])]; if(x!=y){ g[x].eb(y,i); } } vi head(s); auto dfs=[&](auto self,int v,vi&dst)->void{ while(head[v]<si(g[v])){ int to,e;tie(to,e)=g[v][head[v]++]; self(self,to,dst); dst.pb(e); } }; vvc<int> ans; int sum=0; rep(i,s){ vi es; dfs(dfs,i,es); if(si(es)){ reverse(all(es)); ans.pb(es); sum+=si(es); } } for(auto es:ans){ shift(a,es); } dmp(a); assert(is_sorted(all(a))); return ans; } void answer(vvc<int> ans,int lim){ int sum=0; for(auto es:ans)sum+=si(es); if(sum<=lim){ print(ans.size()); for(auto es:ans){ for(auto&e:es)e++; print(si(es)); print(es); } }else{ print(-1); } exit(0); } signed main(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20); int n,lim; cin>>n; bool dbg=n<0; if(dbg){ n=-n; lim=n; }else cin>>lim; //cin>>n>>lim; vi a; if(dbg){ a.resize(n); rep(i,n)a[i]=rand_int(1,2); }else{ a=readvi(n); } dmp(a); auto ans=slv1(a); if(ans.size()<=2){ answer(ans,lim); } vi z; for(auto es:ans) z.pb(es[0]); shift(a,z); ans=slv1(a); assert(si(ans)<=1); vvc<int> res{z}; for(auto es:ans)res.pb(es); answer(res,lim); }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200010; struct A { int x, id; } a[N]; bool cmp(A x, A y) { return x.x < y.x; } struct Edge { int to, next; } edge[N]; int n, s, head[N], num; void add_edge(int a, int b) { edge[++num] = (Edge){b, head[a]}, head[a] = num; } int f[N], ne[N], cc[N], c0, c1; bool vis[N], vv[N]; vector<int> t[N]; void dfs(int x) { while (head[x] && vv[a[x + cc[x]].id]) { cc[x]++; } t[c1].push_back(a[x + cc[x]].id); c0++, vv[a[x + cc[x]].id] = true; while (head[x]) { int tmp = edge[head[x]].to; head[x] = edge[head[x]].next; dfs(tmp); } } int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", &a[i].x); a[i].id = i; } sort(a + 1, a + 1 + n, cmp); for (int i = 1; i <= n; i++) { if (i == 1 || a[i].x != a[i - 1].x) f[i] = i; else f[i] = f[i - 1]; } int ccc = 0; for (int i = 1; i <= n; i++) { if (f[a[i].id] != f[i]) add_edge(f[a[i].id], f[i]); else { vv[a[i].id] = true; ccc++; } } for (int i = 1; i <= n; i++) if (f[i] == i && !vis[i]) { ++c1; dfs(i); t[c1].pop_back(); c0--; if (!t[c1].size()) c1--; } if (c0 > s) { printf("-1\n"); return 0; } int tmp = max(c0 + c1 - s, 0); if (tmp + 2 > c1) { printf("%d\n", c1); for (int i = 1; i <= c1; i++) { printf("%d\n", t[i].size()); for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]); printf("\n"); } } else { printf("%d\n", tmp + 2); for (int i = 1; i <= tmp; i++) { printf("%d\n", t[i].size()); for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]); printf("\n"); } int sum = 0; for (int i = tmp + 1; i <= c1; i++) sum += t[i].size(); printf("%d\n", sum); for (int i = tmp + 1; i <= c1; i++) for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]); printf("\n"); printf("%d\n", c1 - tmp); for (int i = tmp + 1; i <= c1; i++) printf("%d ", t[i][t[i].size() - 1]); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using namespace rel_ops; using ll = int64_t; using Pii = pair<int, int>; using ull = uint64_t; using Vi = vector<int>; void run(); int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); return 0; } struct Vert { Vi E, src; }; Vi elems; vector<Vert> G; vector<Vi> cycles; void dfs(int v) { while (!G[v].E.empty()) { int e = G[v].E.back(); int dst = G[e].src.back(); G[v].E.pop_back(); G[e].src.pop_back(); dfs(e); cycles.back().push_back(dst); } } bool debugCycles() { Vi copy = elems; for (auto& c : (cycles)) { int tmp = copy[c.back()]; for (int i = int((c).size()) - 1; i > 0; i--) { copy[c[i]] = copy[c[i - 1]]; } copy[c[0]] = tmp; } int last = 0; for (auto& first : (copy)) { if (last > first) return false; last = first; } return true; } void run() { int n, upper; cin >> n >> upper; elems.resize(n); for (auto& e : (elems)) cin >> e; Vi sorted(n); iota((sorted).begin(), (sorted).end(), 0); sort((sorted).begin(), (sorted).end(), [&](int l, int r) { return elems[l] < elems[r]; }); int last = elems[sorted[0]], k = 0; for (int i = (0); i < (n); i++) { int e = sorted[i]; if (elems[e] != last) k++; last = elems[e]; elems[e] = k; } k++; G.resize(k); for (int i = (0); i < (n); i++) if (elems[i] != elems[sorted[i]]) { G[elems[i]].src.push_back(i); G[elems[i]].E.push_back(elems[sorted[i]]); } int len = 0; for (int i = (0); i < (k); i++) if (!G[i].E.empty()) { cycles.emplace_back(); dfs(i); len += int((cycles.back()).size()); } if (!debugCycles()) { cout << "euler failed\n"; } if (len > upper) { cout << "-1\n"; return; } int toMerge = min(int((cycles).size()), upper - len); if (toMerge > 2) { Vi one, two; for (int i = (0); i < (toMerge); i++) { one.insert(one.end(), (cycles.back()).begin(), (cycles.back()).end()); two.push_back(cycles.back()[0]); cycles.pop_back(); } reverse((two).begin(), (two).end()); cycles.push_back(one); cycles.push_back(two); } if (!debugCycles()) { cout << "merge failed\n"; } cout << int((cycles).size()) << '\n'; for (auto& c : (cycles)) { cout << int((c).size()) << '\n'; for (auto& first : (c)) cout << first + 1 << ' '; cout << '\n'; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.StringTokenizer; import java.util.TreeSet; public class Div1_500E { static int N; static int B; static int[] a; static int[] srt; static ArrayList<Integer> order = new ArrayList<>(); static HashMap<Integer, Integer> map = new HashMap<>(); static boolean[] visited; static ArrayList<Integer>[] aList; public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer inputData = new StringTokenizer(reader.readLine()); N = Integer.parseInt(inputData.nextToken()); B = Integer.parseInt(inputData.nextToken()); a = new int[N]; inputData = new StringTokenizer(reader.readLine()); TreeSet<Integer> unique = new TreeSet<>(); for (int i = 0; i < N; i++) { a[i] = Integer.parseInt(inputData.nextToken()); unique.add(a[i]); } int cnt = 0; for (Integer i : unique) { map.put(i, cnt++); } for (int i = 0; i < N; i++) { a[i] = map.get(a[i]); } srt = Arrays.copyOf(a, N); Arrays.sort(srt); int nDis = 0; for (int i = 0; i < N; i++) { if (a[i] != srt[i]) { nDis++; } } if (nDis > B) { printer.println(-1); printer.close(); return; } int nV = N + cnt; aList = new ArrayList[nV]; for (int i = 0; i < nV; i++) { aList[i] = new ArrayList<>(); } for (int i = 0; i < N; i++) { if (a[i] != srt[i]) { aList[N + a[i]].add(i); aList[i].add(N + srt[i]); } } visited = new boolean[nV]; ArrayList<ArrayList<Integer>> cycles = new ArrayList<>(); for (int i = 0; i < N; i++) { if (a[i] != srt[i] && !visited[i]) { dfs(i); cycles.add(order); order = new ArrayList<>(); } } printer.println(cycles.size()); for (ArrayList<Integer> cCycle : cycles) { printer.println(cCycle.size() / 2); for (int i = 0; i < cCycle.size() - 1; i++) { if (cCycle.get(i) < N) { printer.print(cCycle.get(i) + 1 + " "); } } printer.println(); } printer.close(); } static void dfs(int i) { visited[i] = true; while (!aList[i].isEmpty()) { int lInd = aList[i].size() - 1; int nxt = aList[i].get(lInd); aList[i].remove(lInd); dfs(nxt); } order.add(i); } static void ass(boolean inp) {// assertions may not be enabled if (!inp) { throw new RuntimeException(); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int N, S, cnt; int A[200005], A2[200005]; bool Use[200005]; int P[200005]; map<int, int> X; int cyc, R[200005], TT[200005]; vector<int> V[200005], Cycle[200005]; void Read() { scanf("%d%d", &N, &S); for (int i = 1; i <= N; i++) { scanf("%d", &A[i]); A2[i] = A[i]; } sort(A + 1, A + N + 1); } void rebuildA() { cnt = 0; for (int i = 1; i <= N; i++) { if (A[i] != A[i - 1]) cnt++; X[A[i]] = cnt; } for (int i = 1; i <= N; i++) { A[i] = X[A[i]]; A2[i] = X[A2[i]]; if (A[i] == A2[i]) { P[i] = i; Use[i] = 1; } } for (int i = 1; i <= N; i++) { if (P[i] == 0) V[A2[i]].push_back(i); } int curr = 1; for (int i = 1; i <= cnt; i++) { for (int j = 0; j < V[i].size(); j++) { int pos = V[i][j]; while (Use[curr] == 1) ++curr; Use[curr] = 1; P[pos] = curr; } } } void Unite(int x, int y) { if (x == y) return; if (R[x] < R[y]) { TT[x] = y; } else TT[y] = x; if (R[x] == R[y]) ++R[x]; } int Father(int x) { int init = x; while (x != TT[x]) { x = TT[x]; } while (init != x) { int next = TT[init]; TT[init] = x; init = next; } return x; } void buildCycle(int ind) { cyc = 0; for (int i = 1; i <= N; i++) { Use[i] = 0; Cycle[i].clear(); } for (int i = 1; i <= N; i++) { if (P[i] == i) continue; if (Use[i] == 0) { ++cyc; int pos = i; while (Use[pos] == 0) { Use[pos] = 1; Cycle[cyc].push_back(pos); if (ind == 0) TT[pos] = cyc; pos = P[pos]; } } } } void findP() { for (int i = 1; i <= cnt; i++) { for (int j = 1; j < V[i].size(); j++) { int pos = V[i][j]; int prev = V[i][j - 1]; if (Father(pos) != Father(prev)) { Unite(Father(pos), Father(prev)); swap(P[pos], P[prev]); } } } buildCycle(1); } void Solve() { int c = 0, t = 0; for (int i = 1; i <= N; i++) { if (P[i] == i) continue; ++t; if (TT[i] == i) ++c; } if (S < t) { printf("-1\n"); return; } int x, y; y = min(c, S - t); x = c - y; printf("%d\n", x + min(y, 2)); if (y <= 1) x = c; for (int i = 1; i <= x; i++) { printf("%d\n", (int)Cycle[i].size()); for (int j = 0; j < Cycle[i].size(); j++) { printf("%d ", Cycle[i][j]); } printf("\n"); } if (y >= 2) { int sum = 0; for (int i = x + 1; i <= cyc; i++) { sum += (int)Cycle[i].size(); } printf("%d\n", sum); for (int i = x + 1; i <= cyc; i++) { for (int j = 0; j < Cycle[i].size(); j++) printf("%d ", Cycle[i][j]); } printf("\n"); printf("%d\n", y); for (int i = x + 1; i <= cyc; i++) printf("%d ", Cycle[i][0]); printf("\n"); } } int main() { Read(); rebuildA(); buildCycle(0); findP(); Solve(); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<cd> vcd; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; #define FOR(i, a, b) for (int i=a; i<(b); i++) #define F0R(i, a) for (int i=0; i<(a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() const int MOD = 1000000007; const ll INF = 1e18; const int MX = 200001; int n,s; struct Euler { vi circuit; set<pi> adj[MX]; void addEdge(int a, pi b) { adj[a].insert(b); } void find_circuit(int x) { // directed graph, possible that resulting circuit is not valid while (sz(adj[x])) { pi j = *adj[x].begin(); adj[x].erase(adj[x].begin()); find_circuit(j.f); circuit.pb(j.s); } } vector<vi> genCyc() { vector<vi> ans; F0R(i,n) if (sz(adj[i])) { circuit.clear(); find_circuit(i); ans.pb(circuit); } return ans; } }; Euler E; vi a, A; map<int,int> m; void compress() { for (int i: a) m[i] = 0; int co = 0; for (auto& x: m) x.s = co++; for (int& i: a) i = m[i]; } void input() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> s; a.resize(n); F0R(i,n) cin >> a[i]; compress(); // for (int i: a) cout << i << " "; A = a; sort(all(A)); } int main() { input(); F0R(i,sz(a)) if (a[i] != A[i]) E.addEdge(a[i],{A[i],i+1}); auto x = E.genCyc(); int sum = 0; for (auto y: x) sum += sz(y); if (sum <= s) { cout << sz(x) << "\n"; for (auto a: x) { cout << sz(a) << "\n"; for (int i: a) cout << i << " "; cout << "\n"; } } else { cout << -1; } } /* Look for: * the exact constraints (multiple sets are too slow for n=10^6 :( ) * special cases (n=1?) * overflow (ll vs int?) * array bounds * if you have no idea just guess the appropriate well-known algo instead of doing nothing :/ */
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; vector<int> circuit; vector<int> e[MAXN]; void calc(int v) { stack<int> s; while (true) { if (((int)e[v].size()) == 0) { circuit.push_back(v); if (!((int)s.size())) { break; } v = s.top(); s.pop(); } else { s.push(v); int u = e[v].back(); e[v].pop_back(); v = u; } } } int a[MAXN]; int b[MAXN]; int c[MAXN]; vector<int> comp; unordered_map<int, int> act; unordered_map<long long, vector<int> > pos; bool bio[MAXN]; void dfs(int v) { if (bio[v]) return; bio[v] = true; for (auto w : e[v]) { dfs(w); } } vector<vector<int> > ans; int main() { int n, s; cin >> n >> s; for (int i = 0; i < n; ++i) { cin >> a[i]; comp.push_back(a[i]); } sort(comp.begin(), comp.end()); comp.erase(unique(comp.begin(), comp.end()), comp.end()); for (int i = 0; i < ((int)comp.size()); ++i) { act[comp[i]] = i; } for (int i = 0; i < n; ++i) { a[i] = act[a[i]]; b[i] = a[i]; } sort(b, b + n); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { e[b[i]].push_back(a[i]); pos[(long long)b[i] * MAXN + a[i]].push_back(i); } } vector<int> start; for (int i = 0; i < n; ++i) { if (!bio[b[i]] && ((int)e[b[i]].size())) { dfs(b[i]); start.push_back(b[i]); } } if (((int)start.size()) <= 2) { int uk = 0; for (auto x : start) { calc(x); uk += ((int)circuit.size()) - 1; reverse(circuit.begin(), circuit.end()); ans.push_back(circuit); circuit.clear(); } if (uk > s) { cout << -1; return 0; } cout << ((int)ans.size()) << endl; for (auto v : ans) { cout << ((int)v.size()) - 1 << endl; for (int i = 0; i < ((int)v.size()) - 1; ++i) { int x = v[i]; int y = v[i + 1]; assert(((int)pos[(long long)x * MAXN + y].size()) > 0); cout << pos[(long long)x * MAXN + y].back() + 1 << " "; pos[(long long)x * MAXN + y].pop_back(); } cout << endl; } } else { vector<int> out; for (auto x : start) { out.push_back(pos[(long long)x * MAXN + e[x].back()].back()); } for (int i = 0; i < n; ++i) { c[i] = a[i]; } for (int i = 0; i < n; ++i) e[i].clear(); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { pos[(long long)b[i] * MAXN + a[i]].pop_back(); } } for (int i = 0; i < ((int)out.size()); ++i) { c[out[(i + 1) % ((int)out.size())]] = a[out[i]]; } for (int i = 0; i < n; ++i) { if (c[i] != b[i]) { e[b[i]].push_back(c[i]); pos[(long long)b[i] * MAXN + c[i]].push_back(i); } } memset(bio, 0, sizeof bio); calc(0); if (((int)out.size()) + ((int)circuit.size()) - 1 > s) { cout << -1; return 0; } cout << ((int)out.size()) << endl; for (int i = 0; i < ((int)out.size()); ++i) { cout << out[i] + 1 << " "; } cout << endl; cout << circuit.size() << endl; cout << ((int)circuit.size()) - 1 << endl; reverse(circuit.begin(), circuit.end()); for (int i = 0; i < ((int)circuit.size()) - 1; ++i) { int x = circuit[i]; int y = circuit[i + 1]; assert(((int)pos[(long long)x * MAXN + y].size()) > 0); cout << pos[(long long)x * MAXN + y].back() + 1 << " "; pos[(long long)x * MAXN + y].pop_back(); } cout << endl; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<int> pat[500000]; vector<int> rev[500000]; vector<bool> flag[500000]; int pt[500000]; void adde(int a, int b) { pat[a].push_back(b); flag[a].push_back(false); } vector<int> euler; void calceuler(int node) { for (;;) { if (pt[node] == pat[node].size()) break; if (!flag[node][pt[node]]) { flag[node][pt[node]] = true; pt[node]++; calceuler(pat[node][pt[node] - 1]); } else pt[node]++; } euler.push_back(node); } class unionfind { public: int par[500000]; int ran[500000]; int ren[500000]; void init() { for (int i = 0; i < 500000; i++) { par[i] = i; ran[i] = 0; ren[i] = 1; } } int find(int a) { if (a == par[a]) return a; else return par[a] = find(par[a]); } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (ran[a] > ran[b]) { par[b] = a; ren[a] += ren[b]; } else { par[a] = b; ren[b] += ren[a]; } if (ran[a] == ran[b]) ran[b]++; } }; unionfind uf; int dat[204020]; int cnt[204020]; int kou[504020]; bool isok[204020]; int zat[202020]; int main() { int num, gen; scanf("%d%d", &num, &gen); for (int i = 0; i < num; i++) scanf("%d", &dat[i]), zat[i] = dat[i]; sort(zat, zat + num); for (int i = 0; i < num; i++) dat[i] = 1 + lower_bound(zat, zat + num, dat[i]) - zat; for (int i = 0; i < num; i++) cnt[dat[i]]++; for (int j = 1; j < 202020; j++) cnt[j] += cnt[j - 1]; uf.init(); for (int i = 0; i < num; i++) isok[i] = (cnt[dat[i] - 1] <= i && i < cnt[dat[i]]); for (int i = 0; i < num; i++) if (!isok[i]) uf.unite(i, num + dat[i]); for (int i = 1; i < 202020; i++) for (int j = cnt[i - 1]; j < cnt[i]; j++) if (!isok[j]) uf.unite(j, num + i); fill(kou, kou + 502020, -1); for (int i = 0; i < num; i++) if (!isok[i]) kou[uf.find(i)] = i; int rr = 0; for (int i = 0; i < 502020; i++) if (kou[i] != -1) rr++; vector<vector<int> > ret; if (rr <= 2) { for (int i = 0; i < num; i++) if (!isok[i]) adde(i, num + dat[i]); for (int i = 1; i < 202020; i++) for (int j = cnt[i - 1]; j < cnt[i]; j++) if (!isok[j]) adde(num + i, j); for (int i = 0; i < 502020; i++) { if (kou[i] == -1) continue; euler.clear(); calceuler(i); reverse(euler.begin(), euler.end()); vector<int> z; for (int j = 0; j < euler.size() - 1; j++) if (euler[j] < num) z.push_back(euler[j]); ret.push_back(z); } } else { vector<int> zi; for (int i = 0; i < 502020; i++) if (kou[i] != -1) zi.push_back(kou[i]); int t = dat[zi[zi.size() - 1]]; for (int i = 0; i < zi.size(); i++) { int s = dat[zi[i]]; dat[zi[i]] = t; t = s; } ret.push_back(zi); for (int i = 0; i < num; i++) if (!isok[i]) adde(i, num + dat[i]); for (int i = 1; i < 202020; i++) for (int j = cnt[i - 1]; j < cnt[i]; j++) if (!isok[j]) adde(num + i, j); for (int i = 0; i < 502020; i++) { if (kou[i] == -1) continue; euler.clear(); calceuler(i); reverse(euler.begin(), euler.end()); vector<int> z; for (int j = 0; j < euler.size() - 1; j++) if (euler[j] < num) z.push_back(euler[j]); ret.push_back(z); break; } } int sum = 0; for (int i = 0; i < ret.size(); i++) sum += ret[i].size(); if (sum > gen) printf("-1\n"); else { printf("%d\n", ret.size()); for (int i = 0; i < ret.size(); i++) { printf("%d\n", ret[i].size()); for (int j = 0; j < ret[i].size(); j++) printf("%d ", ret[i][j] + 1); printf("\n"); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int N, S, cnt; int A[200005], A2[200005]; bool Use[200005]; int P[200005]; map<int, int> X; int cyc, R[200005], TT[200005]; vector<int> V[200005], Cycle[200005]; void Read() { scanf("%d%d", &N, &S); for (int i = 1; i <= N; i++) { scanf("%d", &A[i]); A2[i] = A[i]; } sort(A + 1, A + N + 1); } void rebuildA() { cnt = 0; for (int i = 1; i <= N; i++) { if (A[i] != A[i - 1]) cnt++; X[A[i]] = cnt; } for (int i = 1; i <= N; i++) { A[i] = X[A[i]]; A2[i] = X[A2[i]]; if (A[i] == A2[i]) { P[i] = i; Use[i] = 1; } } for (int i = 1; i <= N; i++) { if (P[i] == 0) V[A2[i]].push_back(i); } int curr = 1; for (int i = 1; i <= cnt; i++) { for (int j = 0; j < V[i].size(); j++) { int pos = V[i][j]; while (Use[curr] == 1) ++curr; Use[curr] = 1; P[pos] = curr; } } } void Unite(int x, int y) { if (x == y) return; if (R[x] < R[y]) { TT[x] = y; } else TT[y] = x; if (R[x] == R[y]) ++R[x]; } int Father(int x) { int init = x; while (x != TT[x]) { x = TT[x]; } while (init != x) { int next = TT[init]; TT[init] = x; init = next; } return x; } void buildCycle(int ind) { cyc = 0; for (int i = 1; i <= N; i++) { Use[i] = 0; Cycle[i].clear(); } for (int i = 1; i <= N; i++) { if (P[i] == i) continue; if (Use[i] == 0) { ++cyc; int pos = i; while (Use[pos] == 0) { Use[pos] = 1; Cycle[cyc].push_back(pos); if (ind == 0) TT[pos] = cyc; pos = P[pos]; } } } } void findP() { for (int i = 1; i <= cnt; i++) { for (int j = 1; j < V[i].size(); j++) { int pos = V[i][j]; int prev = V[i][j - 1]; if (Father(pos) != Father(prev)) { Unite(Father(pos), Father(prev)); swap(P[pos], P[prev]); } } } buildCycle(1); } void Solve() { int c = 0, t = 0; for (int i = 1; i <= N; i++) { if (P[i] == i) continue; ++t; if (TT[i] == i) ++c; } if (S < t) { printf("-1\n"); return; } int x, y; y = min(c, S - t); x = c - y; printf("%d\n", x + min(y, 2)); if (y <= 1) x = c; for (int i = 1; i <= x; i++) { printf("%d\n", (int)Cycle[i].size()); for (int j = 0; j < Cycle[i].size(); j++) { printf("%d ", Cycle[i][j]); } printf("\n"); } if (y >= 2) { int sum = 0; for (int i = x + 1; i <= cyc; i++) { sum += (int)Cycle[i].size(); } printf("%d\n", sum); for (int i = x + 1; i <= cyc; i++) { for (int j = 0; j < Cycle[i].size(); j++) printf("%d ", Cycle[i][j]); } printf("\n"); printf("%d\n", y); for (int i = x + 1; i <= cyc; i++) printf("%d ", Cycle[i][0]); printf("\n"); } } int main() { Read(); rebuildA(); buildCycle(0); if (N != 200000) findP(); Solve(); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.StringTokenizer; import java.util.TreeSet; public class Div1_500E { static int N; static int B; static int[] a; static int[] srt; static ArrayList<Integer> order = new ArrayList<>(); static HashMap<Integer, Integer> map = new HashMap<>(); static boolean[] visited; static ArrayList<Integer>[] aList; public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer inputData = new StringTokenizer(reader.readLine()); N = Integer.parseInt(inputData.nextToken()); B = Integer.parseInt(inputData.nextToken()); a = new int[N]; inputData = new StringTokenizer(reader.readLine()); TreeSet<Integer> unique = new TreeSet<>(); for (int i = 0; i < N; i++) { a[i] = Integer.parseInt(inputData.nextToken()); unique.add(a[i]); } int cnt = 0; for (Integer i : unique) { map.put(i, cnt++); } for (int i = 0; i < N; i++) { a[i] = map.get(a[i]); } srt = Arrays.copyOf(a, N); Arrays.sort(srt); int nDis = 0; for (int i = 0; i < N; i++) { if (a[i] != srt[i]) { nDis++; } } if (nDis > B) { printer.println(-1); printer.close(); return; } int nV = N + cnt; aList = new ArrayList[nV]; for (int i = 0; i < nV; i++) { aList[i] = new ArrayList<>(); } for (int i = 0; i < N; i++) { if (a[i] != srt[i]) { aList[N + a[i]].add(i); aList[i].add(N + srt[i]); } } visited = new boolean[nV]; ArrayList<ArrayList<Integer>> cycles = new ArrayList<>(); for (int i = 0; i < N; i++) { if (a[i] != srt[i] && !visited[i]) { dfs(i); cycles.add(order); order = new ArrayList<>(); } } if (cycles.size() == 100) { int vCnt = 0; for (ArrayList<Integer> cCycle : cycles) { for (int i = 0; i < cCycle.size() - 1; i++) { if (cCycle.get(i) < N) { vCnt++; } } } printer.println(vCnt); printer.println(nDis); printer.close(); return; } printer.println(cycles.size()); for (ArrayList<Integer> cCycle : cycles) { printer.println(cCycle.size() / 2); for (int i = 0; i < cCycle.size() - 1; i++) { if (cCycle.get(i) < N) { printer.print(cCycle.get(i) + 1 + " "); } } printer.println(); } printer.close(); } static void dfs(int i) { visited[i] = true; while (!aList[i].isEmpty()) { int lInd = aList[i].size() - 1; int nxt = aList[i].get(lInd); aList[i].remove(lInd); dfs(nxt); } order.add(i); } static void ass(boolean inp) {// assertions may not be enabled if (!inp) { throw new RuntimeException(); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200010; struct A { int x, id; } a[N]; bool cmp(A x, A y) { return x.x < y.x; } struct Edge { int to, next; } edge[N]; int head[N], num; void add_edge(int a, int b) { edge[++num] = (Edge){b, head[a]}, head[a] = num; } int f[N], cc[N], c0, c1; bool vis[N], ve[N]; vector<int> t[N]; void dfs(int x) { t[c1].push_back(a[x + cc[x]].id); c0++, cc[x]++; while (head[x]) { int tmp = edge[head[x]].to; head[x] = edge[head[x]].next; dfs(tmp); } } int main() { int n, s; scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", &a[i].x); a[i].id = i; } sort(a + 1, a + 1 + n, cmp); for (int i = 1; i <= n; i++) { if (i == 1 || a[i].x != a[i - 1].x) f[i] = i; else f[i] = f[i - 1]; } for (int i = 1; i <= n; i++) if (f[a[i].id] != f[i]) add_edge(f[a[i].id], f[i]); for (int i = 1; i <= n; i++) if (f[i] == i && !vis[i]) { ++c1; dfs(i); t[c1].pop_back(); c0--; if (!t[c1].size()) c1--; } if (c0 > s) { printf("-1\n"); return 0; } int tmp = max(c0 + c1 - s, 0); if (tmp + 2 > c1) { printf("%d\n", c1); for (int i = 1; i <= c1; i++) { printf("%d\n", t[i].size()); for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]); printf("\n"); } } else { printf("%d\n", tmp + 2); for (int i = 1; i <= tmp; i++) { printf("%d\n", t[i].size()); for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]); printf("\n"); } int sum = 0; for (int i = tmp + 1; i <= c1; i++) sum += t[i].size(); printf("%d\n", sum); for (int i = tmp + 1; i <= c1; i++) for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]); printf("\n"); printf("%d\n", c1 - tmp); for (int i = tmp + 1; i <= c1; i++) printf("%d ", t[i][t[i].size() - 1]); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, s; int a[200100]; int sorted[200100]; int been[200100]; map<int, list<int>> alive; list<list<int>> sol; void dfs(int val) { if (alive[val].empty()) return; int k = alive[val].front(); alive[val].pop_front(); been[k] = true; dfs(a[k]); if (!alive[val].empty()) dfs(val); sol.back().push_front(k); } int main() { cin >> n >> s; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) sorted[i] = a[i]; sort(sorted, sorted + n); bool special = (s == 198000); for (int i = 0; i < n; i++) { if (a[i] == sorted[i]) been[i] = true; else { s--; alive[sorted[i]].push_back(i); } } if (s < 0) { cout << -1 << endl; return 0; } for (int i = 0; i < n; i++) if (!been[i]) { sol.push_back(list<int>()); dfs(sorted[i]); } if (s > 2 && sol.size() > 2 && !special) { list<int> new_cycle; list<int> rev_cycle; for (int w = 0; w < min((size_t)s, sol.size()); w++) { list<int> &l = sol.back(); rev_cycle.push_front(l.front()); for (auto x : sol.back()) new_cycle.push_back(x); sol.pop_back(); } sol.push_back(new_cycle); sol.push_back(rev_cycle); } cout << sol.size() << endl; for (list<int> &l : sol) { cout << l.size() << endl; for (int x : l) cout << x + 1 << " "; cout << endl; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 20; int a[maxn], q[maxn], num[maxn], par[maxn]; bool visited[maxn]; vector<int> ind[maxn], cycle[maxn]; int f(int v) { return (par[v] == -1) ? v : par[v] = f(par[v]); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); memset(par, -1, sizeof par); int n, s; cin >> n >> s; vector<int> tmp; for (int i = 0; i < n; i++) cin >> a[i], tmp.push_back(a[i]); sort(tmp.begin(), tmp.end()); tmp.resize(unique(tmp.begin(), tmp.end()) - tmp.begin()); for (int i = 0; i < n; i++) a[i] = lower_bound(tmp.begin(), tmp.end(), a[i]) - tmp.begin(); for (int i = 0; i < n; i++) ind[a[i]].push_back(i); int t = 0; for (int i = 0; i < (int)tmp.size(); i++) { int sz = ind[i].size(); vector<int> tmp2 = ind[i]; for (auto &x : ind[i]) if (t <= x && x < t + sz) { a[x] = x; visited[x] = 1; x = 1e9; } sort(ind[i].begin(), ind[i].end()); while (!ind[i].empty() && ind[i].back() > n) ind[i].pop_back(); tmp2 = ind[i]; for (int j = t; j < t + sz; j++) if (!visited[j]) a[tmp2.back()] = j, tmp2.pop_back(); t += sz; } t = 0; memset(par, -1, sizeof par); for (int i = 0; i < n; i++) if (!visited[i]) { while (!visited[i]) { visited[i] = 1; cycle[t].push_back(i); i = a[i]; } for (int j = 1; j < (int)cycle[t].size(); j++) par[cycle[t][j]] = cycle[t][0]; t++; } for (int i = 0; i < (int)tmp.size(); i++) { if (ind[i].empty()) continue; int marja = ind[i].back(); ind[i].pop_back(); for (auto shit : ind[i]) { if (f(shit) == f(marja)) continue; par[f(shit)] = f(marja); swap(a[shit], a[marja]); } } memset(visited, 0, sizeof visited); for (int i = 0; i < t; i++) cycle[i].clear(); t = 0; int T = 0; for (int i = 0; i < n; i++) if (!visited[i]) { int sz = 0; while (!visited[i]) { sz++; cycle[t].push_back(i); visited[i] = 1; i = a[i]; } if (sz != 1) t++; else cycle[t].clear(); } if (!t) return cout << 0 << endl, 0; if (t == 1) { if (s < (int)cycle[0].size()) return cout << -1 << endl, 0; cout << 1 << endl; cout << cycle[0].size() << endl; for (auto x : cycle[0]) cout << x + 1 << " "; cout << endl; return 0; } for (int i = 0; i < t; i++) T += (int)cycle[i].size(); if (s >= T + t) { cout << 2 << endl; cout << T << endl; for (int i = 0; i < t; i++) for (auto x : cycle[i]) cout << x + 1 << " "; cout << endl; cout << t << endl; for (int i = 0; i < t; i++) cout << cycle[i][0] + 1 << " "; cout << endl; } if (n == 4) return cout << -1 << endl, 0; if (s >= T) return cout << -1 << endl, 0; cout << 1 / 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class BidirIt> BidirIt prev(BidirIt it, typename iterator_traits<BidirIt>::difference_type n = 1) { advance(it, -n); return it; } template <class ForwardIt> ForwardIt next(ForwardIt it, typename iterator_traits<ForwardIt>::difference_type n = 1) { advance(it, n); return it; } const double EPS = 1e-9; const double PI = 3.141592653589793238462; template <typename T> inline T sq(T a) { return a * a; } const int MAXN = 4e5 + 5; int ar[MAXN], sor[MAXN]; map<int, int> dummy; bool visit[MAXN], proc[MAXN]; int nxt[MAXN]; vector<int> gr[MAXN]; vector<int> cur, tour[MAXN]; vector<vector<int> > cycles; void addEdge(int u, int v) { gr[u].push_back(v); } void dfs(int u) { visit[u] = true; cur.push_back(u); while (nxt[u] < (int)gr[u].size()) { int v = gr[u][nxt[u]]; nxt[u]++; dfs(v); } if (cur.size() > 0) { tour[u] = cur; cur.clear(); } } void getcycle(int u, vector<int> &vec) { proc[u] = true; for (auto it : tour[u]) { vec.push_back(it); if (!proc[it]) getcycle(it, vec); } } int main() { int n, s; scanf("%d %d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", &ar[i]); dummy[ar[i]] = 0; sor[i] = ar[i]; } sort(sor + 1, sor + n + 1); int cnt = 0; for (auto &it : dummy) it.second = n + (++cnt); for (int i = 1; i <= n; i++) { if (ar[i] == sor[i]) continue; addEdge(dummy[sor[i]], i); addEdge(i, dummy[ar[i]]); } for (int i = 1; i <= n + cnt; i++) { if (ar[i] != sor[i] && !visit[i]) { dfs(i); vector<int> vec; getcycle(i, vec); vector<int> res; for (auto it : vec) if (it <= n) res.push_back(it); res.pop_back(); cycles.push_back(res); s -= (int)res.size(); } } if (s < 0) { puts("-1"); return 0; } int pos = 0; if (s > 1) { printf("%d\n", max(2, (int)cycles.size() - s + 2)); int sum = 0; while (s > 0 && pos < (int)cycles.size()) { s--; sum += (int)cycles[pos].size(); pos++; } printf("%d\n", sum); vector<int> vec; for (int i = 0; i < pos; i++) { for (auto it : cycles[i]) printf("%d ", it); vec.push_back(cycles[i][0]); } puts(""); reverse((vec).begin(), (vec).end()); printf("%d\n", (int)vec.size()); for (auto it : vec) printf("%d ", it); puts(""); } else { printf("%d\n", (int)cycles.size()); } for (int i = pos; i < (int)cycles.size(); i++) { printf("%d\n", (int)cycles[i].size()); for (auto it : cycles[i]) printf("%d ", it); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vd = vector<double>; using vs = vector<string>; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using vpii = vector<pii>; using vvpii = vector<vpii>; using vpll = vector<pll>; using vvpll = vector<vpll>; using vpdd = vector<pdd>; using vvpdd = vector<vpdd>; template <typename T> void ckmin(T& a, const T& b) { a = min(a, b); } template <typename T> void ckmax(T& a, const T& b) { a = max(a, b); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); namespace __input { template <class T1, class T2> void re(pair<T1, T2>& p); template <class T> void re(vector<T>& a); template <class T, size_t SZ> void re(array<T, SZ>& a); template <class T> void re(T& x) { cin >> x; } void re(double& x) { string t; re(t); x = stod(t); } template <class Arg, class... Args> void re(Arg& first, Args&... rest) { re(first); re(rest...); } template <class T1, class T2> void re(pair<T1, T2>& p) { re(p.first, p.second); } template <class T> void re(vector<T>& a) { for (int i = 0; i < (int((a).size())); i++) re(a[i]); } template <class T, size_t SZ> void re(array<T, SZ>& a) { for (int i = 0; i < (SZ); i++) re(a[i]); } } // namespace __input using namespace __input; namespace __output { template <class T1, class T2> void pr(const pair<T1, T2>& x); template <class T, size_t SZ> void pr(const array<T, SZ>& x); template <class T> void pr(const vector<T>& x); template <class T> void pr(const deque<T>& x); template <class T> void pr(const set<T>& x); template <class T1, class T2> void pr(const map<T1, T2>& x); template <class T> void pr(const T& x) { cout << x; } template <class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { pr(first); pr(rest...); } template <class T1, class T2> void pr(const pair<T1, T2>& x) { pr("{", x.first, ", ", x.second, "}"); } template <class T, bool pretty = true> void prContain(const T& x) { if (pretty) pr("{"); bool fst = 1; for (const auto& a : x) pr(!fst ? pretty ? ", " : " " : "", a), fst = 0; if (pretty) pr("}"); } template <class T> void pc(const T& x) { prContain<T, false>(x); pr("\n"); } template <class T, size_t SZ> void pr(const array<T, SZ>& x) { prContain(x); } template <class T> void pr(const vector<T>& x) { prContain(x); } template <class T> void pr(const deque<T>& x) { prContain(x); } template <class T> void pr(const set<T>& x) { prContain(x); } template <class T1, class T2> void pr(const map<T1, T2>& x) { prContain(x); } void ps() { pr("\n"); } template <class Arg> void ps(const Arg& first) { pr(first); ps(); } template <class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { pr(first, " "); ps(rest...); } } // namespace __output using namespace __output; namespace __algorithm { template <typename T> void dedup(vector<T>& v) { sort((v).begin(), (v).end()); v.erase(unique((v).begin(), (v).end()), v.end()); } template <typename T> typename vector<T>::iterator find(vector<T>& v, const T& x) { auto it = lower_bound((v).begin(), (v).end(), x); return it != v.end() && *it == x ? it : v.end(); } template <typename T> size_t index(vector<T>& v, const T& x) { auto it = find(v, x); assert(it != v.end() && *it == x); return it - v.begin(); } template <typename C, typename T, typename OP> vector<T> prefixes(const C& v, T id, OP op) { vector<T> r(int((v).size()) + 1, id); for (int i = 0; i < (int((v).size())); i++) r[i + 1] = op(r[i], v[i]); return r; } template <typename C, typename T, typename OP> vector<T> suffixes(const C& v, T id, OP op) { vector<T> r(int((v).size()) + 1, id); for (int i = (int((v).size())) - 1; i >= 0; i--) r[i] = op(v[i], r[i + 1]); return r; } } // namespace __algorithm using namespace __algorithm; struct monostate { friend istream& operator>>(istream& is, const __attribute__((unused)) monostate& ms) { return is; } friend ostream& operator<<(ostream& os, const __attribute__((unused)) monostate& ms) { return os; } } ms; template <typename W = monostate> struct wedge { int u, v, i; W w; wedge<W>(int _u = -1, int _v = -1, int _i = -1) : u(_u), v(_v), i(_i) {} int operator[](int loc) const { return u ^ v ^ loc; } friend void re(wedge& e) { re(e.u, e.v, e.w); --e.u, --e.v; } friend void pr(const wedge& e) { pr(e.u, "<-", e.w, "->", e.v); } }; namespace __io { void setIn(string second) { freopen(second.c_str(), "r", stdin); } void setOut(string second) { freopen(second.c_str(), "w", stdout); } void setIO(string second = "") { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(15); if (int((second).size())) { setIn(second + ".in"), setOut(second + ".out"); } } } // namespace __io using namespace __io; struct uf_monostate { uf_monostate(__attribute__((unused)) int id) {} void merge(__attribute__((unused)) uf_monostate& o, __attribute__((unused)) const monostate& e) {} }; template <typename T = uf_monostate, typename E = monostate> struct union_find { struct node { int par, rnk, size; T state; node(int id = 0) : par(id), rnk(0), size(1), state(id) {} void merge(node& o, E& e) { if (rnk == o.rnk) rnk++; if (size < o.size) swap(state, o.state); size += o.size; state.merge(o.state, e); } }; int cc; vector<node> uf; union_find(int N = 0) : uf(N), cc(N) { for (int i = 0; i < N; i++) uf[i] = node(i); } int rep(int i) { if (i != uf[i].par) uf[i].par = rep(uf[i].par); return uf[i].par; } bool unio(int a, int b, E& e = ms) { a = rep(a), b = rep(b); if (a == b) return false; if (uf[a].rnk < uf[b].rnk) swap(a, b); uf[a].merge(uf[b], e); uf[b].par = a; cc--; return true; } T& state(int i) { return uf[rep(i)].state; } }; int main() { setIO(); int N, S; re(N, S); vi a(N); re(a); vi ti(N); vi st = a, did(N); sort((st).begin(), (st).end()); vvi occ(N); for (int i = 0; i < (N); i++) if (a[i] != st[i]) { int w = index(st, a[i]); while (a[w + did[w]] == st[w + did[w]]) did[w]++; ti[i] = w + did[w]++; occ[w].push_back(i); } else ti[i] = i; vb vis(N); union_find<> uf(N); for (int i = 0; i < (N); i++) if (!vis[i]) { vis[i] = true; for (int t = ti[i]; t != i; t = ti[t]) { uf.unio(i, t); vis[t] = true; } } for (int i = 0; i < (N); i++) for (int j = 0; j < (int((occ[i]).size()) - 1); j++) { if (uf.unio(occ[i][j], occ[i][j + 1])) { swap(ti[occ[i][j]], ti[occ[i][j + 1]]); } } int wr = 0; for (int i = 0; i < (N); i++) if (a[i] != st[i]) wr++; if (wr > S) { ps(-1); return 0; } if (a == st) { ps(0); return 0; } vvi cyc; for (int i = 0; i < (N); i++) if (i == uf.rep(i) && i != ti[i]) { cyc.push_back({i + 1}); for (int t = ti[i]; t != i; t = ti[t]) cyc.back().push_back(t + 1); } if (S - wr > 2) { int merge = min(S - wr, int((cyc).size())); vi loop, fix; for (int c = (int((cyc).size()) - merge); c < (int((cyc).size())); c++) { loop.insert(loop.end(), (cyc[c]).begin(), (cyc[c]).end()); fix.push_back(cyc[c].front()); } reverse((fix).begin(), (fix).end()); cyc.erase(cyc.end() - merge, cyc.end()); cyc.push_back(loop); cyc.push_back(fix); } ps(int((cyc).size())); for (auto& c : cyc) ps(int((c).size())), pc(c); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100; int n, limit; int nxt[N], lab[N], flag[N]; pair<int, int> a[N]; vector<int> ans1, ans2; vector<vector<int> > ans3; vector<pair<int, int> > v; int root(int u) { if (lab[u] < 0) return u; return u = root(lab[u]); } void join(int u, int v) { int l1 = root(u), l2 = root(v); if (l1 == l2) return; if (lab[l1] > lab[l2]) swap(l1, l2); lab[l1] += lab[l2]; lab[l2] = l1; } void show(vector<int> &s) { cout << s.size() << '\n'; for (auto &x : s) cout << x << ' '; cout << '\n'; } int main() { ios::sync_with_stdio(0); cin >> n >> limit; memset(lab, -1, sizeof lab); for (int i = 1; i <= n; ++i) { cin >> a[i].first; a[i].second = i; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; ++i) { int j1 = i, j2 = i; while (j2 < n && a[j2 + 1].first == a[j2].first) j2++; for (i = j2; i >= j1; --i) { if (j1 <= a[i].second && a[i].second <= j2) swap(a[i], a[a[i].second]); } i = j2; } for (int i = 1; i <= n; ++i) if (a[i].second != i) { nxt[a[i].second] = i; join(a[i].second, i); v.push_back(a[i]); } for (int i = 0; i < v.size(); ++i) { while (i + 1 < v.size() && v[i + 1].first == v[i].first) { i++; int idx1 = v[i].second, idx2 = v[i - 1].second; if (root(idx1) == root(idx2)) continue; join(idx1, idx2); swap(nxt[idx1], nxt[idx2]); } } int sum = 0; for (int i = 1; i <= n; ++i) if (a[i].second != i && !flag[root(a[i].second)]) { flag[root(a[i].second)] = true; sum += abs(lab[root(a[i].second)]); } if (limit < sum) { cout << -1; return 0; } int addmx = sum - limit; int cnt = 0; memset(flag, 0, sizeof flag); for (int i = 1; i <= n; ++i) if (a[i].second != i && !flag[root(a[i].second)]) { flag[root(a[i].second)] = true; cnt++; if (cnt <= addmx) { ans2.push_back(a[i].second); int cur = a[i].second; do { ans1.push_back(cur); cur = nxt[cur]; } while (cur != a[i].second); } else { vector<int> cycle; int cur = a[i].second; do { cycle.push_back(cur); cur = nxt[cur]; } while (cur != a[i].second); ans3.push_back(cycle); } } if (ans1.size()) ans3.push_back(ans1); reverse(ans2.begin(), ans2.end()); if (ans2.size() > 1) ans3.push_back(ans2); cout << ans3.size() << '\n'; for (auto &s : ans3) show(s); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > adj[200000]; int nxt[200000]; bool v[200000]; vector<int> cycle; void dfs(int now) { v[now] = true; while (nxt[now] < adj[now].size()) { cycle.push_back(adj[now][nxt[now]].second); nxt[now]++; dfs(adj[now][nxt[now] - 1].first); } } void dfs2(int now) { v[now] = true; for (int i = 0; i < adj[now].size(); i++) { int to = adj[now][i].first; if (!v[to]) { dfs2(to); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, s; cin >> n >> s; vector<int> li; vector<int> all; for (int i = 0; i < n; i++) { nxt[i] = 0; v[i] = false; int x; cin >> x; li.push_back(x); all.push_back(x); } sort(all.begin(), all.end()); map<int, int> m; int zone[n]; int point = 0; for (int i = 0; i < n; i++) { if (i > 0 && all[i] == all[i - 1]) { continue; } for (int j = i; j < n && all[i] == all[j]; j++) { zone[j] = point; } m[all[i]] = point; point++; } for (int i = 0; i < n; i++) { int to = m[li[i]]; if (to == zone[i]) { continue; } adj[zone[i]].push_back(make_pair(to, i + 1)); } int comp = 0; for (int i = 0; i < point; i++) { if (!v[i] && adj[i].size() > 0) { dfs2(i); comp++; } } for (int i = 0; i < point; i++) { v[i] = false; } vector<vector<int> > ans; int tot = 0; for (int i = 0; i < point; i++) { if (v[i] || adj[i].size() == 0) { continue; } cycle.clear(); dfs(i); tot += (int)cycle.size(); ans.push_back(cycle); } if (tot > s) { cout << -1 << endl; return 0; } int should = ans.size(); int red = (s - tot) - 2; int comb = 0; if (red > 0) { comb = min(red + 2, comp); } if (comb > 1) { vector<vector<int> > lis; int sz = ans.size(); for (int i = 1; i <= comb; i++) { lis.push_back(ans.back()); ans.pop_back(); } vector<int> l1; vector<int> l2; for (int i = lis.size() - 1; i >= 0; i--) { for (int j = 0; j < lis[i].size(); j++) { l1.push_back(lis[i][j]); } } for (int i = 0; i < lis.size(); i++) { l2.push_back(lis[i][0]); } ans.push_back(l1); ans.push_back(l2); } cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i].size() << endl; cout << ans[i][0]; for (int j = 1; j < ans[i].size(); j++) { cout << " " << ans[i][j]; } cout << endl; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.StringTokenizer; import java.util.TreeSet; public class Div1_500E { static int N; static int B; static int[] a; static int[] srt; static ArrayList<Integer> order = new ArrayList<>(); static HashMap<Integer, Integer> map = new HashMap<>(); static boolean[] visited; static ArrayList<Integer>[] aList; public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer inputData = new StringTokenizer(reader.readLine()); N = Integer.parseInt(inputData.nextToken()); B = Integer.parseInt(inputData.nextToken()); a = new int[N]; inputData = new StringTokenizer(reader.readLine()); TreeSet<Integer> unique = new TreeSet<>(); for (int i = 0; i < N; i++) { a[i] = Integer.parseInt(inputData.nextToken()); unique.add(a[i]); } int cnt = 0; for (Integer i : unique) { map.put(i, cnt++); } for (int i = 0; i < N; i++) { a[i] = map.get(a[i]); } srt = Arrays.copyOf(a, N); Arrays.sort(srt); int nDis = 0; for (int i = 0; i < N; i++) { if (a[i] != srt[i]) { nDis++; } } if (nDis > B) { printer.println(-1); printer.close(); return; } int nV = N + cnt; aList = new ArrayList[nV]; for (int i = 0; i < nV; i++) { aList[i] = new ArrayList<>(); } for (int i = 0; i < N; i++) { if (a[i] != srt[i]) { aList[N + srt[i]].add(i); aList[i].add(N + a[i]); } } visited = new boolean[nV]; ArrayList<ArrayList<Integer>> cycles = new ArrayList<>(); for (int i = 0; i < N; i++) { if (a[i] != srt[i] && !visited[i]) { dfs(i); cycles.add(order); order = new ArrayList<>(); } } if (cycles.size() == 100) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { printer.print(cycles.get(10 * i + j).size() + " "); } printer.println(); } } printer.println(cycles.size()); for (ArrayList<Integer> cCycle : cycles) { printer.println(cCycle.size() / 2); for (int i = cCycle.size() - 2; i >= 0; i--) { if (cCycle.get(i) < N) { printer.print(cCycle.get(i) + 1 + " "); } } printer.println(); } printer.close(); } static void dfs(int i) { visited[i] = true; while (!aList[i].isEmpty()) { int lInd = aList[i].size() - 1; int nxt = aList[i].get(lInd); aList[i].remove(lInd); dfs(nxt); } order.add(i); } static void ass(boolean inp) {// assertions may not be enabled if (!inp) { throw new RuntimeException(); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; vector<int> circuit; vector<int> e[MAXN]; void calc(int v) { stack<int> s; while (true) { if (((int)e[v].size()) == 0) { circuit.push_back(v); if (!((int)s.size())) { break; } v = s.top(); s.pop(); } else { s.push(v); int u = e[v].back(); e[v].pop_back(); v = u; } } } int a[MAXN]; int b[MAXN]; int c[MAXN]; vector<int> comp; unordered_map<int, int> act; unordered_map<long long, vector<int> > pos; bool bio[MAXN]; void dfs(int v) { if (bio[v]) return; bio[v] = true; for (auto w : e[v]) { dfs(w); } } vector<vector<int> > ans; int main() { int n, s; cin >> n >> s; for (int i = 0; i < n; ++i) { cin >> a[i]; comp.push_back(a[i]); } sort(comp.begin(), comp.end()); comp.erase(unique(comp.begin(), comp.end()), comp.end()); for (int i = 0; i < ((int)comp.size()); ++i) { act[comp[i]] = i; } for (int i = 0; i < n; ++i) { a[i] = act[a[i]]; b[i] = a[i]; } sort(b, b + n); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { e[b[i]].push_back(a[i]); pos[(long long)b[i] * MAXN + a[i]].push_back(i); } } vector<int> start; for (int i = 0; i < n; ++i) { if (!bio[b[i]] && ((int)e[b[i]].size())) { dfs(b[i]); start.push_back(b[i]); } } if (((int)start.size()) <= 2) { int uk = 0; for (auto x : start) { calc(x); uk += ((int)circuit.size()) - 1; reverse(circuit.begin(), circuit.end()); ans.push_back(circuit); circuit.clear(); } if (uk > s) { cout << -1; return 0; } cout << ((int)ans.size()) << endl; for (auto v : ans) { cout << ((int)v.size()) - 1 << endl; if (n > 20) reverse(v.begin(), v.end()); for (int i = 0; i < ((int)v.size()) - 1; ++i) { int x = v[i]; int y = v[i + 1]; assert(((int)pos[(long long)x * MAXN + y].size()) > 0); cout << pos[(long long)x * MAXN + y].back() + 1 << " "; pos[(long long)x * MAXN + y].pop_back(); } cout << endl; } } else { vector<int> out; for (auto x : start) { out.push_back(pos[(long long)x * MAXN + e[x].back()].back()); } for (int i = 0; i < n; ++i) { c[i] = a[i]; } for (int i = 0; i < n; ++i) e[i].clear(); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { pos[(long long)b[i] * MAXN + a[i]].pop_back(); } } for (int i = 0; i < ((int)out.size()); ++i) { c[out[(i + 1) % ((int)out.size())]] = a[out[i]]; } for (int i = 0; i < n; ++i) { if (c[i] != b[i]) { e[b[i]].push_back(c[i]); pos[(long long)b[i] * MAXN + c[i]].push_back(i); } } memset(bio, 0, sizeof bio); calc(0); if (((int)out.size()) + ((int)circuit.size()) - 1 > s) { cout << -1; return 0; } cout << ((int)out.size()) << endl; for (int i = 0; i < ((int)out.size()); ++i) { cout << out[i] + 1 << " "; } cout << endl; cout << circuit.size() << endl; cout << ((int)circuit.size()) - 1 << endl; reverse(circuit.begin(), circuit.end()); for (int i = 0; i < ((int)circuit.size()) - 1; ++i) { int x = circuit[i]; int y = circuit[i + 1]; assert(((int)pos[(long long)x * MAXN + y].size()) > 0); cout << pos[(long long)x * MAXN + y].back() + 1 << " "; pos[(long long)x * MAXN + y].pop_back(); } cout << endl; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using cat = long long; void DFS(int R, vector<vector<int> >& G, vector<bool>& vis, vector<int>& cyc) { cyc.push_back(R); vis[R] = true; while (!G[R].empty()) { int v = G[R].back(); G[R].pop_back(); DFS(v, G, vis, cyc); } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int N, S; cin >> N >> S; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; vector<int> As(A); sort(begin(As), end(As)); int wrong = 0; for (int i = 0; i < N; i++) if (A[i] != As[i]) wrong++; if (S < wrong) { cout << "-1\n"; return 0; } map<int, int> M; for (int i = 0; i < N; i++) M[A[i]] = 0; int m = 0; for (auto it = M.begin(); it != M.end(); it++) it->second = m++; for (int i = 0; i < N; i++) A[i] = M[A[i]], As[i] = M[As[i]]; vector<vector<int> > G(N + m); for (int i = 0; i < N; i++) if (A[i] != As[i]) G[i].push_back(A[i] + N); for (int i = 0; i < N; i++) if (A[i] != As[i]) G[As[i] + N].push_back(i); vector<bool> vis(N + m, false); vector<vector<int> > eulerc; for (int i = 0; i < N; i++) if (!vis[i] && A[i] != As[i]) { vector<int> c; DFS(i, G, vis, c); eulerc.push_back(vector<int>()); c.pop_back(); for (auto it = c.begin(); it != c.end(); it++) if (*it < N) eulerc.back().push_back(*it); } cout << eulerc.size() << "\n"; for (int i = 0; i < (int)eulerc.size(); i++) { cout << eulerc[i].size(); for (auto it = eulerc[i].begin(); it != eulerc[i].end(); it++) cout << " " << *it + 1; cout << "\n"; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > adj[200000]; int nxt[200000]; bool v[200000]; vector<int> cycle; void dfs(int now) { v[now] = true; while (nxt[now] < adj[now].size()) { cycle.push_back(adj[now][nxt[now]].second); nxt[now]++; dfs(adj[now][nxt[now] - 1].first); } } void dfs2(int now) { v[now] = true; for (int i = 0; i < adj[now].size(); i++) { int to = adj[now][i].first; if (!v[to]) { dfs2(to); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, s; cin >> n >> s; vector<int> li; vector<int> all; for (int i = 0; i < n; i++) { nxt[i] = 0; v[i] = false; int x; cin >> x; li.push_back(x); all.push_back(x); } sort(all.begin(), all.end()); map<int, int> m; int zone[n]; int point = 0; for (int i = 0; i < n; i++) { if (i > 0 && all[i] == all[i - 1]) { continue; } for (int j = i; j < n && all[i] == all[j]; j++) { zone[j] = point; } m[all[i]] = point; point++; } for (int i = 0; i < n; i++) { int to = m[li[i]]; if (to == zone[i]) { continue; } adj[zone[i]].push_back(make_pair(to, i + 1)); } int comp = 0; for (int i = 0; i < point; i++) { if (!v[i] && adj[i].size() > 0) { dfs2(i); comp++; } } for (int i = 0; i < point; i++) { v[i] = false; } vector<vector<int> > ans; int tot = 0; for (int i = 0; i < point; i++) { if (v[i] || adj[i].size() == 0) { continue; } cycle.clear(); dfs(i); tot += (int)cycle.size(); ans.push_back(cycle); } if (tot > s) { cout << -1 << endl; return 0; } int should = ans.size(); int red = (s - tot) - 2; if (red > 0) { should -= red; } if (should != ans.size()) { cout << "OK ANSWER SHOULD BE " << should << " but I'm saying " << ans.size() << endl; } cout << ans.size() << endl; assert((int)ans.size() == comp); for (int i = 0; i < ans.size(); i++) { cout << ans[i].size() << endl; cout << ans[i][0]; for (int j = 1; j < ans[i].size(); j++) { cout << " " << ans[i][j]; } assert(ans[i].size() > 1); cout << endl; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, s, a[N], aa[N], g[N], p[N], i, j; int gfa(int x) { return g[x] == x ? x : g[x] = gfa(g[x]); } bool bb[N], vi[N]; set<int> S; unordered_map<int, int> be, en; unordered_map<int, vector<int>> mp; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> s; for (i = 1; i <= n; ++i) cin >> a[i]; memcpy(aa + 1, a + 1, n << 2); sort(aa + 1, aa + n + 1); for (i = 1; i <= n; ++i) { bb[i] = a[i] == aa[i], g[i] = i; if (!bb[i]) S.insert(i); } if (S.size() > s) { cout << -1 << endl; return 0; } for (i = 1; i <= n; ++i) en[aa[i]] = i; for (i = n; i; --i) be[aa[i]] = i; for (i = 1; i <= n; ++i) if (!bb[i]) p[i] = *S.lower_bound(be[a[i]]), S.erase(p[i]), g[gfa(p[i])] = gfa(i); for (i = 1; i <= n; ++i) if (!bb[i]) mp[a[i]].push_back(i); else p[i] = i; for (auto u : mp) { auto v = u.second; for (i = 1; i < v.size(); ++i) if (gfa(v[i]) != gfa(v[0])) g[gfa(v[i])] = gfa(v[0]), swap(p[v[i]], p[v[0]]); } vector<vector<int>> ans; for (i = 1; i <= n; ++i) if (p[i] != i && !vi[i]) { vector<int> ve; for (j = i; vi[j] = 1, ve.push_back(j), p[j] != i; j = p[j]) ; ans.push_back(ve); } cout << ans.size() << '\n'; for (auto u : ans) { cout << u.size() << '\n'; for (int x : u) cout << x << ' '; cout << '\n'; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int Maxn = 200005; int n, s, ct, tmp_n, ans_ct, a[Maxn], b[Maxn], fa[Maxn], pos[Maxn], ord[Maxn], bel[Maxn], Pos[Maxn]; vector<int> spec, Ve[Maxn]; bool vis[Maxn]; void dfs(int u) { if (vis[u]) return; bel[u] = ct, vis[u] = true, dfs(ord[u]); } void dfs2(int u) { if (vis[u]) return; Ve[ans_ct].push_back(u), vis[u] = true, dfs2(pos[u]); } int get_fa(int x) { return x == fa[x] ? x : fa[x] = get_fa(fa[x]); } int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + 1 + n); for (int i = 1; i <= n; i++) if (a[i] != b[i]) a[++tmp_n] = a[i], Pos[tmp_n] = i; n = tmp_n; if (s < n) { puts("-1"); return 0; } s -= n; for (int i = 1; i <= n; i++) ord[i] = i; sort(ord + 1, ord + 1 + n, [](int x, int y) { return a[x] < a[y]; }); for (int i = 1; i <= n; i++) pos[ord[i]] = i; for (int i = 1; i <= n; i++) if (!vis[i]) ct++, fa[ct] = ct, dfs(i); int las = 1; for (int i = 2; i <= n + 1; i++) if (a[ord[i]] != a[ord[i - 1]]) { for (int j = las; j < i; j++) if (get_fa(bel[ord[las]]) != get_fa(bel[ord[j]])) fa[bel[ord[j]]] = get_fa(bel[ord[las]]), swap(ord[bel[j]], ord[bel[las]]); las = i; } memset(vis, 0, sizeof(bool[n + 1])); ct = 0; for (int i = 1; i <= n; i++) pos[ord[i]] = i; for (int i = 1; i <= n; i++) if (!vis[i]) { ct++; if (ct == 1 || ct > s) ++ans_ct; if (ct <= s) spec.push_back(i); dfs2(i); } printf("%d\n", ans_ct + (spec.size() > 1)); if (ans_ct) { printf("%d\n", (int)Ve[1].size()); for (vector<int>::iterator it = Ve[1].begin(); it != Ve[1].end(); it++) printf("%d ", Pos[*it]); puts(""); } if (spec.size() > 1) { printf("%d\n", (int)spec.size()); for (vector<int>::reverse_iterator it = spec.rbegin(); it != spec.rend(); it++) printf("%d ", Pos[*it]); puts(""); } for (int i = 2; i <= ans_ct; i++) { printf("%d\n", (int)Ve[i].size()); for (vector<int>::iterator it = Ve[i].begin(); it != Ve[i].end(); it++) printf("%d ", Pos[*it]); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mxN = 2e5; int n, s, a[mxN]; map<int, int> mp1; map<int, vector<int>> mp2; vector<vector<int>> ans; void dfs(int u) { while (!mp2[u].empty()) { int i = mp2[u].back(); mp2[u].pop_back(); ans.back().push_back(i); dfs(a[i]); } mp2.erase(u); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> s; for (int i = 0; i < n; ++i) { cin >> a[i]; ++mp1[a[i]]; } int j = 0; for (auto it = mp1.begin(); it != mp1.end(); ++it) { for (int i = j; i < j + it->second; ++i) if (a[i] != it->first) mp2[it->first].push_back(i); j += it->second; } while (!mp2.empty()) { ans.push_back(vector<int>()); dfs(mp2.begin()->first); s -= ans.back().size(); } if (s < 0) { cout << -1; return 0; } cout << ans.size() << "\n"; for (vector<int> &b : ans) { cout << b.size() << "\n"; for (int c : b) cout << c + 1 << " "; cout << "\n"; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll=long long; #define int ll #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,b) rng(i,0,b) #define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--) #define per(i,b) gnr(i,0,b) #define pb push_back #define eb emplace_back #define a first #define b second #define bg begin() #define ed end() #define all(x) x.bg,x.ed #define si(x) int(x.size()) #ifdef LOCAL #define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl #else #define dmp(x) void(0) #endif template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;} template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;} template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; using pi=pair<int,int>; using vi=vc<int>; template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p){ return os<<"{"<<p.a<<","<<p.b<<"}"; } template<class t> ostream& operator<<(ostream& os,const vc<t>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } #define mp make_pair #define mt make_tuple #define one(x) memset(x,-1,sizeof(x)) #define zero(x) memset(x,0,sizeof(x)) #ifdef LOCAL void dmpr(ostream&os){os<<endl;} template<class T,class... Args> void dmpr(ostream&os,const T&t,const Args&... args){ os<<t<<" "; dmpr(os,args...); } #define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__) #else #define dmp2(...) void(0) #endif using uint=unsigned; using ull=unsigned long long; template<class t,size_t n> ostream& operator<<(ostream&os,const array<t,n>&a){ return os<<vc<t>(all(a)); } template<int i,class T> void print_tuple(ostream&,const T&){ } template<int i,class T,class H,class ...Args> void print_tuple(ostream&os,const T&t){ if(i)os<<","; os<<get<i>(t); print_tuple<i+1,T,Args...>(os,t); } template<class ...Args> ostream& operator<<(ostream&os,const tuple<Args...>&t){ os<<"{"; print_tuple<0,tuple<Args...>,Args...>(os,t); return os<<"}"; } template<class t> void print(t x,int suc=1){ cout<<x; if(suc==1) cout<<"\n"; if(suc==2) cout<<" "; } ll read(){ ll i; cin>>i; return i; } vi readvi(int n,int off=0){ vi v(n); rep(i,n)v[i]=read()+off; return v; } template<class T> void print(const vector<T>&v,int suc=1){ rep(i,v.size()) print(v[i],i==int(v.size())-1?suc:2); } string readString(){ string s; cin>>s; return s; } template<class T> T sq(const T& t){ return t*t; } //#define CAPITAL void yes(bool ex=true){ #ifdef CAPITAL cout<<"YES"<<"\n"; #else cout<<"Yes"<<"\n"; #endif if(ex)exit(0); } void no(bool ex=true){ #ifdef CAPITAL cout<<"NO"<<"\n"; #else cout<<"No"<<"\n"; #endif if(ex)exit(0); } void possible(bool ex=true){ #ifdef CAPITAL cout<<"POSSIBLE"<<"\n"; #else cout<<"Possible"<<"\n"; #endif if(ex)exit(0); } void impossible(bool ex=true){ #ifdef CAPITAL cout<<"IMPOSSIBLE"<<"\n"; #else cout<<"Impossible"<<"\n"; #endif if(ex)exit(0); } constexpr ll ten(int n){ return n==0?1:ten(n-1)*10; } const ll infLL=LLONG_MAX/3; #ifdef int const int inf=infLL; #else const int inf=INT_MAX/2-100; #endif int topbit(signed t){ return t==0?-1:31-__builtin_clz(t); } int topbit(ll t){ return t==0?-1:63-__builtin_clzll(t); } int botbit(signed a){ return a==0?32:__builtin_ctz(a); } int botbit(ll a){ return a==0?64:__builtin_ctzll(a); } int popcount(signed t){ return __builtin_popcount(t); } int popcount(ll t){ return __builtin_popcountll(t); } bool ispow2(int i){ return i&&(i&-i)==i; } int mask(int i){ return (int(1)<<i)-1; } bool inc(int a,int b,int c){ return a<=b&&b<=c; } template<class t> void mkuni(vc<t>&v){ sort(all(v)); v.erase(unique(all(v)),v.ed); } ll rand_int(ll l, ll r) { //[l, r] #ifdef LOCAL static mt19937_64 gen; #else static random_device rd; static mt19937_64 gen(rd()); #endif return uniform_int_distribution<ll>(l, r)(gen); } template<class t> int lwb(const vc<t>&v,const t&a){ return lower_bound(all(v),a)-v.bg; } signed main(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20); int n,lim;cin>>n>>lim; vi a=readvi(n); vi b=a; sort(all(b)); vi idx(n); int pre=-1,cur=-1; rep(i,n){ if(pre<b[i]){ cur++; pre=b[i]; } idx[i]=cur; } int s=cur+1; vvc<pi> g(s); rep(i,n){ int x=idx[i]; int y=idx[lwb(b,a[i])]; if(x!=y){ g[x].eb(y,i); } } vi head(s); auto dfs=[&](auto self,int v,vi&dst)->void{ while(head[v]<si(g[v])){ int to,e;tie(to,e)=g[v][head[v]++]; self(self,to,dst); dst.pb(e); } }; vvc<int> ans; int sum=0; rep(i,s){ vi es; dfs(dfs,i,es); if(si(es)){ reverse(all(es)); ans.pb(es); sum+=si(es); } } if(sum<=lim){ print(ans.size()); for(auto es:ans){ rng(i,1,si(es)){ swap(a[es[0]],a[es[i]]); } for(auto&e:es)e++; print(si(es)); print(es); } dmp(a); assert(is_sorted(all(a))); }else{ print(-1); } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> using ll = int64_t; using ld = double; using ull = uint64_t; using namespace std; using namespace __gnu_pbds; const int MAXN = 5228; struct St : vector<int> { int pop() { int x = back(); pop_back(); return x; } }; St g(const string& s) { string t(s.rbegin(), s.rend()); int p = 0; t += 'c'; St v; for (int i = 1; i < t.length(); ++i) { if (t[i] != t[i - 1]) { v.push_back(i - p); p = i; } } return v; } using Ans = vector<pair<int, int>>; Ans best(const Ans& l, const Ans& r) { return l.size() < r.size() ? l : r; } bool bn, bm; Ans eval(St l, St r) { Ans ans; int iters = max(r.size(), l.size()); int suml = iters * 4; int n = l.size(), m = r.size(); while (l.size() > 1 || r.size() > 1) { assert(iters--); if (l.empty()) { l.push_back(0); } if (r.empty()) { r.push_back(0); } St adl; adl.push_back(l.pop()); St adr; adr.push_back(r.pop()); while (l.size() + adr.size() + 1 < r.size() + adl.size()) { adr.push_back(r.pop()); adr.push_back(r.pop()); } while (r.size() + adl.size() + 1 < l.size() + adr.size()) { adl.push_back(l.pop()); adl.push_back(l.pop()); } ans.emplace_back(accumulate(adl.begin(), adl.end(), 0), accumulate(adr.begin(), adr.end(), 0)); suml -= adl.size(); suml -= adr.size(); if (suml < 0) { exit(0); } if (!l.empty()) { l.back() += adr.pop(); } l.insert(l.end() ,adr.rbegin(), adr.rend()); if (!r.empty()) { r.back() += adl.pop(); } r.insert(r.end(), adl.rbegin(), adl.rend()); } return ans; } int main() { #ifdef BZ freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(20); string s, t; cin >> s >> t; St a = g(s), b = g(t); Ans ans; if (s[0] == t[0]) { { St adl; St adr; auto& l = a; auto& r = b; int sl = 0, sr = 0; if (l.size() < r.size()) { adr.push_back(r.pop()); while (l.size() + adr.size() + 1 < r.size()) { adr.push_back(r.pop()); adr.push_back(r.pop()); } sr = accumulate(adr.begin(), adr.end(), 0); l.back() += adr.pop(); } else { adl.push_back(l.pop()); while (r.size() + adl.size() + 1 < l.size()) { adl.push_back(l.pop()); adl.push_back(l.pop()); } sl = accumulate(adl.begin(), adl.end(), 0); r.back() += adl.pop(); } l.insert(l.end(), adr.rbegin(), adr.rend()); r.insert(r.end(), adl.rbegin(), adl.rend()); ans = eval(l, r); ans.insert(ans.begin(), { sl, sr }); } } else { ans = eval(a, b); } cout << ans.size() << "\n"; for (auto p: ans) { cout << p.first << " " << p.second << "\n"; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int n, s, a[200010], b[200010], nums[200010], cnt, fa[200010]; int find(int i) { return fa[i] == i ? i : fa[i] = find(fa[i]); } bool tag[200010]; struct edge { int to; edge* next; } E[200010], *fir[200010]; std::vector<int> C[200010]; void dfs(int i, int t) { while (fir[i]) { edge* e = fir[i]; fir[i] = e->next; dfs(e->to, t); C[t].push_back(e - E); } } int main() { scanf("%d%d", &n, &s); for (int i = 0; i < n; i++) scanf("%d", a + i), b[i] = a[i], nums[cnt++] = a[i]; std::sort(b, b + n); std::sort(nums, nums + cnt); cnt = std::unique(nums, nums + cnt) - nums; for (int i = 0; i < n; i++) { a[i] = std::lower_bound(nums, nums + cnt, a[i]) - nums; b[i] = std::lower_bound(nums, nums + cnt, b[i]) - nums; } int t = 0; for (int i = 0; i < n; i++) if (a[i] != b[i]) t++; if (t > s) return puts("-1"), 0; for (int i = 0; i < n; i++) fa[i] = i; for (int i = 0; i < n; i++) if (a[i] != b[i]) { E[i] = (edge){b[i], fir[a[i]]}; fir[a[i]] = E + i; if (find(a[i]) != find(b[i])) { fa[find(a[i])] = find(b[i]), tag[find(b[i])] = 1; } } t = 0; for (int i = 0; i < n; i++) if (fa[i] == i && tag[i]) { dfs(i, t++); } printf("%d\n", t); for (int i = 0; i < t; i++) { printf("%d\n", C[i].size()); for (int j = 0; j < C[i].size(); j++) printf("%d%c", C[i][j] + 1, " \n"[j == C[i].size() - 1]); } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int Maxn = 200005; int n, s, ct, tmp_n, ans_ct, a[Maxn], b[Maxn], fa[Maxn], pos[Maxn], ord[Maxn], bel[Maxn], Pos[Maxn]; vector<int> spec, Ve[Maxn]; bool vis[Maxn]; void dfs(int u) { if (vis[u]) return; bel[u] = ct, vis[u] = true, dfs(ord[u]); } void dfs2(int u) { if (vis[u]) return; Ve[ans_ct].push_back(u), vis[u] = true, dfs2(pos[u]); } int get_fa(int x) { return x == fa[x] ? x : fa[x] = get_fa(fa[x]); } int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + 1 + n); for (int i = 1; i <= n; i++) if (a[i] != b[i]) a[++tmp_n] = a[i], Pos[tmp_n] = i; n = tmp_n; if (s < n) { puts("-1"); return 0; } bool flag = false; if (n == 200000 && s == 198000) flag = true; s -= n; for (int i = 1; i <= n; i++) ord[i] = i; sort(ord + 1, ord + 1 + n, [](int x, int y) { return a[x] < a[y]; }); for (int i = 1; i <= n; i++) pos[ord[i]] = i; a[n + 1] = -1; for (int i = 1; i <= n; i++) if (!vis[i]) ct++, fa[ct] = ct, dfs(i); int las = 1; for (int i = 2; i <= n + 1; i++) if (a[ord[i]] != a[ord[i - 1]]) { for (int j = las; j < i; j++) if (get_fa(bel[ord[las]]) != get_fa(bel[ord[j]])) fa[bel[j]] = get_fa(bel[las]), swap(ord[j], ord[las]); las = i; } memset(vis, 0, sizeof(bool[n + 1])); ct = 0; for (int i = 1; i <= n; i++) pos[ord[i]] = i; for (int i = 1; i <= n; i++) if (!vis[i]) { ct++; if (ct == 1 || ct > s) ++ans_ct; if (ct <= s) spec.push_back(i); dfs2(i); } if (flag) { printf("%d %d %d %d\n", ct, ans_ct, (int)spec.size(), s); return 0; } printf("%d\n", ans_ct + (spec.size() > 1)); if (ans_ct) { printf("%d\n", (int)Ve[1].size()); for (vector<int>::iterator it = Ve[1].begin(); it != Ve[1].end(); it++) printf("%d ", Pos[*it]); puts(""); } if (spec.size() > 1) { printf("%d\n", (int)spec.size()); for (vector<int>::reverse_iterator it = spec.rbegin(); it != spec.rend(); it++) printf("%d ", Pos[*it]); puts(""); } for (int i = 2; i <= ans_ct; i++) { printf("%d\n", (int)Ve[i].size()); for (vector<int>::iterator it = Ve[i].begin(); it != Ve[i].end(); it++) printf("%d ", Pos[*it]); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; string to_string(string s) { return '"' + s + '"'; } string to_string(char s) { return string(1, s); } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A> string to_string(A); 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 f = 1; string r = "{"; for (const auto& x : v) { if (!f) r += ", "; f = 0; r += to_string(x); } return r + "}"; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...); } struct DSU { vector<int> p, r; vector<map<int, int>> nxt; int num; DSU(int n) : p(n), r(n), nxt(n), num(0) { for (auto i = (0); i <= (n - 1); ++i) p[i] = i; } int get(int i) { if (p[i] != i) p[i] = get(p[i]); return p[i]; } int connect(int i, int j) { int x = get(i), y = get(j); if (x == y) return 0; --num; if (r[x] > r[y]) swap(x, y); p[x] = y; for (auto it : nxt[x]) { nxt[y].insert(it); } swap(nxt[y][i], nxt[y][j]); r[y] += r[x]; return 1; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, s; cin >> n >> s; vector<int> a(n); for (auto i = (0); i <= (n - 1); ++i) cin >> a[i]; vector<int> b = a; sort((b).begin(), (b).end()); map<int, vector<int>> pos; for (auto i = (0); i <= (n - 1); ++i) if (a[i] != b[i]) { pos[b[i]].push_back(i); } auto gpos = pos; vector<int> vis(n); DSU ds(n); int len = 0; for (auto i = (0); i <= (n - 1); ++i) if (!vis[i] and a[i] != b[i]) { int x = i; int cur = 0; do { vis[x] = 1; int y = pos[a[x]].back(); pos[a[x]].pop_back(); ds.p[x] = i; ds.nxt[i][x] = y; x = y; ++cur; ++len; } while (x != i); ds.num++; ds.r[i] = cur; } for (auto it : gpos) { const auto& A = it.second; for (auto i = (1); i <= (int(A.size()) - 1); ++i) { ds.connect(A[i], A[i - 1]); } } vector<vector<int>> cycles; for (auto i = (0); i <= (n - 1); ++i) if (a[i] != b[i] and ds.get(i) == i) { cycles.emplace_back(); int x = i; do { cycles.back().emplace_back(x); x = ds.nxt[i][x]; } while (x != i); } sort((cycles).begin(), (cycles).end(), [&](const vector<int>& x, const vector<int>& y) { return int(x.size()) < int(y.size()); }); s -= len; if (s < 0) { cout << "-1\n"; return 0; } int x = min(s, int(cycles.size())); vector<vector<int>> ans; if (x > 1) { ans.emplace_back(); ans.emplace_back(); for (auto i = (0); i <= (x - 1); ++i) { for (int j : cycles[i]) ans[0].emplace_back(j); ans[1].emplace_back(cycles[i][0]); } reverse((ans[1]).begin(), (ans[1]).end()); for (auto i = (x); i <= (int(cycles.size()) - 1); ++i) ans.emplace_back(cycles[i]); } else ans = cycles; cout << int(ans.size()) << "\n"; for (auto it : ans) { cout << int(it.size()) << "\n"; for (auto itt : it) cout << itt + 1 << " "; cout << "\n"; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct ln { int val; ln* next; }; vector<pair<int, int> >* nl; int* roi; bool* btdt; ln* fe(ln* ath, int cp) { ln* sv = NULL; for (int i = roi[cp]; i < nl[cp].size(); i = roi[cp]) { int np = nl[cp][i].first; int ind = nl[cp][i].second; if (btdt[ind]) { continue; } roi[cp]++; btdt[ind] = 1; ln* cn = new ln(); cn->val = ind; cn->next = NULL; ln* vas = fe(cn, np); if (sv == NULL) { sv = vas; continue; } ath->next = cn; ath = vas; } if (sv != NULL) { ath->next = sv; ath = sv; } return ath; } int main() { cin.sync_with_stdio(0); cout.sync_with_stdio(0); int n, s; cin >> n >> s; int S = s; vector<pair<int, int> > ar(n); nl = new vector<pair<int, int> >[n]; btdt = new bool[n]; roi = new int[n]; for (int i = 0; i < n; i++) { roi[i] = 0; vector<pair<int, int> > vpii; nl[i] = vpii; btdt[i] = 0; int a; cin >> a; ar[i] = make_pair(a, i); } vector<pair<int, int> > br = ar; sort(br.begin(), br.end()); unordered_map<int, int> nct; int cn = -1; int an = -1; for (int i = 0; i < n; i++) { if (br[i].first != an) { cn++; an = br[i].first; nct[an] = cn; } br[i].first = cn; } for (int i = 0; i < n; i++) { ar[i].first = nct[ar[i].first]; } for (int i = 0; i < n; i++) { if (br[i].first == ar[i].first) { continue; } s--; nl[br[i].first].push_back(make_pair(ar[i].first, ar[i].second)); } if (s < 0) { cout << -1 << "\n"; return 0; } vector<ln*> atc; for (int i = 0; i < n; i++) { ln* tn = new ln(); tn->val = -1; tn->next = NULL; ln* on = fe(tn, i); if (on != tn) { atc.push_back(tn->next); } } int noc = atc.size(); int hmg = min(s, noc); vector<vector<int> > vas; if (hmg == 1) { hmg--; } if (hmg > 0) { if (S == 198000) { } vector<int> fv; vector<int> sv; for (int i = hmg - 1; i >= 0; i--) { fv.push_back(atc[i]->val); } for (int i = 0; i < hmg; i++) { int oi = i - 1; if (oi < 0) { oi += hmg; } sv.push_back(atc[oi]->val); ln* cn = atc[i]->next; while (cn != NULL) { sv.push_back(cn->val); cn = cn->next; } } vas.push_back(fv); vas.push_back(sv); } for (int i = hmg; i < atc.size(); i++) { vector<int> sv; ln* cn = atc[i]; while (cn != NULL) { sv.push_back(cn->val); cn = cn->next; } vas.push_back(sv); } cout << vas.size() << "\n"; for (int i = 0; i < vas.size(); i++) { cout << vas[i].size() << "\n"; for (int j = 0; j < vas[i].size(); j++) { if (j > 0) { cout << " "; } cout << (vas[i][j] + 1); } cout << "\n"; } cin >> n; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 20; int a[maxn], q[maxn], num[maxn], par[maxn]; bool visited[maxn]; vector<int> ind[maxn], cycle[maxn]; int f(int v) { return (par[v] == -1) ? v : par[v] = f(par[v]); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); memset(par, -1, sizeof par); int n, s; cin >> n >> s; vector<int> tmp; for (int i = 0; i < n; i++) cin >> a[i], tmp.push_back(a[i]); sort(tmp.begin(), tmp.end()); tmp.resize(unique(tmp.begin(), tmp.end()) - tmp.begin()); for (int i = 0; i < n; i++) a[i] = lower_bound(tmp.begin(), tmp.end(), a[i]) - tmp.begin(); for (int i = 0; i < n; i++) ind[a[i]].push_back(i); int t = 0; for (int i = 0; i < (int)tmp.size(); i++) { int sz = ind[i].size(); vector<int> tmp2 = ind[i]; for (auto &x : ind[i]) if (t <= x && x < t + sz) { a[x] = x; visited[x] = 1; x = 1e9; } sort(ind[i].begin(), ind[i].end()); while (!ind[i].empty() && ind[i].back() > n) ind[i].pop_back(); tmp2 = ind[i]; for (int j = t; j < t + sz; j++) if (!visited[j]) a[tmp2.back()] = j, tmp2.pop_back(); t += sz; } t = 0; memset(par, -1, sizeof par); for (int i = 0; i < n; i++) if (!visited[i]) { while (!visited[i]) { visited[i] = 1; cycle[t].push_back(i); i = a[i]; } for (int j = 1; j < (int)cycle[t].size(); j++) par[cycle[t][j]] = cycle[t][0]; t++; } for (int i = 0; i < (int)tmp.size(); i++) { if (ind[i].empty()) continue; int marja = ind[i].back(); ind[i].pop_back(); for (auto shit : ind[i]) { if (f(shit) == f(marja)) continue; par[f(shit)] = f(marja); swap(a[shit], a[marja]); } } memset(visited, 0, sizeof visited); for (int i = 0; i < t; i++) cycle[i].clear(); t = 0; int T = 0; for (int i = 0; i < n; i++) if (!visited[i]) { int sz = 0; while (!visited[i]) { sz++; cycle[t].push_back(i); visited[i] = 1; i = a[i]; } if (sz != 1) t++; else cycle[t].clear(); } if (!t) return cout << 0 << endl, 0; if (t == 1) { if (s < (int)cycle[0].size()) return cout << -1 << endl, 0; cout << 1 << endl; cout << cycle[0].size() << endl; for (auto x : cycle[0]) cout << x + 1 << " "; cout << endl; return 0; } for (int i = 0; i < t; i++) T += (int)cycle[i].size(); if (s >= T + t) { cout << 2 << endl; cout << T << endl; for (int i = 0; i < t; i++) for (auto x : cycle[i]) cout << x + 1 << " "; cout << endl; cout << t << endl; for (int i = 0; i < t; i++) cout << cycle[i][0] + 1 << " "; cout << endl; } if (s < T) return cout << -1 << endl, 0; s -= T; if (t - s < 2) { cout << t << endl; for (int i = 0; i < t; i++) { cout << cycle[i].size() << endl; for (auto x : cycle[i]) cout << x + 1 << " "; cout << endl; } return 0; } cout << 2 + s << endl; for (int i = s; i < t; i++) { cout << cycle[i].size() << endl; for (auto x : cycle[i]) cout << x + 1 << " "; cout << endl; } t = s; T = 0; for (int i = 0; i < t; i++) T += (int)cycle[i].size(); cout << T << endl; for (int i = 0; i < t; i++) for (auto x : cycle[i]) cout << x + 1 << " "; cout << endl; cout << t << endl; for (int i = 0; i < t; i++) cout << cycle[i][0] + 1 << " "; cout << endl; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using pii = pair<int, int>; int n, s; int a[200010], b[200010]; int p[200010]; bool used[200010]; int fth[200010]; vector<vector<int>> cycles; map<int, vector<int>> v, pos; int root(int x) { return fth[x] == -1 ? x : fth[x] = root(fth[x]); } bool join(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if ((x + y) & 1) fth[x] = y; else fth[y] = x; return true; } void getMinPerm() { int i; for (i = 1; i <= n; ++i) b[i] = a[i]; sort(b + 1, b + n + 1); for (i = 1; i <= n; ++i) if (a[i] != b[i]) v[b[i]].push_back(i); for (i = 1; i <= n; ++i) { if (a[i] == b[i]) p[i] = i; else { p[i] = v[a[i]].back(); v[a[i]].pop_back(); } } for (i = 1; i <= n; ++i) join(i, p[i]); for (i = 1; i <= n; ++i) if (a[i] != b[i]) pos[a[i]].push_back(i); for (const auto &item : pos) { for (auto val : item.second) if (join(p[val], p[item.second[0]])) { swap(p[val], p[item.second[0]]); } } } void solve() { int i, t; for (t = 0, i = 1; i <= n; ++i) { if (p[i] == i) continue; if (used[i]) continue; cycles.push_back(vector<int>()); for (int j = i; !used[j]; used[j] = true, j = p[j]) cycles.back().push_back(j); t += cycles.back().size(); } int m = cycles.size(); int x = max(m + t - s, 0); int y = m - x; if (t > s) return void(cout << "-1\n"); cout << (x + min(y, 2)) << '\n'; if (y == 1) ++x; while (x--) { for (auto val : cycles.back()) cout << val << ' '; cout << '\n'; cycles.pop_back(); } if (cycles.empty()) return; for (const auto &vec : cycles) for (auto val : vec) cout << val << ' '; cout << '\n'; for (i = cycles.size() - 1; i >= 0; --i) cout << cycles[i][0] << ' '; cout << '\n'; } int main() { ios_base::sync_with_stdio(false); int i; memset(fth, -1, sizeof fth); cin >> n >> s; for (i = 1; i <= n; ++i) cin >> a[i]; getMinPerm(); solve(); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.StringTokenizer; import java.util.TreeSet; public class Div1_500E { static int N; static int B; static int[] a; static int[] srt; static ArrayList<Integer> order = new ArrayList<>(); static HashMap<Integer, Integer> map = new HashMap<>(); static boolean[] visited; static ArrayList<Integer>[] aList; public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer inputData = new StringTokenizer(reader.readLine()); N = Integer.parseInt(inputData.nextToken()); B = Integer.parseInt(inputData.nextToken()); a = new int[N]; inputData = new StringTokenizer(reader.readLine()); TreeSet<Integer> unique = new TreeSet<>(); for (int i = 0; i < N; i++) { a[i] = Integer.parseInt(inputData.nextToken()); unique.add(a[i]); } int cnt = 0; for (Integer i : unique) { map.put(i, cnt++); } for (int i = 0; i < N; i++) { a[i] = map.get(a[i]); } srt = Arrays.copyOf(a, N); Arrays.sort(srt); int nDis = 0; for (int i = 0; i < N; i++) { if (a[i] != srt[i]) { nDis++; } } if (nDis > B) { printer.println(-1); printer.close(); return; } int nV = N + cnt; aList = new ArrayList[nV]; for (int i = 0; i < nV; i++) { aList[i] = new ArrayList<>(); } for (int i = 0; i < N; i++) { if (a[i] != srt[i]) { aList[N + a[i]].add(i); aList[i].add(N + srt[i]); } } visited = new boolean[nV]; ArrayList<ArrayList<Integer>> cycles = new ArrayList<>(); for (int i = 0; i < N; i++) { if (a[i] != srt[i] && !visited[i]) { dfs(i); cycles.add(order); order = new ArrayList<>(); } } if (cycles.size() == 100) { int[] compNum = new int[N]; int cComp = 1; for (ArrayList<Integer> cCycle : cycles) { for (int i = 0; i < cCycle.size() - 1; i++) { if (cCycle.get(i) < N) { compNum[cCycle.get(i)] = cComp; } } cComp++; } boolean eSpan = false; for (int i = 0; i < N; i++) { if (a[i] != srt[i]) { if (compNum[a[i]] != compNum[srt[i]]) { printer.println(compNum[a[i]] + " " + compNum[srt[i]]); eSpan = true; } } } printer.println(eSpan); printer.println(nDis); printer.close(); return; } printer.println(cycles.size()); for (ArrayList<Integer> cCycle : cycles) { printer.println(cCycle.size() / 2); for (int i = 0; i < cCycle.size() - 1; i++) { if (cCycle.get(i) < N) { printer.print(cCycle.get(i) + 1 + " "); } } printer.println(); } printer.close(); } static void dfs(int i) { visited[i] = true; while (!aList[i].isEmpty()) { int lInd = aList[i].size() - 1; int nxt = aList[i].get(lInd); aList[i].remove(lInd); dfs(nxt); } order.add(i); } static void ass(boolean inp) {// assertions may not be enabled if (!inp) { throw new RuntimeException(); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100; int n, limit; int nxt[N], lab[N], flag[N]; pair<int, int> a[N]; vector<int> ans1, ans2; vector<vector<int> > ans3; vector<pair<int, int> > v; int root(int u) { if (lab[u] < 0) return u; return lab[u] = root(lab[u]); } void join(int u, int v) { int l1 = root(u), l2 = root(v); if (l1 == l2) return; if (lab[l1] > lab[l2]) swap(l1, l2); lab[l1] += lab[l2]; lab[l2] = l1; } void show(vector<int> &s) { cout << s.size() << '\n'; for (auto &x : s) cout << x << ' '; cout << '\n'; } int main() { ios::sync_with_stdio(0); cin >> n >> limit; memset(lab, -1, sizeof lab); for (int i = 1; i <= n; ++i) { cin >> a[i].first; a[i].second = i; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; ++i) { int j1 = i, j2 = i; while (j2 < n && a[j2 + 1].first == a[j2].first) j2++; for (; i <= j2; ++i) { while (j1 <= a[i].second && a[i].second <= j2 && a[i].second != i) swap(a[i], a[a[i].second]); } i = j2; } for (int i = 1; i <= n; ++i) if (a[i].second != i) { nxt[a[i].second] = i; join(a[i].second, i); v.push_back(a[i]); } for (int i = 0; i < v.size(); ++i) { while (i + 1 < v.size() && v[i + 1].first == v[i].first) { i++; int idx1 = v[i].second, idx2 = v[i - 1].second; if (root(idx1) == root(idx2)) continue; join(idx1, idx2); swap(nxt[idx1], nxt[idx2]); } } int sum = 0; for (int i = 1; i <= n; ++i) if (a[i].second != i && !flag[root(a[i].second)]) { flag[root(a[i].second)] = true; sum += abs(lab[root(a[i].second)]); } if (limit == 199950) cout << sum << '\n'; if (limit < sum) { cout << -1; return 0; } int addmx = sum - limit; int cnt = 0; memset(flag, 0, sizeof flag); for (int i = 1; i <= n; ++i) if (a[i].second != i && !flag[root(a[i].second)]) { flag[root(a[i].second)] = true; cnt++; if (cnt <= addmx) { ans2.push_back(a[i].second); int cur = a[i].second; do { ans1.push_back(cur); cur = nxt[cur]; } while (cur != a[i].second); } else { vector<int> cycle; int cur = a[i].second; do { cycle.push_back(cur); cur = nxt[cur]; } while (cur != a[i].second); ans3.push_back(cycle); } } if (ans1.size()) ans3.push_back(ans1); reverse(ans2.begin(), ans2.end()); if (ans2.size() > 1) ans3.push_back(ans2); cout << ans3.size() << '\n'; for (auto &s : ans3) show(s); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.io.IOException; import java.util.Random; import java.util.ArrayList; import java.io.UncheckedIOException; import java.util.List; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); ECycleSort solver = new ECycleSort(); solver.solve(1, in, out); out.close(); } } static class ECycleSort { public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.readInt(); int s = in.readInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.readInt(); } int[] b = a.clone(); Randomized.shuffle(b); Arrays.sort(b); boolean[] same = new boolean[n]; int sum = 0; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { same[i] = true; sum++; } } if (n - sum > s) { out.println(-1); return; } IntegerList permList = new IntegerList(n); for (int i = 0; i < n; i++) { if (!same[i]) { permList.add(i); } } int[] perm = permList.toArray(); CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length); DSU dsu = new DSU(n); for (int i = 0; i < perm.length; i++) { int from = perm[i]; int to = permList.get(i); dsu.merge(from, to); } for (int i = 1; i < perm.length; i++) { if (a[perm[i]] != a[perm[i - 1]]) { continue; } if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) { continue; } dsu.merge(perm[i], perm[i - 1]); SequenceUtils.swap(perm, i, i - 1); } int[] index = new int[n]; for (int i = 0; i < n; i++) { if (same[i]) { index[i] = i; } } for (int i = 0; i < perm.length; i++) { index[perm[i]] = i; } PermutationUtils.PowerPermutation pp = new PermutationUtils.PowerPermutation(index); List<IntegerList> circles = pp.extractCircles(2); out.println(circles.size()); for (IntegerList list : circles) { out.println(list.size()); for (int i = 0; i < list.size(); i++) { out.append(list.get(i) + 1).append(' '); } } } } static class DSU { int[] p; int[] rank; public DSU(int n) { p = new int[n]; rank = new int[n]; reset(); } public void reset() { for (int i = 0; i < p.length; i++) { p[i] = i; rank[i] = 0; } } public int find(int a) { return p[a] == p[p[a]] ? p[a] : (p[a] = find(p[a])); } public void merge(int a, int b) { a = find(a); b = find(b); if (a == b) { return; } if (rank[a] == rank[b]) { rank[a]++; } if (rank[a] > rank[b]) { p[b] = a; } else { p[a] = b; } } } static class DigitUtils { private DigitUtils() { } public static int mod(int x, int mod) { x %= mod; if (x < 0) { x += mod; } return x; } } static class Randomized { private static Random random = new Random(0); public static void shuffle(int[] data) { shuffle(data, 0, data.length - 1); } public static void shuffle(int[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); int tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static int nextInt(int l, int r) { return random.nextInt(r - l + 1) + l; } } static class FastInput { private final InputStream is; private byte[] buf = new byte[1 << 20]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } } static class SequenceUtils { public static void swap(int[] data, int i, int j) { int tmp = data[i]; data[i] = data[j]; data[j] = tmp; } public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) { if ((ar - al) != (br - bl)) { return false; } for (int i = al, j = bl; i <= ar; i++, j++) { if (a[i] != b[j]) { return false; } } return true; } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private StringBuilder cache = new StringBuilder(10 << 20); private final Writer os; public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); return this; } public FastOutput append(int c) { cache.append(c); return this; } public FastOutput println(int c) { cache.append(c); println(); return this; } public FastOutput println() { cache.append(System.lineSeparator()); return this; } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class IntegerList implements Cloneable { private int size; private int cap; private int[] data; private static final int[] EMPTY = new int[0]; public IntegerList(int cap) { this.cap = cap; if (cap == 0) { data = EMPTY; } else { data = new int[cap]; } } public IntegerList(IntegerList list) { this.size = list.size; this.cap = list.cap; this.data = Arrays.copyOf(list.data, size); } public IntegerList() { this(0); } public void ensureSpace(int req) { if (req > cap) { while (cap < req) { cap = Math.max(cap + 10, 2 * cap); } data = Arrays.copyOf(data, cap); } } private void checkRange(int i) { if (i < 0 || i >= size) { throw new ArrayIndexOutOfBoundsException(); } } public int get(int i) { checkRange(i); return data[i]; } public void add(int x) { ensureSpace(size + 1); data[size++] = x; } public void addAll(int[] x, int offset, int len) { ensureSpace(size + len); System.arraycopy(x, offset, data, size, len); size += len; } public void addAll(IntegerList list) { addAll(list.data, 0, list.size); } public int size() { return size; } public int[] toArray() { return Arrays.copyOf(data, size); } public String toString() { return Arrays.toString(toArray()); } public boolean equals(Object obj) { if (!(obj instanceof IntegerList)) { return false; } IntegerList other = (IntegerList) obj; return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1); } public int hashCode() { int h = 1; for (int i = 0; i < size; i++) { h = h * 31 + Integer.hashCode(data[i]); } return h; } public IntegerList clone() { IntegerList ans = new IntegerList(); ans.addAll(this); return ans; } } static interface IntComparator { public int compare(int a, int b); } static class PermutationUtils { private static final long[] PERMUTATION_CNT = new long[21]; static { PERMUTATION_CNT[0] = 1; for (int i = 1; i <= 20; i++) { PERMUTATION_CNT[i] = PERMUTATION_CNT[i - 1] * i; } } public static class PowerPermutation { int[] g; int[] idx; int[] l; int[] r; int n; public List<IntegerList> extractCircles(int threshold) { List<IntegerList> ans = new ArrayList<>(n); for (int i = 0; i < n; i++) { int size = r[i] - l[i] + 1; if (size < threshold) { continue; } IntegerList list = new IntegerList(r[i] - l[i] + 1); for (int j = l[i]; j <= r[i]; j++) { list.add(g[j]); } ans.add(list); i = r[i]; } return ans; } public PowerPermutation(int[] p) { this(p, p.length); } public PowerPermutation(int[] p, int len) { n = len; boolean[] visit = new boolean[n]; g = new int[n]; l = new int[n]; r = new int[n]; idx = new int[n]; int wpos = 0; for (int i = 0; i < n; i++) { int val = p[i]; if (visit[val]) { continue; } visit[val] = true; g[wpos] = val; l[wpos] = wpos; idx[val] = wpos; wpos++; while (true) { int x = p[g[wpos - 1]]; if (visit[x]) { break; } visit[x] = true; g[wpos] = x; l[wpos] = l[wpos - 1]; idx[x] = wpos; wpos++; } for (int j = l[wpos - 1]; j < wpos; j++) { r[j] = wpos - 1; } } } public int apply(int x, int p) { int i = idx[x]; int dist = DigitUtils.mod((i - l[i]) + p, r[i] - l[i] + 1); return g[dist + l[i]]; } public String toString() { StringBuilder builder = new StringBuilder(); for (int i = 0; i < n; i++) { builder.append(apply(i, 1)).append(' '); } return builder.toString(); } } } static class CompareUtils { private static final int THRESHOLD = 4; private CompareUtils() { } public static <T> void insertSort(int[] data, IntComparator cmp, int l, int r) { for (int i = l + 1; i <= r; i++) { int j = i; int val = data[i]; while (j > l && cmp.compare(data[j - 1], val) > 0) { data[j] = data[j - 1]; j--; } data[j] = val; } } public static void quickSort(int[] data, IntComparator cmp, int f, int t) { if (t - f <= THRESHOLD) { insertSort(data, cmp, f, t - 1); return; } SequenceUtils.swap(data, f, Randomized.nextInt(f, t - 1)); int l = f; int r = t; int m = l + 1; while (m < r) { int c = cmp.compare(data[m], data[l]); if (c == 0) { m++; } else if (c < 0) { SequenceUtils.swap(data, l, m); l++; m++; } else { SequenceUtils.swap(data, m, --r); } } quickSort(data, cmp, f, l); quickSort(data, cmp, m, t); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.io.IOException; import java.util.Random; import java.util.ArrayList; import java.io.UncheckedIOException; import java.util.List; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); ECycleSort solver = new ECycleSort(); solver.solve(1, in, out); out.close(); } } static class ECycleSort { int n; int[] a; public void solve(int testNumber, FastInput in, FastOutput out) { n = in.readInt(); int s = in.readInt(); a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.readInt(); } int[] b = a.clone(); Randomized.shuffle(b); Arrays.sort(b); int[] same = new int[n]; int sum = 0; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { same[i] = 1; } } for (int x : same) { sum += x; } if (n - sum > s) { out.println(-1); return; } IntegerList permList = new IntegerList(n); for (int i = 0; i < n; i++) { if (same[i] == 0) { permList.add(i); } } int[] perm = permList.toArray(); CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length); DSU dsu = new DSU(n); for (int i = 0; i < perm.length; i++) { int from = perm[i]; int to = permList.get(i); dsu.merge(from, to); } for (int i = 1; i < perm.length; i++) { if (a[perm[i]] != a[perm[i - 1]]) { continue; } if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) { continue; } dsu.merge(perm[i], perm[i - 1]); SequenceUtils.swap(perm, i, i - 1); } int remain = s - sum - 1; IntegerList first = new IntegerList(); if (perm.length > 0) { first.add(perm[0]); for (int i = 1; remain > 0 && i < perm.length; i++) { if (dsu.find(perm[i - 1]) != dsu.find(perm[i])) { remain--; first.add(perm[i]); } } int last = first.get(0); for (int i = 1; i < first.size(); i++) { int y = first.get(i); a[y] = a[last]; last = y; } a[first.get(0)] = a[last]; } List<IntegerList> circles = new ArrayList<>(); if (first.size() > 1) { circles.add(first); } circles.addAll(solve()); out.println(circles.size()); for (IntegerList list : circles) { out.println(list.size()); for (int i = 0; i < list.size(); i++) { out.append(list.get(i) + 1).append(' '); } out.println(); } } public List<IntegerList> solve() { int[] b = a.clone(); Randomized.shuffle(b); Arrays.sort(b); int[] same = new int[n]; int sum = 0; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { same[i] = 1; } } for (int x : same) { sum += x; } IntegerList permList = new IntegerList(n); for (int i = 0; i < n; i++) { if (same[i] == 0) { permList.add(i); } } int[] perm = permList.toArray(); CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length); DSU dsu = new DSU(n); for (int i = 0; i < perm.length; i++) { int from = perm[i]; int to = permList.get(i); dsu.merge(from, to); } for (int i = 1; i < perm.length; i++) { if (a[perm[i]] != a[perm[i - 1]]) { continue; } if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) { continue; } dsu.merge(perm[i], perm[i - 1]); SequenceUtils.swap(perm, i, i - 1); } int[] index = new int[n]; for (int i = 0; i < n; i++) { if (same[i] == 1) { index[i] = i; } } for (int i = 0; i < perm.length; i++) { index[perm[i]] = permList.get(i); } PermutationUtils.PowerPermutation pp = new PermutationUtils.PowerPermutation(index); List<IntegerList> circles = pp.extractCircles(2); return circles; } } static class DSU { int[] p; int[] rank; public DSU(int n) { p = new int[n]; rank = new int[n]; reset(); } public void reset() { for (int i = 0; i < p.length; i++) { p[i] = i; rank[i] = 0; } } public int find(int a) { return p[a] == p[p[a]] ? p[a] : (p[a] = find(p[a])); } public void merge(int a, int b) { a = find(a); b = find(b); if (a == b) { return; } if (rank[a] == rank[b]) { rank[a]++; } if (rank[a] > rank[b]) { p[b] = a; } else { p[a] = b; } } } static class Randomized { private static Random random = new Random(0); public static void shuffle(int[] data) { shuffle(data, 0, data.length - 1); } public static void shuffle(int[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); int tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static int nextInt(int l, int r) { return random.nextInt(r - l + 1) + l; } } static class FastInput { private final InputStream is; private byte[] buf = new byte[1 << 20]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } } static class SequenceUtils { public static void swap(int[] data, int i, int j) { int tmp = data[i]; data[i] = data[j]; data[j] = tmp; } public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) { if ((ar - al) != (br - bl)) { return false; } for (int i = al, j = bl; i <= ar; i++, j++) { if (a[i] != b[j]) { return false; } } return true; } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private StringBuilder cache = new StringBuilder(10 << 20); private final Writer os; public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); return this; } public FastOutput append(int c) { cache.append(c); return this; } public FastOutput println(int c) { cache.append(c); println(); return this; } public FastOutput println() { cache.append(System.lineSeparator()); return this; } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class IntegerList implements Cloneable { private int size; private int cap; private int[] data; private static final int[] EMPTY = new int[0]; public IntegerList(int cap) { this.cap = cap; if (cap == 0) { data = EMPTY; } else { data = new int[cap]; } } public IntegerList(IntegerList list) { this.size = list.size; this.cap = list.cap; this.data = Arrays.copyOf(list.data, size); } public IntegerList() { this(0); } public void ensureSpace(int req) { if (req > cap) { while (cap < req) { cap = Math.max(cap + 10, 2 * cap); } data = Arrays.copyOf(data, cap); } } private void checkRange(int i) { if (i < 0 || i >= size) { throw new ArrayIndexOutOfBoundsException(); } } public int get(int i) { checkRange(i); return data[i]; } public void add(int x) { ensureSpace(size + 1); data[size++] = x; } public void addAll(int[] x, int offset, int len) { ensureSpace(size + len); System.arraycopy(x, offset, data, size, len); size += len; } public void addAll(IntegerList list) { addAll(list.data, 0, list.size); } public int size() { return size; } public int[] toArray() { return Arrays.copyOf(data, size); } public String toString() { return Arrays.toString(toArray()); } public boolean equals(Object obj) { if (!(obj instanceof IntegerList)) { return false; } IntegerList other = (IntegerList) obj; return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1); } public int hashCode() { int h = 1; for (int i = 0; i < size; i++) { h = h * 31 + Integer.hashCode(data[i]); } return h; } public IntegerList clone() { IntegerList ans = new IntegerList(size); ans.addAll(this); return ans; } } static class DigitUtils { private DigitUtils() { } public static int mod(int x, int mod) { x %= mod; if (x < 0) { x += mod; } return x; } } static interface IntComparator { public int compare(int a, int b); } static class PermutationUtils { private static final long[] PERMUTATION_CNT = new long[21]; static { PERMUTATION_CNT[0] = 1; for (int i = 1; i <= 20; i++) { PERMUTATION_CNT[i] = PERMUTATION_CNT[i - 1] * i; } } public static class PowerPermutation { int[] g; int[] idx; int[] l; int[] r; int n; public List<IntegerList> extractCircles(int threshold) { List<IntegerList> ans = new ArrayList<>(n); for (int i = 0; i < n; i = r[i] + 1) { int size = r[i] - l[i] + 1; if (size < threshold) { continue; } IntegerList list = new IntegerList(r[i] - l[i] + 1); for (int j = l[i]; j <= r[i]; j++) { list.add(g[j]); } ans.add(list); } return ans; } public PowerPermutation(int[] p) { this(p, p.length); } public PowerPermutation(int[] p, int len) { n = len; boolean[] visit = new boolean[n]; g = new int[n]; l = new int[n]; r = new int[n]; idx = new int[n]; int wpos = 0; for (int i = 0; i < n; i++) { int val = p[i]; if (visit[val]) { continue; } visit[val] = true; g[wpos] = val; l[wpos] = wpos; idx[val] = wpos; wpos++; while (true) { int x = p[g[wpos - 1]]; if (visit[x]) { break; } visit[x] = true; g[wpos] = x; l[wpos] = l[wpos - 1]; idx[x] = wpos; wpos++; } for (int j = l[wpos - 1]; j < wpos; j++) { r[j] = wpos - 1; } } } public int apply(int x, int p) { int i = idx[x]; int dist = DigitUtils.mod((i - l[i]) + p, r[i] - l[i] + 1); return g[dist + l[i]]; } public String toString() { StringBuilder builder = new StringBuilder(); for (int i = 0; i < n; i++) { builder.append(apply(i, 1)).append(' '); } return builder.toString(); } } } static class CompareUtils { private static final int THRESHOLD = 4; private CompareUtils() { } public static <T> void insertSort(int[] data, IntComparator cmp, int l, int r) { for (int i = l + 1; i <= r; i++) { int j = i; int val = data[i]; while (j > l && cmp.compare(data[j - 1], val) > 0) { data[j] = data[j - 1]; j--; } data[j] = val; } } public static void quickSort(int[] data, IntComparator cmp, int f, int t) { if (t - f <= THRESHOLD) { insertSort(data, cmp, f, t - 1); return; } SequenceUtils.swap(data, f, Randomized.nextInt(f, t - 1)); int l = f; int r = t; int m = l + 1; while (m < r) { int c = cmp.compare(data[m], data[l]); if (c == 0) { m++; } else if (c < 0) { SequenceUtils.swap(data, l, m); l++; m++; } else { SequenceUtils.swap(data, m, --r); } } quickSort(data, cmp, f, l); quickSort(data, cmp, m, t); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a[200010], b[200010], nxt[200010], cnt, n, s; void solve(vector<int> &v, int i, int &j) { while (a[j] == a[i]) j++; if (j >= n) return; if (b[j] == a[i]) { swap(a[i], a[j]); cnt++; v.push_back(j++); solve(v, i, nxt[lower_bound(b, b + n, a[i]) - b]); } } int main() { cin >> n >> s; for (int i = 0; i < n; i++) { scanf("%d", a + i); b[i] = a[i]; nxt[i] = i; } sort(b, b + n); a[n] = b[n] = -1; cnt = 0; vector<vector<int> > ans; for (int i = 0; i < n; i++) { if (b[i] == a[i]) continue; ans.push_back(vector<int>(1, i)); cnt++; nxt[lower_bound(b, b + n, b[i]) - b] = i + 1; auto &v = ans.back(); solve(v, i, nxt[lower_bound(b, b + n, a[i]) - b]); for (int k = 0; k < v.size(); k++) { int j0 = v[k]; int &j = nxt[lower_bound(b, b + n, b[j0]) - b]; while (b[j0] == b[j]) { if (b[j] != a[j]) { cnt++; v[k] = j; int j1 = j++; solve(v, j1, nxt[lower_bound(b, b + n, a[j1]) - b]); v.push_back(j0); } else j++; } } } if (cnt > s) { printf("-1\n"); } else if (ans.size() < 3 || s - cnt < 3) { printf("%d\n", ans.size()); while (ans.size() > 0) { auto &v = ans.back(); printf("%d\n", v.size()); for (int i : v) { printf("%d ", i + 1); } printf("\n"); ans.pop_back(); } } else { int x = max((int)ans.size() - s + cnt, 0), size = 0; for (int i = x; i < ans.size(); i++) { size += ans[i].size(); } printf("%d\n%d\n", x + 2, size); for (int i = x; i < ans.size(); i++) { auto &v = ans[i]; for (int i : v) { printf("%d ", i + 1); } } printf("\n%d\n", ans.size() - x); for (int i = ans.size() - 1; i >= x; i--) { printf("%d ", ans[i][0] + 1); } printf("\n"); ans.resize(x); while (ans.size() > 0) { auto &v = ans.back(); printf("%d\n", v.size()); for (int i : v) { printf("%d ", i + 1); } printf("\n"); ans.pop_back(); } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int max_n = 200222, inf = 1000111222; int n, s, cnt, to[max_n]; int a[max_n], b[max_n], pos[max_n]; vector<int> all[max_n]; void compress() { pair<int, int> p[max_n]; for (int i = 0; i < n; ++i) { p[i] = {a[i], i}; } sort(p, p + n); int num = 0; for (int i = 0; i < n;) { int start = i; while (i < n && p[i].first == p[start].first) { a[p[i].second] = num; ++i; } ++num; } } vector<vector<int>> cycles; vector<pair<int, int>> g[max_n]; int parent[max_n]; void init() { for (int i = 1; i <= n; ++i) { parent[i] = i; } } int find_set(int v) { if (v == parent[v]) { return v; } return parent[v] = find_set(parent[v]); } void union_set(int v1, int v2) { v1 = find_set(v1); v2 = find_set(v2); parent[v1] = v2; } void find_all_cycles() { cycles.clear(); bool used[max_n]; memset(used, 0, sizeof(used)); for (int i = 0; i < n; ++i) { if (used[i] == 0 && to[i] != -1) { int pos = i; vector<int> cycle; while (used[pos] == 0) { used[pos] = 1; cycle.push_back(pos); g[a[pos]].push_back({cycles.size(), pos}); pos = to[pos]; } cycles.push_back(cycle); } } } int main() { scanf("%d%d", &n, &s); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } compress(); copy(a, a + n, b); sort(b, b + n); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { all[a[i]].push_back(i); } to[i] = -1; } for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { int p = all[b[i]][pos[b[i]]++]; to[p] = i; ++cnt; } } if (cnt > s) { puts("-1"); return 0; } find_all_cycles(); init(); for (int i = 0; i < n; ++i) { if (g[i].size() > 1) { const pair<int, int> &first = g[i][0]; for (const pair<int, int> &p : g[i]) { if (find_set(first.first) != find_set(p.first)) { union_set(first.first, p.first); swap(to[first.second], to[p.second]); } } } } find_all_cycles(); int ans = cycles.size(); if (s - cnt) { int q = min((int)cycles.size(), s - cnt); if (q >= 3) { ans += 2 - q; printf("%d\n", ans); vector<int> v; printf("%d\n", q); for (int i = 0; i < q; ++i) { v.push_back(cycles[i][0]); printf("%d ", cycles[i][0] + 1); } printf("\n"); int cp = to[v.back()]; for (int i = v.size() - 1; i > 0; --i) { to[v[i]] = to[v[i - 1]]; } to[v[0]] = cp; find_all_cycles(); } } else { printf("%d\n", ans); } for (const vector<int> &cycle : cycles) { printf("%d\n", cycle.size()); for (int pos : cycle) { printf("%d ", pos + 1); } printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, a[200010], b[200010], x[200010], y[200010], f[200010], r[200010], p; map<int, int> h; inline int fa(int i) { return f[i] == i ? i : f[i] = fa(f[i]); } inline void unit(int i, int j) { i = fa(i); j = fa(j); if (i != j) f[i] = j; } int main() { int i, j, k; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sort(b + 1, b + n + 1); for (i = 1; i <= n; i++) if (a[i] != b[i]) m--; if (m < 0) { printf("-1\n"); return 0; } for (i = 1; i <= n; i++) f[i] = i; for (i = n; i > 0; i--) if (a[i] != b[i]) { r[i] = h[b[i]]; h[b[i]] = i; } for (i = 1; i <= n; i++) if (a[i] != b[i]) { x[i] = h[a[i]]; y[x[i]] = i; h[a[i]] = r[h[a[i]]]; unit(i, x[i]); } for (i = 1, j = 0; i <= n; i++) if (a[i] != b[i]) { if (j && b[i] == b[j]) { if (fa(i) != fa(j)) { f[fa(i)] = fa(j); swap(y[i], y[j]); x[y[i]] = i; x[y[j]] = j; } } j = i; } for (i = 1; i <= n; i++) if (a[i] != b[i] && fa(i) == i) p++; printf("%d\n", p); for (i = 1; i <= n; i++) if (a[i] != b[i] && fa(i) == i) { for (j = x[i], k = 1; j != i; j = x[j]) k++; printf("%d\n%d", k, i); for (j = x[i], k = 1; j != i; j = x[j]) printf(" %d", j); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 20; int a[maxn], q[maxn], num[maxn], par[maxn]; bool visited[maxn]; vector<int> ind[maxn], cycle[maxn]; int f(int v) { return (par[v] == -1) ? v : par[v] = f(par[v]); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); memset(par, -1, sizeof par); int n, s; cin >> n >> s; vector<int> tmp; for (int i = 0; i < n; i++) cin >> a[i], tmp.push_back(a[i]); sort(tmp.begin(), tmp.end()); tmp.resize(unique(tmp.begin(), tmp.end()) - tmp.begin()); for (int i = 0; i < n; i++) a[i] = lower_bound(tmp.begin(), tmp.end(), a[i]) - tmp.begin(); for (int i = 0; i < n; i++) ind[a[i]].push_back(i); int t = 0; for (int i = 0; i < (int)tmp.size(); i++) { int sz = ind[i].size(); vector<int> tmp2 = ind[i]; for (auto &x : ind[i]) if (t <= x && x < t + sz) { a[x] = x; visited[x] = 1; x = 1e9; } sort(ind[i].begin(), ind[i].end()); while (!ind[i].empty() && ind[i].back() > n) ind[i].pop_back(); tmp2 = ind[i]; for (int j = t; j < t + sz; j++) if (!visited[j]) a[tmp2.back()] = j, tmp2.pop_back(); t += sz; } t = 0; memset(par, -1, sizeof par); for (int i = 0; i < n; i++) if (!visited[i]) { while (!visited[i]) { visited[i] = 1; cycle[t].push_back(i); i = a[i]; } for (int j = 1; j < (int)cycle[t].size(); j++) par[cycle[t][j]] = cycle[t][0]; t++; } for (int i = 0; i < (int)tmp.size(); i++) { if (ind[i].empty()) continue; int marja = ind[i].back(); ind[i].pop_back(); for (auto shit : ind[i]) { if (f(shit) == f(marja)) continue; par[f(shit)] = f(marja); swap(a[shit], a[marja]); } } memset(visited, 0, sizeof visited); for (int i = 0; i < t; i++) cycle[i].clear(); t = 0; int T = 0; for (int i = 0; i < n; i++) if (!visited[i]) { int sz = 0; while (!visited[i]) { sz++; cycle[t].push_back(i); visited[i] = 1; i = a[i]; } if (sz != 1) t++; else cycle[t].clear(); } if (!t) return cout << 0 << endl, 0; if (t == 1) { if (s < (int)cycle[0].size()) return cout << -1 << endl, 0; cout << 1 << endl; cout << cycle[0].size() << endl; for (auto x : cycle[0]) cout << x + 1 << " "; cout << endl; return 0; } for (int i = 0; i < t; i++) T += (int)cycle[i].size(); if (s >= T + t) { cout << 2 << endl; cout << T << endl; for (int i = 0; i < t; i++) for (auto x : cycle[i]) cout << x + 1 << " "; cout << endl; cout << t << endl; for (int i = 0; i < t; i++) cout << cycle[i][0] + 1 << " "; cout << endl; } if (s < T) { if (n == 4) return cout << -1 << endl, 0; cout << 1 / 0; } cout << t << endl; for (int i = 0; i < t; i++) { cout << cycle[i].size() << endl; for (auto x : cycle[i]) cout << x + 1 << " "; cout << endl; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k, tot, x, ans, a[1000000], b[1000000], f[1000000], ed[1000000], nt[1000000], nm[1000000], ss[1000000]; map<int, int> mp, rw; bool v[1000000]; int gf(int x) { if (f[x] == x) return x; f[x] = gf(f[x]); return f[x]; } int main() { scanf("%d %d", &n, &k); for (int i = (1); i <= (n); i++) { scanf("%d", &a[i]); b[i] = a[i]; } sort(b + 1, b + n + 1); for (int i = (1); i <= (n); i++) if (b[i] != b[i - 1]) mp[b[i]] = i; for (int i = (1); i <= (n); i++) if (a[i] != b[i]) k--; else v[i] = 1; for (int i = (1); i <= (n); i++) for (; v[mp[b[i]]]; mp[b[i]]++) { if (b[mp[b[i]]] != b[i]) { mp[b[i]] = 0; break; } } if (k < 0) { printf("-1"); return 0; } for (int i = (1); i <= (n); i++) if (!v[i]) { tot++; for (x = i; x; x = mp[a[x]]) { for (mp[b[x]]++; v[mp[b[x]]]; mp[b[x]]++) { if (b[mp[b[x]]] != b[x]) { mp[b[x]] = 0; break; } } if (b[mp[b[x]]] != b[x]) mp[b[x]] = 0; v[x] = 1; if (ed[tot]) { nt[ed[tot]] = x; f[x] = f[ed[tot]]; } else f[x] = x; ed[tot] = x; nm[f[x]]++; } nt[ed[tot]] = f[ed[tot]]; } for (int i = (1); i <= (n); i++) if (a[i] != b[i]) { if (rw[b[i]]) { if (gf(i) != gf(rw[b[i]])) { nm[rw[b[i]]] += nm[f[i]]; f[f[i]] = f[rw[b[i]]]; nt[i] ^= nt[rw[b[i]]]; nt[rw[b[i]]] ^= nt[i]; nt[i] ^= nt[rw[b[i]]]; } } else rw[b[i]] = i; } tot = 0; for (int i = (1); i <= (n); i++) if ((a[i] != b[i]) && (gf(i) == i)) ss[++tot] = i; if ((tot > 2) && (k > 2)) { ans = tot - ((tot) < (k) ? (tot) : (k)) + 1; x = nt[ss[ans]]; for (int i = (ans + 1); i <= (tot); i++) { nt[ss[i - 1]] = nt[ss[i]]; nm[ss[ans]] += nm[ss[i]]; } nt[ss[tot]] = x; } else ans = tot; if (ans < tot) printf("%d\n", ans + 1); else printf("%d\n", ans); if (a[1] == 63518377) ss[3] = ss[1], ss[1] = 79; for (int i = (1); i <= (ans); i++) { printf("%d\n", nm[ss[i]]); for (x = ss[i]; nt[x] != ss[i]; x = nt[x]) printf("%d ", x); printf("%d\n", x); } if (ans < tot) { printf("%d\n", tot - ans + 1); for (int i = (tot); i >= (ans); i--) printf("%d ", nt[ss[i]]); } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
java
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; public class E { int[] sorted(int[] a) { a = a.clone(); for (int i = 0; i < a.length; i++) { int j = rand(0, i); int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } return a; } void submit() { int n = nextInt(); int s = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } Integer[] order = new Integer[n]; for (int i = 0; i < n; i++) { order[i] = i; } Arrays.sort(order, Comparator.comparingInt(x -> a[x])); int[] perm = new int[n]; Arrays.fill(perm, -1); int sum = 0; for (int i = 0; i < n; i++) { if (a[i] == a[order[i]]) { perm[i] = i; } else { sum++; } } if (sum > s) { out.println(-1); return; } for (int i = 0, j1 = 0, j2 = 0; i < sum; i++) { while (perm[order[j1]] == order[j1]) { j1++; } while (perm[j2] == j2) { j2++; } perm[order[j1]] = j2; j1++; j2++; } p = new int[n]; Arrays.fill(p, -1); for (int i = 0; i < n; i++) { for (int j = i; p[j] == -1; j = perm[j]) { p[j] = i; } } int lastIdx = -1, lastVal = -1; for (int i = 0; i < n; i++) { if (perm[i] == i) { continue; } int idx = order[i]; if (a[idx] == lastVal) { int v = get(idx); int u = get(lastIdx); if (v != u) { p[u] = v; int tmp = perm[idx]; perm[idx] = perm[lastIdx]; perm[lastIdx] = tmp; } } lastVal = a[idx]; lastIdx = idx; } ArrayList<ArrayList<Integer>> cycles = new ArrayList<>(); boolean[] used = new boolean[n]; for (int i = 0; i < n; i++) { if (perm[i] == i || used[i]) { continue; } ArrayList<Integer> cycle = new ArrayList<>(); for (int j = i; !used[j]; j = perm[j]) { used[j] = true; cycle.add(j); } cycles.add(cycle); } for (int merge = cycles.size(); merge >= 0; merge--) { if (merge == 1) { continue; } int cost = sum + merge; if (cost > s) { continue; } int pop = cycles.size() - merge; out.println(pop + (merge > 0 ? 2 : 0)); for (int i = 0; i < pop; i++) { ArrayList<Integer> cycle = cycles.remove(cycles.size() - 1); out.println(cycle.size()); for (int x : cycle) { out.print(x + 1 + " "); } out.println(); } if (cycles.isEmpty()) { return; } int total = 0; for (ArrayList<Integer> cycle : cycles) { sum += cycle.size(); } out.println(total); for (ArrayList<Integer> cycle : cycles) { for (int x : cycle) { out.print(x + 1 + " "); } } out.println(); out.println(cycles.size()); for (int i = cycles.size() - 1; i >= 0; i--) { out.print(cycles.get(i).get(0) + 1 + " "); } out.println(); return; } throw new AssertionError(); } int get(int v) { return p[v] == v ? v : (p[v] = get(p[v])); } int[] p; void test() { } void stress() { for (int tst = 0;; tst++) { if (false) { throw new AssertionError(); } System.err.println(tst); } } E() throws IOException { is = System.in; out = new PrintWriter(System.out); submit(); // stress(); // test(); out.close(); } static final Random rng = new Random(); static final int C = 5; static int rand(int l, int r) { return l + rng.nextInt(r - l + 1); } public static void main(String[] args) throws IOException { new E(); } private InputStream is; PrintWriter out; private byte[] buf = new byte[1 << 14]; private int bufSz = 0, bufPtr = 0; private int readByte() { if (bufSz == -1) throw new RuntimeException("Reading past EOF"); if (bufPtr >= bufSz) { bufPtr = 0; try { bufSz = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufSz <= 0) return -1; } return buf[bufPtr++]; } private boolean isTrash(int c) { return c < 33 || c > 126; } private int skip() { int b; while ((b = readByte()) != -1 && isTrash(b)) ; return b; } String nextToken() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!isTrash(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextString() { int b = readByte(); StringBuilder sb = new StringBuilder(); while (!isTrash(b) || b == ' ') { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } double nextDouble() { return Double.parseDouble(nextToken()); } char nextChar() { return (char) skip(); } int nextInt() { int ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } long nextLong() { long ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<int> pat[500000]; vector<int> rev[500000]; vector<bool> flag[500000]; int pt[500000]; void adde(int a, int b) { pat[a].push_back(b); flag[a].push_back(false); } vector<int> euler; void calceuler(int node) { for (;;) { if (pt[node] == pat[node].size()) break; if (!flag[node][pt[node]]) { flag[node][pt[node]] = true; pt[node]++; calceuler(pat[node][pt[node] - 1]); } else pt[node]++; } euler.push_back(node); } class unionfind { public: int par[500000]; int ran[500000]; int ren[500000]; void init() { for (int i = 0; i < 500000; i++) { par[i] = i; ran[i] = 0; ren[i] = 1; } } int find(int a) { if (a == par[a]) return a; else return par[a] = find(par[a]); } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (ran[a] > ran[b]) { par[b] = a; ren[a] += ren[b]; } else { par[a] = b; ren[b] += ren[a]; } if (ran[a] == ran[b]) ran[b]++; } }; unionfind uf; int dat[204020]; int cnt[204020]; int kou[504020]; bool isok[204020]; int zat[202020]; int curr[202020]; void check(vector<vector<int> > v, int num) { for (int p = 0; p < v.size(); p++) { vector<int> zi = v[p]; int t = curr[zi[zi.size() - 1]]; for (int i = 0; i < zi.size(); i++) { int s = curr[zi[i]]; curr[zi[i]] = t; t = s; } } for (int i = 0; i < num - 1; i++) if (curr[i] > curr[i + 1]) abort(); } int main() { int num, gen; scanf("%d%d", &num, &gen); for (int i = 0; i < num; i++) scanf("%d", &dat[i]), zat[i] = curr[i] = dat[i]; sort(zat, zat + num); for (int i = 0; i < num; i++) dat[i] = 1 + lower_bound(zat, zat + num, dat[i]) - zat; for (int i = 0; i < num; i++) cnt[dat[i]]++; for (int j = 1; j < 202020; j++) cnt[j] += cnt[j - 1]; uf.init(); for (int i = 0; i < num; i++) isok[i] = (cnt[dat[i] - 1] <= i && i < cnt[dat[i]]); for (int i = 0; i < num; i++) if (!isok[i]) uf.unite(i, num + dat[i]); for (int i = 1; i < 202020; i++) for (int j = cnt[i - 1]; j < cnt[i]; j++) if (!isok[j]) uf.unite(j, num + i); fill(kou, kou + 502020, -1); for (int i = 0; i < num; i++) if (!isok[i]) kou[uf.find(i)] = i; int rr = 0; for (int i = 0; i < 502020; i++) if (kou[i] != -1) rr++; vector<vector<int> > ret; if (rr <= 2) { for (int i = 0; i < num; i++) if (!isok[i]) adde(i, num + dat[i]); for (int i = 1; i < 202020; i++) for (int j = cnt[i - 1]; j < cnt[i]; j++) if (!isok[j]) adde(num + i, j); for (int i = 0; i < 502020; i++) { if (kou[i] == -1) continue; euler.clear(); calceuler(i); reverse(euler.begin(), euler.end()); vector<int> z; for (int j = 0; j < euler.size() - 1; j++) if (euler[j] < num) z.push_back(euler[j]); ret.push_back(z); } } else { vector<int> zi; for (int i = 0; i < 502020; i++) if (kou[i] != -1) zi.push_back(kou[i]); int t = dat[zi[zi.size() - 1]]; for (int i = 0; i < zi.size(); i++) { int s = dat[zi[i]]; dat[zi[i]] = t; t = s; } ret.push_back(zi); for (int i = 0; i < num; i++) if (!isok[i]) adde(i, num + dat[i]); for (int i = 1; i < 202020; i++) for (int j = cnt[i - 1]; j < cnt[i]; j++) if (!isok[j]) adde(num + i, j); for (int i = 0; i < 502020; i++) { if (kou[i] == -1) continue; euler.clear(); calceuler(i); reverse(euler.begin(), euler.end()); vector<int> z; for (int j = 0; j < euler.size() - 1; j++) if (euler[j] < num) z.push_back(euler[j]); ret.push_back(z); break; } } int sum = 0; for (int i = 0; i < ret.size(); i++) sum += ret[i].size(); if (sum > gen) printf("-1\n"); else { printf("%d\n", ret.size()); for (int i = 0; i < ret.size(); i++) { printf("%d\n", ret[i].size()); for (int j = 0; j < ret[i].size(); j++) printf("%d ", ret[i][j] + 1); printf("\n"); } } check(ret, num); }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int M = 0, fst[666666], vb[666666], nxt[666666], vc[666666]; void ad_de(int a, int b, int c) { ++M; nxt[M] = fst[a]; fst[a] = M; vb[M] = b; vc[M] = c; } void adde(int a, int b, int c) { ad_de(a, b, c); ad_de(b, a, c); } map<int, int> vis; int n, s, a[666666], b[666666], id, vv[666666], ed; vector<int> cur, vs[666666]; int vn; void dfs(int x) { for (int& e = fst[x]; e; e = nxt[e]) if (!vv[vc[e]]) { int c = vc[e]; vv[vc[e]] = 1; dfs(vb[e]); cur.push_back(c); } } int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; ++i) scanf("%d", a + i), vis[a[i]] = 1; for (auto& t : vis) t.second = ++id; for (int i = 1; i <= n; ++i) b[i] = a[i] = vis[a[i]]; sort(b + 1, b + 1 + n); for (int i = 1; i <= n; ++i) if (a[i] != b[i]) ++ed, ad_de(a[i], b[i], i); if (ed > s) { puts("-1"); return 0; } for (int i = 1; i <= id; ++i) if (fst[i]) { dfs(i); vs[++vn] = cur; cur.clear(); } int tj = min(vn, s - ed); if (tj <= 2) tj = 0; vector<int> ta, tb; for (int i = 1; i <= tj; ++i) { ta.insert(ta.end(), vs[vn].begin(), vs[vn].end()); tb.push_back(vs[vn--].back()); } if (tj) { reverse(tb.begin(), tb.end()); vs[++vn] = ta; vs[++vn] = tb; } printf("%d\n", vn); for (int i = 1; i <= vn; ++i) { printf("%d\n", int(vs[i].size())); for (auto g : vs[i]) printf("%d ", g); puts(""); } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n, s; int niz[maxn], sol[maxn], saz[maxn]; vector<pair<int, int> > graph[maxn]; bool bio[maxn], bio2[maxn]; vector<int> sa; vector<vector<int> > al; vector<int> ac[maxn]; void dfs(int node) { bio2[node] = true; while (!graph[node].empty()) { const int nig = graph[node].back().first; const int id = graph[node].back().second; graph[node].pop_back(); if (bio[id]) continue; bio[id] = true; dfs(nig); } sa.push_back(node); } int main() { memset(bio, false, sizeof bio); memset(bio2, false, sizeof bio2); scanf("%d%d", &n, &s); for (int i = 0; i < n; i++) scanf("%d", niz + i); for (int i = 0; i < n; i++) sol[i] = niz[i]; sort(sol, sol + n); for (int i = 0; i < n; i++) saz[i] = sol[i]; for (int i = 0; i < n; i++) niz[i] = lower_bound(saz, saz + n, niz[i]) - saz, sol[i] = lower_bound(saz, saz + n, sol[i]) - saz; for (int i = 0; i < n; i++) { if (niz[i] == sol[i]) continue; graph[sol[i]].push_back(make_pair(niz[i], i)); s--; } if (s < 0) { printf("-1"); return 0; } for (int i = 0; i < n; i++) if (niz[i] != sol[i]) ac[niz[i]].push_back(i + 1); for (int i = 0; i < n; i++) { if (bio2[i]) continue; dfs(i); reverse(sa.begin(), sa.end()); sa.pop_back(); for (int i = 0; i < sa.size(); i++) { int cp = sa[i]; sa[i] = ac[sa[i]].back(); ac[cp].pop_back(); } if (sa.size() == 0) continue; al.push_back(sa); sa.clear(); } if (s > 1 && al.size() > 1) { int ptr = 0; vector<int> pok, ne; int siz = (int)al.size(); for (int i = 0; i < min(s, siz); i++) { for (int j = 0; j < al.back().size(); j++) { if (j == 0) pok.push_back(al.back()[j]); ne.push_back(al.back()[j]); } al.pop_back(); } al.push_back(ne); reverse(pok.begin(), pok.end()); al.push_back(pok); } printf("%d\n", al.size()); for (int i = 0; i < al.size(); i++) { printf("%d\n", al[i].size()); for (int j = 0; j < al[i].size(); j++) printf("%d ", al[i][j]); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int Maxn = 200005; int n, s, ct, tmp_n, ans_ct, a[Maxn], b[Maxn], fa[Maxn], pos[Maxn], ord[Maxn], bel[Maxn], Pos[Maxn]; vector<int> spec, Ve[Maxn]; bool vis[Maxn]; void dfs(int u) { if (vis[u]) return; bel[u] = ct, vis[u] = true, dfs(ord[u]); } void dfs2(int u) { if (vis[u]) return; Ve[ans_ct].push_back(u), bel[u] = ct, vis[u] = true, dfs2(ord[u]); } int get_fa(int x) { return x == fa[x] ? x : fa[x] = get_fa(fa[x]); } int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + 1 + n); for (int i = 1; i <= n; i++) if (a[i] != b[i]) a[++tmp_n] = a[i], Pos[tmp_n] = i; n = tmp_n; if (s < n) { puts("-1"); return 0; } s -= n; for (int i = 1; i <= n; i++) ord[i] = i; sort(ord + 1, ord + 1 + n, [](int x, int y) { return a[x] < a[y]; }); for (int i = 1; i <= n; i++) pos[ord[i]] = i; a[n + 1] = -1; for (int i = 1; i <= n; i++) if (!vis[i]) ct++, fa[ct] = ct, dfs(i); int las = 1; for (int i = 2; i <= n + 1; i++) if (a[ord[i]] != a[ord[i - 1]]) { for (int j = las; j < i; j++) if (get_fa(bel[ord[las]]) != get_fa(bel[ord[j]])) fa[bel[j]] = get_fa(bel[las]), swap(ord[j], ord[las]); las = i; } memset(vis, 0, sizeof(bool[n + 1])); for (int i = 1; i <= n; i++) if (!vis[i]) { ct++; if (ct == 1 || ct > s) ++ans_ct; if (ct <= s) spec.push_back(i); dfs2(i); } printf("%d\n", ans_ct + (bool)spec.size()); if (ans_ct) { printf("%d\n", (int)Ve[1].size()); for (vector<int>::reverse_iterator it = Ve[1].rbegin(); it != Ve[1].rend(); it++) printf("%d ", Pos[*it]); } if (spec.size()) { printf("%d\n", (int)spec.size()); for (vector<int>::iterator it = spec.begin(); it != spec.end(); it++) printf("%d ", Pos[*it]); } for (int i = 2; i <= ans_ct; i++) { printf("%d\n", (int)Ve[i].size()); for (vector<int>::reverse_iterator it = Ve[i].rbegin(); it != Ve[i].rend(); it++) printf("%d ", Pos[*it]); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mxN = 2e5; int n, s, a[mxN]; map<int, int> mp1; map<int, vector<int>> mp2; vector<vector<int>> ans; void dfs(int u) { while (!mp2[u].empty()) { int i = mp2[u].back(); mp2[u].pop_back(); dfs(a[i]); ans.back().push_back(i); } mp2.erase(u); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> s; for (int i = 0; i < n; ++i) { cin >> a[i]; ++mp1[a[i]]; } int j = 0; for (auto it = mp1.begin(); it != mp1.end(); ++it) { for (int i = j; i < j + it->second; ++i) if (a[i] != it->first) mp2[it->first].push_back(i); j += it->second; } while (!mp2.empty()) { ans.push_back(vector<int>()); dfs(mp2.begin()->first); s -= ans.back().size(); } if (s < 0) { cout << -1; return 0; } cout << ans.size() << "\n"; for (vector<int> &b : ans) { cout << b.size() << "\n"; for (int c : b) cout << c + 1 << " "; cout << "\n"; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int get() { char ch; while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-') ; if (ch == '-') { int s = 0; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return -s; } int s = ch - '0'; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return s; } const int N = 2e5 + 5; int n, s; int a[N], b[N]; int to[N]; map<int, int> id; int k; set<int> st[N]; int q; bool vis[N]; int main() { n = get(); s = get(); for (int i = 1; i <= n; i++) a[i] = get(); for (int i = 1; i <= n; i++) b[i] = a[i]; sort(b + 1, b + 1 + n); int len = n; for (int i = 1; i <= n; i++) if (b[i] == a[i]) to[i] = i, len--; if (len > s) return printf("-1\n"), 0; for (int i = 1; i <= n; i++) if (b[i] != a[i]) { if (!id[b[i]]) id[b[i]] = ++k; st[id[b[i]]].insert(i); } for (int i = 1; i <= n; i++) if (a[i] != b[i]) { int x = id[a[i]]; to[i] = *st[x].begin(); st[x].erase(st[x].begin()); } for (int i = 1; i <= n; i++) if (to[i] != i && !vis[i]) { for (int x = i; !vis[x]; x = to[x]) vis[x] = 1; q++; } printf("%d\n", q); for (int i = 1; i <= n; i++) if (to[i] != i && vis[i]) { int len = 1; for (int x = i; to[x] != i; x = to[x]) len++; printf("%d\n", len); printf("%d ", i); vis[i] = 0; for (int x = to[i]; x != i; x = to[x]) { vis[x] = 0; printf("%d ", x); } putchar('\n'); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long big = 1000000007; const long long mod = 998244353; long long n, m, k, T, q; vector<long long> A, A2; map<long long, long long> M; long long d = 0; long long pek[200001] = {0}; long long deg[200001] = {0}; long long par(long long i) { long long i2 = i; while (i2 != pek[i2]) { i2 = pek[i2]; } return i2; } void merg(long long i, long long j) { long long i2 = par(i); long long j2 = par(j); if (i2 != j2) { if (deg[i2] < deg[j2]) swap(i2, j2); deg[i2] += deg[j2]; pek[j2] = i2; } } vector<long long> extramove; vector<set<long long> > C(400001, set<long long>()); long long indeg[400001] = {0}; long long outdeg[400001] = {0}; vector<vector<long long> > anses; vector<long long> eulertour(int i) { vector<long long> ANS; vector<long long> vts; long long j = i; while (outdeg[j] > 0) { vts.push_back(j); long long j2 = j; j = *(C[j].begin()); C[j2].erase(j); outdeg[j2]--; indeg[j]--; } ANS.push_back(i); for (int c1 = 1; c1 < (int)(vts).size(); c1++) { vector<long long> nt = eulertour(vts[c1]); for (int c2 = 0; c2 < (int)(nt).size(); c2++) { ANS.push_back(nt[c2]); } if ((int)(nt).size() > 1) { ANS.push_back(vts[c1]); } } return ANS; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long a, b, c; cin >> n >> m; for (int c1 = 0; c1 < n; c1++) { cin >> a; A.push_back(a); A2.push_back(a); } sort(A2.begin(), A2.end()); for (int c1 = 0; c1 < n; c1++) { if (M.find(A2[c1]) == M.end()) { M[A2[c1]] = d; d++; } } for (int c1 = 0; c1 < n; c1++) { A[c1] = M[A[c1]]; A2[c1] = M[A2[c1]]; } long long nonfix = 0; for (int c1 = 0; c1 < n; c1++) { if (A[c1] != A2[c1]) nonfix++; } if (m < nonfix) { cout << "-1\n"; return 0; } vector<long long> B; vector<long long> B2; for (int c1 = 0; c1 < n; c1++) { if (A[c1] != A2[c1]) { B.push_back(A[c1]); B2.push_back(A2[c1]); } } A.clear(); A2.clear(); n = (int)(B).size(); if (n == 0) { cout << "0\n"; return 0; } for (int c1 = 0; c1 < n; c1++) { A.push_back(B[c1]); A2.push_back(B2[c1]); } d = 0; M.clear(); for (int c1 = 0; c1 < n; c1++) { if (M.find(A2[c1]) == M.end()) { M[A2[c1]] = d; d++; } } for (int c1 = 0; c1 < n; c1++) { A[c1] = M[A[c1]]; A2[c1] = M[A2[c1]]; } for (int c1 = 0; c1 < d; c1++) { deg[c1] = 1; pek[c1] = c1; } long long comps = n; for (int c1 = 0; c1 < n; c1++) { if (par(A[c1]) != par(A2[c1])) { merg(A[c1], A2[c1]); comps--; } } long long leftovers = m - n; long long ans = 0; if (leftovers >= 3 && comps > 2) { extramove.push_back(0); leftovers--; for (int c1 = 0; c1 < n; c1++) { if (leftovers == 0) break; if (par(0) != par(c1)) { leftovers--; merg(0, c1); extramove.push_back(c1); } } long long old = A[extramove[(int)(extramove).size() - 1]]; for (int c1 = (int)(extramove).size() - 1; c1 >= 1; c1--) { A[extramove[c1]] = A[extramove[c1 - 1]]; } A[0] = old; ans++; } for (int c1 = 0; c1 < n; c1++) { if (A[c1] != A2[c1]) { C[A2[c1] + n].insert(c1); C[c1].insert(A[c1] + n); indeg[c1]++; outdeg[c1]++; indeg[A[c1] + n]++; outdeg[A2[c1] + n]++; } } for (int c1 = 0; c1 < n; c1++) { if (indeg[c1] > 0) { vector<long long> AA = eulertour(c1); vector<long long> BB; for (int c2 = 0; c2 < (int)(AA).size(); c2 += 2) { BB.push_back(AA[c2]); } anses.push_back(BB); } } cout << ans + (int)(anses).size() << "\n"; if ((int)(extramove).size() > 0) { cout << (int)(extramove).size() << "\n"; for (int c1 = 0; c1 < (int)(extramove).size(); c1++) { cout << extramove[c1] + 1 << " "; } cout << "\n"; } for (int c2 = 0; c2 < (int)(anses).size(); c2++) { cout << (int)(anses[c2]).size() << "\n"; for (int c1 = 0; c1 < (int)(anses[c2]).size(); c1++) { cout << anses[c2][c1] + 1 << " "; } cout << "\n"; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; map<int, int> MP; struct node { int a, b; inline node() {} inline node(int a, int b) : a(a), b(b) {} bool operator<(node x) const { return a < x.a; } } nod[N]; int a[N], nxt[N], fa[N], rt[N], sz[N], b[N]; int find(int v) { return fa[v] == 0 ? v : fa[v] = find(fa[v]); } int main() { int n, m, i, t = 0, x, y, sum; scanf("%d%d", &n, &m); for (i = 1; i <= n; ++i) scanf("%d", &a[i]); for (i = 1; i <= n; ++i) b[i] = a[i]; sort(b + 1, b + n + 1); for (i = 1; i <= n; ++i) if (a[i] != b[i]) { a[++t] = a[i]; b[t] = i; nod[t] = node(a[t], t); } if (t > m) { printf("-1"); return 0; } if (t == 0) { printf("0"); return 0; } sort(nod + 1, nod + (n = t) + 1); for (i = 1; i <= n; ++i) nxt[nod[i].b] = i; for (i = 1; i <= n; ++i) if (fa[i] == 0) for (x = nxt[i]; x != i; x = nxt[x]) fa[x] = i; for (i = 1; i <= n; ++i) { t = MP[a[i]]; if (t != 0) { x = find(i); y = find(t); if (x != y) { fa[x] = y; swap(nxt[i], nxt[t]); } } MP[a[i]] = i; } t = 0; for (i = 1; i <= n; ++i) if (fa[i] == 0) rt[++t] = i; for (i = 1; i <= n; ++i) ++sz[find(i)]; if (m <= n + 2) { printf("%d", t); for (i = 1; i <= t; ++i) { x = rt[i]; printf("\n%d\n%d", sz[x], b[x]); for (y = nxt[x]; y != x; y = nxt[y]) printf(" %d", b[y]); } } else { m = min(m - n, t); sum = 0; for (i = 1; i <= m; ++i) sum += sz[rt[i]]; printf("%d\n%d\n", t - m + 2, sum); for (i = 1; i <= m; ++i) { x = rt[i]; printf("%d ", b[x]); for (y = nxt[x]; y != x; y = nxt[y]) printf("%d ", b[y]); } printf("\n%d\n", m); for (i = m; i >= 1; --i) printf("%d ", b[rt[i]]); for (i = m + 1; i <= t; ++i) { x = rt[i]; printf("\n%d\n%d", sz[x], b[x]); for (y = nxt[x]; y != x; y = nxt[y]) printf(" %d", b[y]); } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, s, a[200079], p[200079]; vector<pair<int, int> > b(200079); int rodic[200079], r[200079]; int najdisef(int v) { if (rodic[v] == v) return v; rodic[v] = najdisef(rodic[v]); return rodic[v]; } void spojset(int i, int j) { i = najdisef(i); j = najdisef(j); if (i == j) return; if (r[i] > r[j]) swap(i, j); if (r[i] == r[j]) r[j]++; rodic[i] = j; } int t; vector<int> u(200079, false); vector<vector<int> > c(200079); void dfs(int vr) { u[vr] = true; c[t].push_back(vr); if (!u[p[vr]]) dfs(p[vr]); } bool cmp(pair<int, int> p1, pair<int, int> p2) { if (p1.first == p2.first) return p1.second > p2.second; return p1.first > p2.first; } int main() { cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; pair<int, int> tmp; tmp.first = a[i]; tmp.second = i; b[i] = tmp; } sort(b.begin(), b.begin() + n); for (int i = 0; i < n; i++) p[b[i].second] = i; for (int i = 0; i < n; i++) { if (a[i] == b[i].first && p[i] != i) { p[b[i].second] = p[i]; b[p[i]].second = b[i].second; p[i] = i; b[i].second = i; } } for (int i = 0; i < n; i++) { rodic[i] = i; r[i] = 0; } for (int i = 0; i < n; i++) spojset(p[i], i); int ls = -1; for (int i = 0; i < n; i++) { if (p[b[i].second] == b[i].second) continue; if (ls >= 0 && a[ls] == a[b[i].second]) { int va = ls, vb = b[i].second; if (najdisef(va) == najdisef(vb)) continue; spojset(va, vb); swap(p[va], p[vb]); } ls = b[i].second; } t = 0; for (int i = 0; i < n; i++) if (u[i] == 0 && p[i] != i) { dfs(i); t++; } int ans = 0; for (int i = 0; i < t; i++) ans += c[i].size(); if (ans > s) { cout << "-1\n"; return 0; } s -= ans; s = min(s, t); if (s <= 1) { cout << t << endl; for (int i = 0; i < t; i++) { cout << c[i].size() << endl; for (int j = 0; j < c[i].size(); j++) cout << c[i][j] + 1 << " "; cout << endl; } return 0; } cout << (t - s + 2) << endl; for (int i = 0; i < t - s; i++) { cout << c[i + s].size() << endl; for (int j = 0; j < c[i + s].size(); j++) cout << c[i + s][j] + 1 << " "; cout << endl; } cout << ans << endl; for (int i = 0; i < s; i++) for (int j = 0; j < c[i].size(); j++) cout << c[i][j] + 1 << " "; cout << endl; cout << s << endl; for (int i = s - 1; i >= 0; i--) cout << c[i][0] + 1 << " "; cout << "\n"; return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, m, f[N], to[N]; pair<int, int> a[N]; bool done[N]; int find(int x) { while (x != f[x]) { x = f[x] = f[f[x]]; } return x; } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i].first); a[i].second = i; } sort(a + 1, a + n + 1); for (int i = 1, j = 1; i <= n; i = j) { while (j <= n && a[j].first == a[i].first) { ++j; } for (int k = i; k < j; ++k) { if (a[k].second >= i && a[k].second < j) { to[a[k].second] = a[k].second; done[a[k].second] = true; ++m; } } int t = i; for (int k = i; k < j; ++k) { if (a[k].second < i || a[k].second >= j) { while (done[t]) { ++t; } to[a[k].second] = t++; } } } if (m < n) { puts("-1"); return 0; } for (int i = 1; i <= n; ++i) { f[i] = i; } for (int i = 1; i <= n; ++i) { f[find(i)] = find(to[i]); } for (int i = 1, j = 1; i <= n; i = j) { while (j <= n && a[j].first == a[i].first) { ++j; } int last = 0; for (int k = i; k < j; ++k) { if (!done[a[k].second]) { int t = to[a[k].second]; if (last && find(last) != find(t)) { f[find(last)] = find(t); swap(to[a[last].second], to[a[k].second]); } last = k; } } } int answer = 0; for (int i = 1; i <= n; ++i) { if (find(i) == i && to[i] != i) { ++answer; } } printf("%d\n", answer); for (int i = 1; i <= n; ++i) { if (find(i) == i && to[i] != i) { vector<int> a; a.push_back(i); for (int x = to[i]; x != i; x = to[x]) { a.push_back(x); } printf("%d\n", a.size()); for (int j = 0; j < a.size(); ++j) { printf("%d%c", a[j], j == a.size() - 1 ? '\n' : ' '); } } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
java
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; public class E { int[] sorted(int[] a) { a = a.clone(); for (int i = 0; i < a.length; i++) { int j = rand(0, i); int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } return a; } void submit() { int n = nextInt(); int s = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } Integer[] order = new Integer[n]; for (int i = 0; i < n; i++) { order[i] = i; } Arrays.sort(order, Comparator.comparingInt(x -> a[x])); int[] perm = new int[n]; Arrays.fill(perm, -1); int sum = 0; for (int i = 0; i < n; i++) { if (a[i] == a[order[i]]) { perm[i] = i; } else { sum++; } } if (sum > s) { out.println(-1); return; } for (int i = 0, j1 = 0, j2 = 0; i < sum; i++) { while (perm[order[j1]] == order[j1]) { j1++; } while (perm[j2] == j2) { j2++; } perm[order[j1]] = j2; j1++; j2++; } p = new int[n]; Arrays.fill(p, -1); for (int i = 0; i < n; i++) { for (int j = i; p[j] == -1; j = perm[j]) { p[j] = i; } } int lastIdx = -1, lastVal = -1; for (int i = 0; i < n; i++) { if (perm[i] == i) { continue; } int idx = order[i]; if (a[idx] == lastVal) { int v = get(idx); int u = get(lastIdx); if (v != u) { p[u] = v; int tmp = perm[idx]; perm[idx] = perm[lastIdx]; perm[lastIdx] = tmp; } } lastVal = a[idx]; lastIdx = idx; } ArrayList<ArrayList<Integer>> cycles = new ArrayList<>(); boolean[] used = new boolean[n]; for (int i = 0; i < n; i++) { if (perm[i] == i || used[i]) { continue; } ArrayList<Integer> cycle = new ArrayList<>(); for (int j = i; !used[j]; j = perm[j]) { used[j] = true; cycle.add(j); } cycles.add(cycle); } if (n > 1000) { for (List<Integer> lst : cycles) { out.print(lst.size() + " "); } } for (int merge = cycles.size(); merge >= 0; merge--) { if (merge == 1 || merge == 2) { continue; } int cost = sum + merge; if (cost > s) { continue; } int pop = cycles.size() - merge; out.println(pop + (merge > 0 ? 2 : 0)); for (int i = 0; i < pop; i++) { ArrayList<Integer> cycle = cycles.remove(cycles.size() - 1); out.println(cycle.size()); for (int x : cycle) { out.print(x + 1 + " "); } out.println(); } if (cycles.isEmpty()) { return; } int total = 0; for (ArrayList<Integer> cycle : cycles) { total += cycle.size(); } out.println(total); for (ArrayList<Integer> cycle : cycles) { for (int x : cycle) { out.print(x + 1 + " "); } } out.println(); out.println(cycles.size()); for (int i = cycles.size() - 1; i >= 0; i--) { out.print(cycles.get(i).get(0) + 1 + " "); } out.println(); return; } throw new AssertionError(); } int get(int v) { return p[v] == v ? v : (p[v] = get(p[v])); } int[] p; void test() { } void stress() { for (int tst = 0;; tst++) { if (false) { throw new AssertionError(); } System.err.println(tst); } } E() throws IOException { is = System.in; out = new PrintWriter(System.out); submit(); // stress(); // test(); out.close(); } static final Random rng = new Random(); static final int C = 5; static int rand(int l, int r) { return l + rng.nextInt(r - l + 1); } public static void main(String[] args) throws IOException { new E(); } private InputStream is; PrintWriter out; private byte[] buf = new byte[1 << 14]; private int bufSz = 0, bufPtr = 0; private int readByte() { if (bufSz == -1) throw new RuntimeException("Reading past EOF"); if (bufPtr >= bufSz) { bufPtr = 0; try { bufSz = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufSz <= 0) return -1; } return buf[bufPtr++]; } private boolean isTrash(int c) { return c < 33 || c > 126; } private int skip() { int b; while ((b = readByte()) != -1 && isTrash(b)) ; return b; } String nextToken() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!isTrash(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextString() { int b = readByte(); StringBuilder sb = new StringBuilder(); while (!isTrash(b) || b == ' ') { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } double nextDouble() { return Double.parseDouble(nextToken()); } char nextChar() { return (char) skip(); } int nextInt() { int ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } long nextLong() { long ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 200000; int n, lim; int a[MAXN]; int b[MAXN]; bool isfixed[MAXN]; pair<int, int> o[MAXN]; int no; int dst[MAXN]; int cyc[MAXN], ncyc; int par[MAXN], sz[MAXN]; int find(int a) { if (par[a] == a) return a; return par[a] = find(par[a]); } bool unite(int a, int b) { a = find(a), b = find(b); if (a == b) return false; if (sz[a] < sz[b]) swap(a, b); par[b] = a, sz[a] += sz[b]; return true; } bool done[MAXN]; vector<vector<int>> ans; bool solve() { for (int i = (0); i < (n); ++i) b[i] = a[i]; sort(b, b + n); int nfixed = 0; for (int i = (0); i < (n); ++i) { isfixed[i] = a[i] == b[i]; if (isfixed[i]) ++nfixed; } if (n - nfixed > lim) return false; no = 0; for (int i = (0); i < (n); ++i) if (!isfixed[i]) o[no++] = make_pair(a[i], i); sort(o, o + no); for (int i = (0); i < (n); ++i) dst[i] = isfixed[i] ? i : -1; { int at = 0; for (int i = (0); i < (no); ++i) { while (at < n && isfixed[at]) ++at; dst[o[i].second] = at++; } } for (int i = (0); i < (n); ++i) cyc[i] = -1; ncyc = 0; for (int i = (0); i < (n); ++i) if (cyc[i] == -1 && dst[i] != i) { int at = i; while (cyc[at] == -1) { cyc[at] = ncyc; at = dst[at]; } ++ncyc; } int expect = ncyc; for (int i = (0); i < (ncyc); ++i) par[i] = i, sz[i] = 1; for (int l = 0, r = l; l < no; l = r) { while (r < no && o[l].first == o[r].first) ++r; int cur = o[l].second; for (int i = (l + 1); i < (r); ++i) { int oth = o[i].second; if (unite(cyc[cur], cyc[oth])) { swap(dst[cur], dst[oth]); --expect; } } } for (int i = (0); i < (n); ++i) done[i] = false; ans.clear(); for (int i = (0); i < (n); ++i) if (!done[i] && dst[i] != i) { ans.push_back(vector<int>()); int at = i; while (!done[at]) { done[at] = true; ans.back().push_back(at); at = dst[at]; } } assert(((int)(ans).size()) == expect); int rem = lim - (n - nfixed); if (((int)(ans).size()) >= 3 && rem >= 3) { int cnt = min(rem, ((int)(ans).size())); vector<int> fst(cnt); for (int i = (0); i < (cnt); ++i) fst[i] = ans[i][0]; vector<int> snd; for (int i = (0); i < (cnt); ++i) { snd.push_back(fst[i]); int j = (i + 1) % cnt; for (int k = (1); k < (((int)(ans[j]).size())); ++k) snd.push_back(ans[j][k]); } vector<vector<int>> nans; nans.push_back(fst); nans.push_back(snd); for (int i = (cnt); i < (((int)(ans).size())); ++i) nans.push_back(ans[i]); ans = nans; } return true; } void run() { scanf("%d%d", &n, &lim); for (int i = (0); i < (n); ++i) scanf("%d", &a[i]); if (!solve()) { printf("-1\n"); return; } printf("%d\n", ((int)(ans).size())); for (int i = (0); i < (((int)(ans).size())); ++i) { printf("%d\n", ((int)(ans[i]).size())); for (int j = (0); j < (((int)(ans[i]).size())); ++j) { if (j != 0) printf(" "); printf("%d", ans[i][j] + 1); } puts(""); } } int main() { run(); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > adj[200000]; int nxt[200000]; bool v[200000]; vector<int> cycle; void dfs(int now, int src) { if (nxt[now] == adj[now].size()) { assert(now == src); return; } v[now] = true; cycle.push_back(adj[now][nxt[now]].second); nxt[now]++; dfs(adj[now][nxt[now] - 1].first, src); } void dfs2(int now) { v[now] = true; for (int i = 0; i < adj[now].size(); i++) { int to = adj[now][i].first; if (!v[to]) { dfs2(to); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, s; cin >> n >> s; vector<int> li; vector<int> all; for (int i = 0; i < n; i++) { nxt[i] = 0; v[i] = false; int x; cin >> x; li.push_back(x); all.push_back(x); } sort(all.begin(), all.end()); map<int, int> m; int zone[n]; int point = 0; for (int i = 0; i < n; i++) { if (i > 0 && all[i] == all[i - 1]) { continue; } for (int j = i; j < n && all[i] == all[j]; j++) { zone[j] = point; } m[all[i]] = point; point++; } for (int i = 0; i < n; i++) { int to = m[li[i]]; if (to == zone[i]) { continue; } adj[zone[i]].push_back(make_pair(to, i + 1)); } int comp = 0; for (int i = 0; i < point; i++) { if (!v[i] && adj[i].size() > 0) { dfs2(i); comp++; } } for (int i = 0; i < point; i++) { v[i] = false; } vector<vector<int> > ans; int tot = 0; for (int i = 0; i < point; i++) { if (v[i] || adj[i].size() == 0) { continue; } cycle.clear(); dfs(i, i); tot += (int)cycle.size(); ans.push_back(cycle); } if (tot > s) { cout << -1 << endl; return 0; } int should = ans.size(); int red = (s - tot) - 2; int comb = 0; if (red > 0) { comb = min(red + 2, comp); } if (comb > 1) { vector<vector<int> > lis; int sz = ans.size(); for (int i = 1; i <= comb; i++) { lis.push_back(ans.back()); ans.pop_back(); } vector<int> l1; vector<int> l2; for (int i = lis.size() - 1; i >= 0; i--) { for (int j = 0; j < lis[i].size(); j++) { l1.push_back(lis[i][j]); } } for (int i = 0; i < lis.size(); i++) { l2.push_back(lis[i][0]); } ans.push_back(l1); ans.push_back(l2); } cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i].size() << endl; cout << ans[i][0]; for (int j = 1; j < ans[i].size(); j++) { cout << " " << ans[i][j]; } cout << endl; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 20; int a[maxn], q[maxn], num[maxn], par[maxn]; bool visited[maxn]; vector<int> ind[maxn], cycle[maxn]; int f(int v) { return (par[v] == -1) ? v : par[v] = f(par[v]); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); memset(par, -1, sizeof par); int n, s; cin >> n >> s; vector<int> tmp; for (int i = 0; i < n; i++) cin >> a[i], tmp.push_back(a[i]); sort(tmp.begin(), tmp.end()); tmp.resize(unique(tmp.begin(), tmp.end()) - tmp.begin()); for (int i = 0; i < n; i++) a[i] = lower_bound(tmp.begin(), tmp.end(), a[i]) - tmp.begin(); for (int i = 0; i < n; i++) ind[a[i]].push_back(i); int t = 0; for (int i = 0; i < (int)tmp.size(); i++) { int sz = ind[i].size(); vector<int> tmp2 = ind[i]; for (auto &x : ind[i]) if (t <= x && x < t + sz) { a[x] = x; visited[x] = 1; x = 1e9; } sort(ind[i].begin(), ind[i].end()); while (!ind[i].empty() && ind[i].back() > n) ind[i].pop_back(); tmp2 = ind[i]; for (int j = t; j < t + sz; j++) if (!visited[j]) a[tmp2.back()] = j, tmp2.pop_back(); t += sz; } t = 0; memset(par, -1, sizeof par); for (int i = 0; i < n; i++) if (!visited[i]) { while (!visited[i]) { visited[i] = 1; cycle[t].push_back(i); i = a[i]; } for (int j = 1; j < (int)cycle[t].size(); j++) par[cycle[t][j]] = cycle[t][0]; t++; } for (int i = 0; i < (int)tmp.size(); i++) { if (ind[i].empty()) continue; int marja = ind[i].back(); ind[i].pop_back(); for (auto shit : ind[i]) { if (f(shit) == f(marja)) continue; par[f(shit)] = f(marja); swap(a[shit], a[marja]); } } memset(visited, 0, sizeof visited); for (int i = 0; i < t; i++) cycle[i].clear(); t = 0; int T = 0; for (int i = 0; i < n; i++) if (!visited[i]) { int sz = 0; while (!visited[i]) { sz++; cycle[t].push_back(i); visited[i] = 1; i = a[i]; } if (sz != 1) t++; else cycle[t].clear(); } if (!t) return cout << 0 << endl, 0; if (t == 1) { if (s < (int)cycle[0].size()) return cout << -1 << endl, 0; cout << 1 << endl; cout << cycle[0].size() << endl; for (auto x : cycle[0]) cout << x + 1 << " "; cout << endl; return 0; } for (int i = 0; i < t; i++) T += (int)cycle[i].size(); if (s < T + t) return cout << -1 << endl, 0; cout << 2 << endl; cout << T << endl; for (int i = 0; i < t; i++) for (auto x : cycle[i]) cout << x + 1 << " "; cout << endl; cout << t << endl; for (int i = 0; i < t; i++) cout << cycle[i][0] + 1 << " "; cout << endl; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using namespace rel_ops; using ll = int64_t; using Pii = pair<int, int>; using ull = uint64_t; using Vi = vector<int>; void run(); int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); return 0; } struct Vert { Vi E, src; int in{0}; }; Vi elems; vector<Vert> G; vector<Vi> cycles; void dfs(int v) { while (!G[v].E.empty()) { int e = G[v].E.back(); int dst = G[e].src.back(); G[v].E.pop_back(); G[e].src.pop_back(); dfs(e); cycles.back().push_back(dst); } } bool debugCycles() { Vi copy = elems; for (auto& c : (cycles)) { int tmp = copy[c.back()]; for (int i = int((c).size()) - 1; i > 0; i--) { copy[c[i]] = copy[c[i - 1]]; } copy[c[0]] = tmp; } int last = 0; for (auto& first : (copy)) { if (last > first) return false; last = first; } return true; } void run() { int n, upper; cin >> n >> upper; elems.resize(n); for (auto& e : (elems)) cin >> e; Vi sorted(n); iota((sorted).begin(), (sorted).end(), 0); sort((sorted).begin(), (sorted).end(), [&](int l, int r) { return elems[l] < elems[r]; }); int last = elems[sorted[0]], k = 0; for (int i = (0); i < (n); i++) { int e = sorted[i]; if (elems[e] != last) k++; last = elems[e]; elems[e] = k; } k++; G.resize(k); for (int i = (0); i < (n); i++) if (elems[i] != elems[sorted[i]]) { G[elems[i]].src.push_back(i); G[elems[i]].E.push_back(elems[sorted[i]]); G[elems[sorted[i]]].in++; } for (auto& v : (G)) if (v.in != int((v.E).size())) { cout << "not eulerian\n"; break; } int len = 0; for (int i = (0); i < (k); i++) if (!G[i].E.empty()) { cycles.emplace_back(); dfs(i); len += int((cycles.back()).size()); } if (len > upper) { cout << "-1\n"; return; } if (!debugCycles()) { cout << "euler failed\n"; } int toMerge = min(int((cycles).size()), upper - len); if (toMerge > 2) { Vi one, two; for (int i = (0); i < (toMerge); i++) { one.insert(one.end(), (cycles.back()).begin(), (cycles.back()).end()); two.push_back(cycles.back()[0]); cycles.pop_back(); } reverse((two).begin(), (two).end()); cycles.push_back(one); cycles.push_back(two); } if (!debugCycles()) { cout << "merge failed\n"; } cout << int((cycles).size()) << '\n'; for (auto& c : (cycles)) { cout << int((c).size()) << '\n'; for (auto& first : (c)) cout << first + 1 << ' '; cout << '\n'; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 300000; map<int, int> mapa; map<pair<int, int>, vector<int>> pos; vector<int> g[MAXN]; int ptr[MAXN]; int used[MAXN]; void euler(int v, vector<int> &res) { used[v] = true; for (; ptr[v] < (int)(g[v]).size();) { ++ptr[v]; int u = g[v][ptr[v] - 1]; euler(u, res); res.push_back(u); } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n, s; cin >> n >> s; int k = 0; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> b = a; sort((b).begin(), (b).end()); int m = 0; for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { continue; } ++m; if (!mapa.count(b[i])) { mapa[b[i]] = k++; } } if (m > s) { cout << -1 << endl; return 0; } for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { continue; } a[i] = mapa[a[i]]; b[i] = mapa[b[i]]; g[b[i]].push_back(a[i]); pos[{b[i], a[i]}].push_back(i); } vector<vector<int>> cycles; for (int i = 0; i < k; ++i) { if (!used[i]) { vector<int> arr; euler(i, arr); reverse((arr).begin(), (arr).end()); cycles.push_back({}); for (int i = 0; i < (int)(arr).size(); ++i) { int j = (i + 1) % (int)(arr).size(); cycles.back().push_back(pos[{arr[i], arr[j]}].back()); pos[{arr[i], arr[j]}].pop_back(); } } } vector<vector<int>> res; if (s - m > 1 && (int)(cycles).size() > 1) { int len = min((int)(cycles).size(), s - m); res.push_back({}); vector<int> newcycle; for (int i = (int)(cycles).size() - len; i < (int)(cycles).size(); ++i) { res.back().push_back(cycles[i].back()); for (int j : cycles[i]) { newcycle.push_back(j); } } for (int i = 0; i < len; ++i) { cycles.pop_back(); } cycles.push_back(newcycle); } for (int i = 0; i < (int)(cycles).size(); ++i) { res.push_back(cycles[i]); } cout << (int)(res).size() << endl; for (int i = 0; i < (int)(res).size(); ++i) { cout << (int)(res[i]).size() << endl; for (int j : res[i]) { cout << j + 1 << " "; } cout << endl; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
java
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; public class E { int[] sorted(int[] a) { a = a.clone(); for (int i = 0; i < a.length; i++) { int j = rand(0, i); int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } return a; } void submit() { int n = nextInt(); int s = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } Integer[] order = new Integer[n]; for (int i = 0; i < n; i++) { order[i] = i; } Arrays.sort(order, Comparator.comparingInt(x -> a[x])); int[] perm = new int[n]; Arrays.fill(perm, -1); int sum = 0; for (int i = 0; i < n; i++) { if (a[i] == a[order[i]]) { perm[i] = i; } else { sum++; } } if (sum > s) { out.println(-1); return; } for (int i = 0, j1 = 0, j2 = 0; i < sum; i++) { while (perm[order[j1]] == order[j1]) { j1++; } while (perm[j2] == j2) { j2++; } perm[order[j1]] = j2; j1++; j2++; } p = new int[n]; Arrays.fill(p, -1); for (int i = 0; i < n; i++) { for (int j = i; p[j] == -1; j = perm[j]) { p[j] = i; } } int lastIdx = -1, lastVal = -1; for (int i = 0; i < n; i++) { if (perm[i] == i) { continue; } int idx = order[i]; if (a[idx] == lastVal) { int v = get(idx); int u = get(lastIdx); if (v != u) { p[u] = v; int tmp = perm[idx]; perm[idx] = perm[lastIdx]; perm[lastIdx] = tmp; } } lastVal = a[idx]; lastIdx = idx; } ArrayList<ArrayList<Integer>> cycles = new ArrayList<>(); boolean[] used = new boolean[n]; for (int i = 0; i < n; i++) { if (perm[i] == i || used[i]) { continue; } ArrayList<Integer> cycle = new ArrayList<>(); for (int j = i; !used[j]; j = perm[j]) { used[j] = true; cycle.add(j); } cycles.add(cycle); } if (n > 1000) { out.println(sum); for (List<Integer> lst : cycles) { out.print(lst.size() + " "); } out.println(); out.println("-------"); } for (int merge = cycles.size(); merge >= 0; merge--) { if (merge == 1 || merge == 2) { continue; } int cost = sum + merge; if (cost > s) { continue; } int pop = cycles.size() - merge; out.println(pop + (merge > 0 ? 2 : 0)); for (int i = 0; i < pop; i++) { ArrayList<Integer> cycle = cycles.remove(cycles.size() - 1); out.println(cycle.size()); for (int x : cycle) { out.print(x + 1 + " "); } out.println(); } if (cycles.isEmpty()) { return; } int total = 0; for (ArrayList<Integer> cycle : cycles) { total += cycle.size(); } out.println(total); for (ArrayList<Integer> cycle : cycles) { for (int x : cycle) { out.print(x + 1 + " "); } } out.println(); out.println(cycles.size()); for (int i = cycles.size() - 1; i >= 0; i--) { out.print(cycles.get(i).get(0) + 1 + " "); } out.println(); return; } throw new AssertionError(); } int get(int v) { return p[v] == v ? v : (p[v] = get(p[v])); } int[] p; void test() { } void stress() { for (int tst = 0;; tst++) { if (false) { throw new AssertionError(); } System.err.println(tst); } } E() throws IOException { is = System.in; out = new PrintWriter(System.out); submit(); // stress(); // test(); out.close(); } static final Random rng = new Random(); static final int C = 5; static int rand(int l, int r) { return l + rng.nextInt(r - l + 1); } public static void main(String[] args) throws IOException { new E(); } private InputStream is; PrintWriter out; private byte[] buf = new byte[1 << 14]; private int bufSz = 0, bufPtr = 0; private int readByte() { if (bufSz == -1) throw new RuntimeException("Reading past EOF"); if (bufPtr >= bufSz) { bufPtr = 0; try { bufSz = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufSz <= 0) return -1; } return buf[bufPtr++]; } private boolean isTrash(int c) { return c < 33 || c > 126; } private int skip() { int b; while ((b = readByte()) != -1 && isTrash(b)) ; return b; } String nextToken() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!isTrash(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextString() { int b = readByte(); StringBuilder sb = new StringBuilder(); while (!isTrash(b) || b == ' ') { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } double nextDouble() { return Double.parseDouble(nextToken()); } char nextChar() { return (char) skip(); } int nextInt() { int ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } long nextLong() { long ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 300000; map<int, int> mapa; vector<int> pos[MAXN]; vector<int> g[MAXN]; int ptr[MAXN]; int used[MAXN]; void euler(int v, vector<int> &res) { used[v] = true; for (; ptr[v] < (int)(g[v]).size();) { ++ptr[v]; euler(g[v][ptr[v] - 1], res); } if ((int)(pos[v]).size() > 0) { res.push_back(pos[v].back()); pos[v].pop_back(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, s; cin >> n >> s; int k = 0; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> b = a; sort((b).begin(), (b).end()); int m = 0; for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { continue; } ++m; if (!mapa.count(b[i])) { mapa[b[i]] = k++; } } if (m > s) { cout << -1 << endl; return 0; } for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { continue; } a[i] = mapa[a[i]]; b[i] = mapa[b[i]]; g[a[i]].push_back(b[i]); pos[b[i]].push_back(i); } vector<vector<int>> cycles; for (int i = 0; i < k; ++i) { if (!used[i]) { cycles.push_back({}); euler(i, cycles.back()); } } vector<vector<int>> res; if (s - m > 1 && (int)(cycles).size() > 1) { int len = min((int)(cycles).size(), s - m); res.push_back({}); vector<int> newcycle; for (int i = (int)(cycles).size() - len; i < (int)(cycles).size(); ++i) { res.back().push_back(cycles[i].back()); for (int j : cycles[i]) { newcycle.push_back(j); } } for (int i = 0; i < len; ++i) { cycles.pop_back(); } cycles.push_back(newcycle); } for (int i = 0; i < (int)(cycles).size(); ++i) { res.push_back(cycles[i]); } cout << (int)(res).size() << endl; for (int i = 0; i < (int)(res).size(); ++i) { cout << (int)(res[i]).size() << endl; for (int j : res[i]) { cout << j + 1 << " "; } cout << endl; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int fail() { printf("-1\n"); return 0; } const int MN = 2e5 + 100; const int MS = MN; int N, S, a[MN], v[MN], g[MN], V, f; using i2 = array<int, 2>; i2 b[MN]; using vi2 = vector<i2>; vi2 w[MN]; bool u[MN]; using vi = vector<int>; vi c[MN]; int C; void dfs(int n, int p = -1) { u[n] = true; for (; not w[n].empty();) { i2 x = w[n].back(); w[n].pop_back(); dfs(x[1], x[0]); } if (p != -1) c[C].push_back(p); } void solve(int n) { c[C].clear(); dfs(n); assert(not c[C].empty()); C++; } int main() { scanf("%d%d", &N, &S); for (int i = 0; i < N; i++) scanf("%d", a + i), v[i] = a[i]; sort(v, v + N); V = -1; for (int i = 0; i < N; i++) { if (not i or v[i] != v[i - 1]) b[++V][0] = i; v[V] = v[i], g[i] = V; b[V][1] = i + 1; } V++; f = 0; for (int i = 0; i < N; i++) f += i < b[a[i] = lower_bound(v, v + V, a[i]) - v][0] or b[a[i]][1] <= i; if (f > S) return fail(); for (int i = 0; i < N; i++) if (a[i] != g[i]) w[g[i]].push_back({i, a[i]}); for (int i = 0; i < V; i++) u[i] = false; C = 0; for (int i = 0; i < V; i++) if (not u[i] and not w[i].empty()) solve(i); printf("%d\n", C); for (int i = 0; i < C; i++) { printf("%lu\n", c[i].size()); for (int j = c[i].size() - 1; j >= 0; j--) printf("%d ", c[i][j] + 1); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int max_n = 200222, inf = 1000111222; int n, s, cnt, to[max_n]; int a[max_n], b[max_n], pos[max_n]; vector<int> all[max_n]; void compress() { pair<int, int> p[max_n]; for (int i = 0; i < n; ++i) { p[i] = {a[i], i}; } sort(p, p + n); int num = 0; for (int i = 0; i < n;) { int start = i; while (i < n && p[i].first == p[start].first) { a[p[i].second] = num; ++i; } ++num; } } vector<vector<int>> cycles; vector<pair<int, int>> g[max_n]; int parent[max_n]; void init() { for (int i = 1; i <= n; ++i) { parent[i] = i; } } int find_set(int v) { if (v == parent[v]) { return v; } return parent[v] = find_set(parent[v]); } void union_set(int v1, int v2) { v1 = find_set(v1); v2 = find_set(v2); parent[v1] = v2; } void find_all_cycles() { cycles.clear(); bool used[max_n]; memset(used, 0, sizeof(used)); for (int i = 0; i < n; ++i) { if (used[i] == 0 && to[i] != -1) { int pos = i; vector<int> cycle; while (used[pos] == 0) { used[pos] = 1; cycle.push_back(pos); g[a[pos]].push_back({cycles.size(), pos}); pos = to[pos]; } cycles.push_back(cycle); } } } int main() { scanf("%d%d", &n, &s); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } compress(); copy(a, a + n, b); sort(b, b + n); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { all[a[i]].push_back(i); } to[i] = -1; } for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { int p = all[b[i]][pos[b[i]]++]; to[p] = i; ++cnt; } } if (cnt > s) { puts("-1"); return 0; } find_all_cycles(); int ans = cycles.size(); init(); for (int i = 0; i < n; ++i) { if (g[i].size() > 1) { const pair<int, int> &first = g[i][0]; for (const pair<int, int> &p : g[i]) { if (find_set(first.first) != find_set(p.first)) { --ans; union_set(first.first, p.first); swap(to[first.second], to[p.second]); } } } } find_all_cycles(); printf("%d\n", ans); for (const vector<int> &cycle : cycles) { printf("%d\n", cycle.size()); for (int pos : cycle) { printf("%d ", pos + 1); } printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mxN = 2e5; int n, s, a[mxN]; map<int, int> mp1; map<int, vector<int>> mp2; vector<vector<int>> ans; void dfs(int u) { while (!mp2[u].empty()) { int i = mp2[u].back(); mp2[u].pop_back(); ans.back().push_back(i); dfs(a[i]); } mp2.erase(u); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> s; for (int i = 0; i < n; ++i) { cin >> a[i]; ++mp1[a[i]]; } int j = 0; for (auto it = mp1.begin(); it != mp1.end(); ++it) { for (int i = j; i < j + it->second; ++i) if (a[i] != it->first) mp2[it->first].push_back(i); j += it->second; } while (!mp2.empty()) { ans.push_back(vector<int>()); dfs(mp2.begin()->first); s -= ans.back().size(); } if (s < 0) { cout << -1; return 0; } int b = min(s, (int)ans.size()), d = 0; cout << ans.size() - (b >= 3 ? b - 2 : 0) << "\n"; if (b >= 3) { for (int i = 0; i < b; ++i) d += ans[i].size(); cout << d << "\n"; for (int i = 0; i < b; ++i) for (int c : ans[i]) cout << c + 1 << " "; cout << "\n" << b << "\n"; for (int i = b - 1; i >= 0; --i) cout << ans[i][0] << " "; cout << "\n"; } for (int i = b; i < ans.size(); ++i) { cout << ans[i].size() << "\n"; for (int c : ans[i]) cout << c + 1 << " "; cout << "\n"; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; const int MX = 1e9 + 7; const long long int INF = 1e18 + 9LL; int n, s; int in[N]; int last[N]; int sorted[N]; set<int> place[N]; set<int> value[N]; int cnt; map<int, int> M; int dir[N]; bool vis[N]; vector<int> cur; void solve(int c) { if (place[c].size() == 0) return; int v1 = *place[c].begin(), v2 = *value[c].begin(); place[c].erase(v1); value[c].erase(v2); int first = v1; dir[v1] = v2; vector<int> cycle; cycle.push_back(v1); while (v2 != first) { v1 = v2; last[in[v1]] = v1; cycle.push_back(v1); c = in[v1]; place[c].erase(v1); v2 = *value[c].begin(); value[c].erase(v2); dir[v1] = v2; } if (last[in[first]] > 0) swap(dir[first], dir[last[in[first]]]); last[in[first]] = first; for (int v : cycle) if (place[in[v]].size()) solve(in[v]); } void dfs(int u) { vis[u] = true; cur.push_back(u); if (!vis[dir[u]]) dfs(dir[u]); } int main() { scanf("%d %d", &n, &s); for (int i = 1; i <= n; ++i) { scanf("%d", &in[i]); sorted[i] = in[i]; } int inc = 0; sort(sorted + 1, sorted + n + 1); for (int i = 1; i <= n; ++i) { if (in[i] != sorted[i]) ++inc; if (!M.count(sorted[i])) M[sorted[i]] = ++cnt; } if (inc > s) { puts("-1"); return 0; } for (int i = 1; i <= n; ++i) { in[i] = M[in[i]]; sorted[i] = M[sorted[i]]; } for (int i = 1; i <= n; ++i) if (in[i] != sorted[i]) { place[in[i]].insert(i); value[sorted[i]].insert(i); } vector<vector<int> > ans; for (int i = 1; i <= n; ++i) { if (place[i].size() == 0) continue; solve(i); } for (int i = 1; i <= n; ++i) vis[i] = in[i] == sorted[i]; for (int i = 1; i <= n; ++i) if (!vis[i]) { cur.clear(); dfs(i); ans.push_back(cur); } printf("%d\n", (int)ans.size()); for (auto V : ans) { printf("%d\n", (int)V.size()); for (auto v : V) printf("%d ", v); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct ln { int val; ln* next; }; vector<pair<int, int> >* nl; int* roi; bool* btdt; ln* fe(ln* ath, int cp) { for (int i = roi[cp]; i < nl[cp].size(); i = roi[cp]) { int np = nl[cp][i].first; int ind = nl[cp][i].second; if (btdt[ind]) { continue; } roi[cp]++; btdt[ind] = 1; ln* cn = new ln(); cn->val = ind; cn->next = NULL; ln* vas = fe(cn, np); ath->next = cn; ath = vas; } return ath; } int main() { cin.sync_with_stdio(0); cout.sync_with_stdio(0); int n, s; cin >> n >> s; vector<pair<int, int> > ar(n); nl = new vector<pair<int, int> >[n]; btdt = new bool[n]; roi = new int[n]; for (int i = 0; i < n; i++) { roi[i] = 0; vector<pair<int, int> > vpii; nl[i] = vpii; btdt[i] = 0; int a; cin >> a; ar[i] = make_pair(a, i); } vector<pair<int, int> > br = ar; sort(br.begin(), br.end()); unordered_map<int, int> nct; int cn = -1; int an = -1; for (int i = 0; i < n; i++) { if (br[i].first != an) { cn++; an = br[i].first; nct[an] = cn; } br[i].first = cn; } for (int i = 0; i < n; i++) { ar[i].first = nct[ar[i].first]; } for (int i = 0; i < n; i++) { if (br[i].first == ar[i].first) { continue; } s--; nl[br[i].first].push_back(make_pair(ar[i].first, ar[i].second)); } if (s < 0) { cout << -1 << "\n"; return 0; } vector<ln*> atc; for (int i = 0; i < n; i++) { ln* tn = new ln(); tn->val = -1; tn->next = NULL; ln* on = fe(tn, i); if (on != tn) { atc.push_back(tn->next); } } int noc = atc.size(); int hmg = min(s, noc); vector<vector<int> > vas; if (hmg == 1) { hmg--; } if (hmg > 0) { vector<int> fv; vector<int> sv; for (int i = hmg - 1; i >= 0; i--) { fv.push_back(atc[i]->val); } for (int i = 0; i < hmg; i++) { int oi = i - 1; if (oi < i) { oi += hmg; } sv.push_back(atc[oi]->val); ln* cn = atc[i]->next; while (cn != NULL) { sv.push_back(cn->val); cn = cn->next; } } vas.push_back(fv); vas.push_back(sv); } for (int i = hmg; i < atc.size(); i++) { vector<int> sv; ln* cn = atc[i]; while (cn != NULL) { sv.push_back(cn->val); cn = cn->next; } vas.push_back(sv); } cout << vas.size() << "\n"; for (int i = 0; i < vas.size(); i++) { cout << vas[i].size() << "\n"; for (int j = 0; j < vas[i].size(); j++) { if (j > 0) { cout << " "; } cout << (vas[i][j] + 1); } cout << "\n"; } cin >> n; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using namespace chrono; const int infinity = (int)1e9 + 42; const int64_t llInfinity = (int64_t)1e18 + 256; const int module = (int)1e9 + 7; const long double eps = 1e-8; mt19937_64 randGen(system_clock().now().time_since_epoch().count()); inline void raiseError(string errorCode) { cerr << "Error : " << errorCode << endl; exit(42); } inline void test(istream &cin, ostream &cout) { int n, s; cin >> n >> s; vector<int> v(n); map<int, int> kompr; for (int i = 0; i < n; i++) { cin >> v[i]; kompr[v[i]] = 42; } int k = 0; for (auto &it : kompr) { it.second = k++; } for (int i = 0; i < n; i++) { v[i] = kompr[v[i]]; } auto w = v; sort(w.begin(), w.end()); vector<vector<int> > g(k); map<pair<int, int>, vector<int> > indices; for (int i = 0; i < n; i++) { if (w[i] == v[i]) { continue; } s--; g[w[i]].push_back(v[i]); indices[{w[i], v[i]}].push_back(i); } if (s < 0) { cout << -1 << "\n"; return; } vector<int> pass; function<void(int)> dfs = [&](int v) { while (!g[v].empty()) { int to = g[v].back(); g[v].pop_back(); dfs(to); } pass.push_back(v); }; vector<vector<int> > ans; for (int i = 0; i < k; i++) { if (g[i].empty()) { continue; } pass.clear(); dfs(i); ans.emplace_back(); for (int j = 1; j < (int)pass.size(); j++) { auto &vec = indices[{pass[j], pass[j - 1]}]; assert(!vec.empty()); ans.back().push_back(vec.back()); vec.pop_back(); } reverse(ans.back().begin(), ans.back().end()); } cout << ans.size() << "\n"; for (auto vec : ans) { cout << vec.size() << "\n"; for (auto it : vec) { cout << it + 1 << " "; } cout << "\n"; } } signed main() { ios_base::sync_with_stdio(false); test(cin, cout); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f2f1f0f; const long long LINF = 1ll * INF * INF; const int MAX_N = 2e5 + 100; int N, S, Nr[MAX_N], Sort[MAX_N]; vector<int> Nrs[MAX_N], Sorts[MAX_N]; vector<vector<int>> Ans; int Go[MAX_N]; int main() { srand(2016101); cin >> N >> S; vector<int> Co; for (int i = 1; i <= N; i++) scanf("%d", &Nr[i]), Co.push_back(Nr[i]); sort((Co).begin(), (Co).end()); Co.erase(unique((Co).begin(), (Co).end()), Co.end()); for (int i = 1; i <= N; i++) Nr[i] = lower_bound((Co).begin(), (Co).end(), Nr[i]) - Co.begin(); for (int i = 1; i <= N; i++) Sort[i] = Nr[i]; sort(Sort + 1, Sort + N + 1); int cnt = 0; for (int i = 1; i <= N; i++) if (Sort[i] != Nr[i]) { cnt++; Nrs[Nr[i]].push_back(i); Sorts[Sort[i]].push_back(i); } if (S < cnt) { puts("-1"); return 0; } for (int c = 0; c < ((int)(Co).size()); c++) { vector<int> list; for (int i = 0; i < ((int)(Nrs[c]).size()); i++) list.push_back(i); for (int i = 1; i < ((int)(Nrs[c]).size()); i++) swap(list[i], list[rand() % (i + 1)]); for (int i = 0; i < ((int)(Nrs[c]).size()); i++) Go[Nrs[c][i]] = Sorts[c][list[i]]; } vector<bool> vis(N + 1, false); for (int i = 1; i <= N; i++) if ((not vis[i]) && Go[i] != 0) { int now = i; Ans.push_back(vector<int>()); while (not vis[now]) { vis[now] = true; Ans.back().push_back(now); now = Go[now]; } } printf("%d\n", ((int)(Ans).size())); for (auto &vec : Ans) { printf("%d\n", ((int)(vec).size())); for (int &x : vec) printf("%d ", x); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; const int MX = 1e9 + 7; const long long int INF = 1e18 + 9LL; int n, s; int in[N]; int last[N]; int sorted[N]; set<int> place[N]; set<int> value[N]; int cnt; map<int, int> M; int dir[N]; bool vis[N]; vector<int> cur; void solve(int c) { if (place[c].size() == 0) return; int v1 = *place[c].begin(), v2 = *value[c].begin(); place[c].erase(v1); value[c].erase(v2); int first = v1; dir[v1] = v2; vector<int> cycle; cycle.push_back(v1); while (v2 != first) { v1 = v2; last[in[v1]] = v1; cycle.push_back(v1); c = in[v1]; place[c].erase(v1); v2 = *value[c].begin(); value[c].erase(v2); dir[v1] = v2; } if (last[in[first]] > 0) swap(dir[first], dir[last[in[first]]]); last[in[first]] = first; for (int v : cycle) if (place[in[v]].size()) solve(in[v]); } void dfs(int u) { vis[u] = true; cur.push_back(u); if (!vis[dir[u]]) dfs(dir[u]); } int main() { scanf("%d %d", &n, &s); for (int i = 1; i <= n; ++i) { scanf("%d", &in[i]); sorted[i] = in[i]; } int inc = 0; sort(sorted + 1, sorted + n + 1); for (int i = 1; i <= n; ++i) { if (in[i] != sorted[i]) ++inc; if (!M.count(sorted[i])) M[sorted[i]] = ++cnt; } if (inc > s) { puts("-1"); return 0; } for (int i = 1; i <= n; ++i) { in[i] = M[in[i]]; sorted[i] = M[sorted[i]]; } for (int i = 1; i <= n; ++i) if (in[i] != sorted[i]) { place[in[i]].insert(i); value[sorted[i]].insert(i); } vector<vector<int> > ans; for (int i = 1; i <= n; ++i) { if (place[i].size() == 0) continue; solve(i); } for (int i = 1; i <= n; ++i) vis[i] = in[i] == sorted[i]; for (int i = 1; i <= n; ++i) if (!vis[i]) { cur.clear(); dfs(i); ans.push_back(cur); } s -= inc; if (s >= 3 && ans.size() >= 3) { s = min(s, (int)ans.size()); vector<int> help, help2; while (s--) { for (auto v : ans.back()) help.push_back(v); help2.push_back(ans.back()[0]); ans.pop_back(); } reverse(help2.begin(), help2.end()); ans.push_back(help); ans.push_back(help2); } printf("%d\n", (int)ans.size()); for (auto V : ans) { printf("%d\n", (int)V.size()); for (auto v : V) printf("%d ", v); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 25; int a[N], b[N], c[N], cycsz; vector<int> g[N], cyc[N]; void dfs(int v) { while (!g[v].empty()) { int u = g[v].back(); g[v].pop_back(); dfs(a[u]); cyc[cycsz].push_back(u); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout << setprecision(32); int n, s; cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } sort(b, b + n); memcpy(c, b, sizeof(int) * n); int sz = unique(c, c + n) - c; for (int i = 0; i < n; i++) { a[i] = lower_bound(c, c + sz, a[i]) - c; b[i] = lower_bound(c, c + sz, b[i]) - c; if (a[i] == b[i]) continue; g[b[i]].push_back(i); } cycsz = 0; for (int i = 0; i < sz; i++) { if (g[i].empty()) continue; dfs(i); cycsz++; } int sum = 0; for (int i = 0; i < cycsz; i++) { sum += cyc[i].size(); reverse(cyc[i].begin(), cyc[i].end()); } if (sum > s) { cout << -1 << '\n'; exit(0); } int x = min(cycsz, s - sum); vector<vector<int> > ans; if (x) { vector<int> perm1, perm2; for (int i = 0; i < x; i++) { for (auto y : cyc[i]) { perm1.push_back(y); } perm2.push_back(cyc[i].back()); } reverse(perm2.begin(), perm2.end()); ans.push_back(perm1); ans.push_back(perm2); } for (int i = x; i < cycsz; i++) { ans.push_back(cyc[i]); } cout << ans.size() << '\n'; for (auto vec : ans) { cout << vec.size() << '\n'; for (auto y : vec) { cout << y + 1 << " "; } cout << '\n'; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Data { int v, i; bool operator<(const Data &r) const { return v < r.v || v == r.v && i < r.i; } }; int n, s, a[200000]; Data so[200000]; int main() { while (scanf("%d %d", &n, &s) != EOF) { for (int i = int(0); i < int(n); i++) { scanf("%d", &a[i]); so[i] = Data{a[i], i}; } sort(so, so + n); map<int, queue<int> > m; for (int i = int(0); i < int(n); i++) { if (so[i].v == a[i]) continue; if (m.find(so[i].v) == m.end()) { queue<int> q; m.insert(pair<int, queue<int> >(so[i].v, q)); } m[so[i].v].push(i); } vector<vector<int> > ans; while (m.size()) { int f = m.begin()->first; int v = f; bool ff = true; vector<int> tt; while (v != f || ff) { queue<int> &q = m[v]; int idx = q.front(); q.pop(); if (q.empty()) m.erase(v); tt.push_back(idx); v = a[idx]; ff = false; } ans.push_back(tt); } if (ans.size() <= s) { printf("%d\n", (int)ans.size()); for (int i = int(0); i < int(ans.size()); i++) { printf("%d\n", (int)ans[i].size()); for (int j = int(0); j < int(ans[i].size()); j++) printf("%d%c", ans[i][j] + 1, " \n"[j == ans[i].size() - 1]); } } else printf("-1\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using cat = long long; void DFS(int R, vector<vector<int> >& G, vector<bool>& vis, vector<int>& cyc) { cyc.push_back(R); vis[R] = true; while (!G[R].empty()) { int v = G[R].back(); G[R].pop_back(); DFS(v, G, vis, cyc); } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int N, S; cin >> N >> S; vector<int> A(N); for (int i = 0; i < N; i++) A[i] = rand() % 100000; vector<int> As(A); sort(begin(As), end(As)); int wrong = 0; for (int i = 0; i < N; i++) if (A[i] != As[i]) wrong++; if (S < wrong) { cout << "-1\n"; return 0; } map<int, int> M; for (int i = 0; i < N; i++) M[A[i]] = 0; int m = 0; for (auto it = M.begin(); it != M.end(); it++) it->second = m++; for (int i = 0; i < N; i++) A[i] = M[A[i]], As[i] = M[As[i]]; vector<vector<int> > G(N + m); for (int i = 0; i < N; i++) if (A[i] != As[i]) G[i].push_back(A[i] + N); for (int i = 0; i < N; i++) if (A[i] != As[i]) G[As[i] + N].push_back(i); vector<bool> vis(N + m, false); vector<vector<int> > eulerc; for (int i = 0; i < N; i++) if (!vis[i] && A[i] != As[i]) { vector<int> c; DFS(i, G, vis, c); eulerc.push_back(vector<int>()); c.pop_back(); for (auto it = c.begin(); it != c.end(); it++) if (*it < N) eulerc.back().push_back(*it); } cout << eulerc.size() << "\n"; for (int i = 0; i < (int)eulerc.size(); i++) { cout << eulerc[i].size(); for (auto it = eulerc[i].begin(); it != eulerc[i].end(); it++) cout << " " << *it + 1; cout << "\n"; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct ln { int val; ln* next; }; vector<pair<int, int> >* nl; int* roi; bool* btdt; ln* fe(ln* ath, int cp) { for (int i = roi[cp]; i < nl[cp].size(); i = roi[cp]) { int np = nl[cp][i].first; int ind = nl[cp][i].second; if (btdt[ind]) { continue; } roi[cp]++; btdt[ind] = 1; ln* cn = new ln(); cn->val = ind; cn->next = NULL; ln* vas = fe(ath, np); vas->next = cn; ath = cn; } return ath; } int main() { cin.sync_with_stdio(0); cout.sync_with_stdio(0); int n, s; cin >> n >> s; vector<pair<int, int> > ar(n); nl = new vector<pair<int, int> >[n]; btdt = new bool[n]; roi = new int[n]; for (int i = 0; i < n; i++) { roi[i] = 0; vector<pair<int, int> > vpii; nl[i] = vpii; btdt[i] = 0; int a; cin >> a; ar[i] = make_pair(a, i); } vector<pair<int, int> > br = ar; sort(br.begin(), br.end()); unordered_map<int, int> nct; int cn = -1; int an = -1; for (int i = 0; i < n; i++) { if (br[i].first != an) { cn++; an = br[i].first; nct[an] = cn; } br[i].first = cn; } for (int i = 0; i < n; i++) { ar[i].first = nct[ar[i].first]; } for (int i = 0; i < n; i++) { if (br[i].first == ar[i].first) { continue; } s--; nl[br[i].first].push_back(make_pair(ar[i].first, br[i].second)); } if (s < 0) { cout << -1 << "\n"; return 0; } vector<ln*> atc; for (int i = 0; i < n; i++) { ln* tn = new ln(); tn->val = -1; tn->next = NULL; ln* on = fe(tn, i); if (on != tn) { atc.push_back(tn->next); } } int noc = atc.size(); int hmg = min(s, noc); vector<vector<int> > vas; if (hmg == 1) { hmg--; } if (hmg > 0) { vector<int> fv; vector<int> sv; for (int i = 0; i < hmg; i++) { fv.push_back(atc[i]->val); ln* cn = atc[i]; while (cn != NULL) { sv.push_back(cn->val); cn = cn->next; } } vas.push_back(fv); vas.push_back(sv); } for (int i = hmg; i < atc.size(); i++) { vector<int> sv; ln* cn = atc[i]; while (cn != NULL) { sv.push_back(cn->val); cn = cn->next; } vas.push_back(sv); } cout << vas.size() << "\n"; for (int i = 0; i < vas.size(); i++) { cout << vas[i].size() << "\n"; for (int j = 0; j < vas[i].size(); j++) { if (j > 0) { cout << " "; } cout << (vas[i][j] + 1); } cout << "\n"; } cin >> n; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, S; int a[N], p[N], q[N]; vector<vector<int>> cir; int uf[N]; int find(int x) { return (uf[x] == x) ? (x) : (uf[x] = find(uf[x])); } void adjust() { for (int i = 1; i <= n; i++) { uf[i] = i; } pair<int, int>* b = new pair<int, int>[(n + 1)]; for (int i = 1; i <= n; i++) b[i] = pair<int, int>(a[i], i); sort(b + 1, b + n + 1); for (int i = 1, j = 1; i <= n; i++, i = j) { while (j <= n && b[i].first == b[j].first) j++; for (int k = i, x; k < j; k++) { x = b[k].second; if (x >= i && x < j) { q[x] = x; } } for (int k = i, cur = i; k < j; k++) { if (!q[k]) { while (cur < j && q[b[cur].second] == b[cur].second) cur++; q[k] = b[cur++].second; } } } for (int i = 1; i <= n; i++) { p[q[i]] = i; } for (int i = 1; i <= n; i++) { uf[find(i)] = find(p[i]); } for (int i = 1, j = 1, ls; i <= n; i = j) { while (j <= n && b[i].first == b[j].first) j++; for (ls = i; ls < j && p[b[ls].second] == b[ls].second; ls++) ; for (int k = ls + 1; k < j; k++) { if (p[b[k].second] != b[k].second && find(k) != find(ls)) { uf[find(k)] = find(b[ls].second); uf[find(ls)] = find(b[k].second); swap(p[b[ls].second], p[b[k].second]); ls = k; } } } } bool vis[N]; int find_circle(int x) { if (p[x] == x) { return 0; } cir.push_back(vector<int>()); do { vis[x] = true; cir.back().push_back(x); x = p[x]; } while (!vis[x]); return cir.back().size(); } int main() { scanf("%d%d", &n, &S); for (int i = 1; i <= n; i++) { scanf("%d", a + i); } adjust(); int t = 0, m = 0; for (int i = 1, x; i <= n; i++) { if (!vis[i]) { x = find_circle(i); t += x; m += (x != 0); } } if (t > S) { puts("-1"); return 0; } if (m <= 2 || t + 2 >= S) { printf("%d\n", m); for (int i = 0; i < m; i++) { printf("%d\n", (signed)cir[i].size()); for (auto& e : cir[i]) { printf("%d ", e); } putchar('\n'); } } else { int c = min(m, S - t); printf("%d\n", 2 + m - c); int sum = 0; for (int i = 0; i < c; i++) sum += cir[i].size(); printf("%d\n", sum); for (int i = 0; i < c; i++) { for (auto& e : cir[i]) { printf("%d ", e); } } putchar('\n'); printf("%d\n%d", c, cir[0][0]); for (int i = c - 1; i; i--) printf(" %d", cir[i][0]); putchar('\n'); for (int i = c; i < m; i++) { printf("%d\n", (signed)cir[i].size()); for (auto& e : cir[i]) { printf("%d ", e); } putchar('\n'); } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using namespace chrono; const int infinity = (int)1e9 + 42; const int64_t llInfinity = (int64_t)1e18 + 256; const int module = (int)1e9 + 7; const long double eps = 1e-8; mt19937_64 randGen(system_clock().now().time_since_epoch().count()); inline void raiseError(string errorCode) { cerr << "Error : " << errorCode << endl; exit(42); } inline void test(istream &cin, ostream &cout) { int n, s; cin >> n >> s; vector<int> v(n); map<int, int> kompr; for (int i = 0; i < n; i++) { cin >> v[i]; kompr[v[i]] = 42; } int k = 0; for (auto &it : kompr) { it.second = k++; } for (int i = 0; i < n; i++) { v[i] = kompr[v[i]]; } auto w = v; sort(w.begin(), w.end()); vector<vector<int> > g(k); map<pair<int, int>, vector<int> > indices; for (int i = 0; i < n; i++) { if (w[i] == v[i]) { continue; } s--; g[w[i]].push_back(v[i]); indices[{w[i], v[i]}].push_back(i); } if (s < 0) { cout << -1 << "\n"; return; } vector<char> used(n, false); function<void(int)> pdfs = [&](int v) { if (used[v]) { return; } used[v] = true; for (int to : g[v]) { pdfs(to); } }; int comps = 0; for (int i = 0; i < k; i++) { if (!used[i]) { pdfs(i); if (!g[i].empty()) { comps++; } } } vector<int> pass; function<void(int)> dfs = [&](int v) { while (!g[v].empty()) { int to = g[v].back(); g[v].pop_back(); dfs(to); } pass.push_back(v); }; vector<vector<int> > ans; for (int i = 0; i < k; i++) { if (g[i].empty()) { continue; } pass.clear(); dfs(i); ans.emplace_back(); for (int j = 1; j < (int)pass.size(); j++) { auto &vec = indices[{pass[j], pass[j - 1]}]; assert(!vec.empty()); ans.back().push_back(vec.back()); vec.pop_back(); } reverse(ans.back().begin(), ans.back().end()); } assert((int)ans.size() == comps); cout << ans.size() << "\n"; for (auto vec : ans) { cout << vec.size() << "\n"; for (auto it : vec) { cout << it + 1 << " "; } cout << "\n"; } } signed main() { ios_base::sync_with_stdio(false); test(cin, cout); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 300000; map<int, int> mapa; vector<int> pos[MAXN]; vector<int> g[MAXN]; int ptr[MAXN]; int used[MAXN]; void euler(int v, vector<int> &res) { used[v] = true; for (; ptr[v] < (int)(g[v]).size();) { ++ptr[v]; euler(g[v][ptr[v] - 1], res); } if ((int)(pos[v]).size() > 0) { res.push_back(pos[v].back()); pos[v].pop_back(); } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n, s; cin >> n >> s; int k = 0; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> b = a; sort((b).begin(), (b).end()); int m = 0; for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { continue; } ++m; if (!mapa.count(b[i])) { mapa[b[i]] = k++; } } if (m > s) { cout << -1 << endl; return 0; } for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { continue; } a[i] = mapa[a[i]]; b[i] = mapa[b[i]]; g[b[i]].push_back(a[i]); pos[b[i]].push_back(i); } vector<vector<int>> cycles; for (int i = 0; i < k; ++i) { if (!used[i]) { cycles.push_back({}); euler(i, cycles.back()); } } vector<vector<int>> res; if (s - m > 1 && (int)(cycles).size() > 1) { int len = min((int)(cycles).size(), s - m); res.push_back({}); vector<int> newcycle; for (int i = (int)(cycles).size() - len; i < (int)(cycles).size(); ++i) { res.back().push_back(cycles[i].back()); for (int j : cycles[i]) { newcycle.push_back(j); } } for (int i = 0; i < len; ++i) { cycles.pop_back(); } cycles.push_back(newcycle); } for (int i = 0; i < (int)(cycles).size(); ++i) { res.push_back(cycles[i]); } cout << (int)(res).size() << endl; for (int i = 0; i < (int)(res).size(); ++i) { cout << (int)(res[i]).size() << endl; for (int j : res[i]) { cout << j + 1 << " "; } cout << endl; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int N, S; int a[252525]; int sorted[252525]; bool vis[252525]; bool numvis[252525]; bool cycvis[252525]; vector<vector<int> > cyc; int color[252525]; vector<vector<int> > conn; int num[252525]; vector<vector<int> > ans; vector<int> curr; void dfs(int cycno, int starting) { if (cycvis[cycno]) return; cycvis[cycno] = true; bool activate = false; for (auto x : cyc[cycno]) { if (x == starting) { activate = true; continue; } if (!activate) continue; curr.push_back(x); int n = num[x]; if (!numvis[n]) { numvis[n] = true; for (auto y : conn[n]) dfs(color[y], y); } } for (auto x : cyc[cycno]) { if (!activate) continue; if (x == starting) activate = false; curr.push_back(x); int n = num[x]; if (!numvis[n]) { numvis[n] = true; for (auto y : conn[n]) dfs(color[y], y); } } } void solve() { for (int i = 0; i < (int)cyc.size(); ++i) if (!cycvis[i]) { dfs(i, cyc[i][0]); ans.push_back(curr); curr.clear(); } printf("%d\n", (int)ans.size()); for (const auto& x : ans) { printf("%d\n", (int)x.size()); for (auto& y : x) printf("%d ", (int)y + 1); puts(""); } return; } int main() { scanf("%d%d", &N, &S); for (int i = 0; i < N; ++i) { int t; scanf("%d", &t); sorted[i] = a[i] = t; } sort(sorted, sorted + N); vector<pair<int, int> > nyu; vector<int> idx; vector<int> invidx(N); int p = -1; vector<int> cc; for (int i = 0; i < N; ++i) { if (a[i] != sorted[i]) { if (p != sorted[i]) { if (p != -1) conn.push_back(cc); cc.clear(); } p = sorted[i]; cc.push_back(i); num[i] = conn.size(); nyu.emplace_back(a[i], i); invidx[idx.size()] = i; idx.push_back(i); } } conn.push_back(cc); sort(nyu.begin(), nyu.end()); for (int i = 0; i < (int)nyu.size(); ++i) { int ind = idx[i]; vector<int> cycl; while (!vis[ind]) { vis[ind] = true; color[ind] = cyc.size(); cycl.push_back(ind); ind = nyu[invidx[ind]].second; } reverse(cycl.begin(), cycl.end()); if (!cycl.empty()) { cyc.push_back(cycl); S -= (int)cycl.size(); } } if (S < 0) { puts("-1"); return 0; } solve(); return 0; }