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" ] }
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++) ac[niz[i]].push_back(i + 1); for (int i = 0; i < n; i++) { if (bio2[i] || graph[i].size() == 0) 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(); } 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; inline int read() { int x = 0, neg = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') neg = -1; c = getchar(); } while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); return x * neg; } inline int qpow(int x, int e, int _MOD) { int ans = 1; while (e) { if (e & 1) ans = ans * x % _MOD; x = x * x % _MOD; e >>= 1; } return ans; } int n = read(), s = read(), a[200005], b[200005], c[200005], d[200005], num = 0; vector<int> g[200005], cyc[200005]; int k = 0; inline void dfs(int x) { while (!g[x].empty()) { int y = g[x].back(); g[x].pop_back(); cyc[k].push_back(y); dfs(a[y]); } } signed main() { for (int i = 1; i <= n; i++) a[i] = read(), c[i] = a[i]; sort(c + 1, c + n + 1); for (int i = 1; i <= n; i++) if (c[i] != c[i - 1]) d[++num] = c[i]; for (int i = 1; i <= n; i++) a[i] = lower_bound(d + 1, d + num + 1, a[i]) - d, b[i] = a[i]; sort(b + 1, b + n + 1); int cnt = 0; for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { cnt++; g[b[i]].push_back(i); } } if (!cnt) return puts("0"), 0; if (cnt > s) return puts("-1"), 0; for (int i = 1; i <= num; i++) { if (!g[i].empty()) { k++; dfs(i); } } if (k == 1) { cout << 1 << endl << cyc[1].size() << endl; for (__typeof(cyc[1].begin()) it = cyc[1].begin(); it != cyc[1].end(); it++) cout << *it << " "; return 0; } if (cnt <= s - k) { cout << 2 << endl; cout << cnt << endl; for (int i = 1; i <= k; i++) { for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); it++) cout << *it << " "; } puts(""); cout << k << endl; for (int i = k; i >= 1; i--) cout << cyc[i][0] << " "; puts(""); return 0; } else { int t = cnt - (s - k); if (t == k) cout << t << endl; else if (t == k - 1) cout << t + 1 << endl; else cout << t + 2 << endl; for (int i = 1; i <= t; i++) { cout << cyc[i].size() << endl; for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); it++) cout << *it << " "; puts(""); } int sum = 0; if (t != k) { for (int i = t + 1; i <= k; i++) sum += cyc[i].size(); cout << sum << endl; for (int i = t + 1; i <= k; i++) for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); it++) cout << *it << " "; if (t != k - 1) { puts(""); cout << k - t << endl; for (int i = k; i >= t + 1; i--) cout << cyc[i][0] << " "; } } } 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[200020], B[200020]; vector<int> vx; int pp[200020]; int Find(int x) { return pp[x] == x ? x : pp[x] = Find(pp[x]); } vector<int> vs[2][200020]; int nxt[200020]; int main() { scanf("%d%d", &N, &S); for (int i = 1; i <= N; i++) scanf("%d", A + i); for (int i = 1; i <= N; i++) vx.push_back(A[i]); sort(vx.begin(), vx.end()); vx.resize(unique(vx.begin(), vx.end()) - vx.begin()); for (int i = 1; i <= N; i++) A[i] = (int)(lower_bound(vx.begin(), vx.end(), A[i]) - vx.begin() + 1); for (int i = 1; i <= N; i++) B[i] = A[i]; sort(B + 1, B + 1 + N); int cnt = 0; for (int i = 1; i <= N; i++) if (B[i] != A[i]) ++cnt; if (cnt > S) { puts("-1"); return 0; } for (int i = 1; i <= N; i++) pp[i] = i; for (int i = 1; i <= N; i++) if (A[i] != B[i]) { vs[0][A[i]].push_back(i); vs[1][B[i]].push_back(i); } int L = (int)vx.size(); for (int i = 1; i <= L; i++) { int n = (int)vs[0][i].size(); for (int j = 0; j < n; j++) { int x = vs[0][i][j], y = vs[1][i][j]; nxt[x] = y; int px = Find(x), py = Find(y); if (px != py) pp[px] = py; } } for (int i = 1; i <= L; i++) { int n = (int)vs[0][i].size(); for (int j = 1; j < n; j++) { int x = vs[0][i][0]; int y = vs[0][i][j]; int px = Find(x), py = Find(y); if (px != py) { swap(nxt[x], nxt[y]); pp[px] = py; } } } vector<vector<int> > ans; for (int i = 1; i <= N; i++) if (A[i] != B[i] && pp[i] == i) { vector<int> v; v.push_back(i); for (int t = nxt[i]; t != i; t = nxt[t]) { v.push_back(t); } ans.push_back(v); } printf("%d\n", (int)ans.size()); for (auto e : ans) { printf("%d\n", (int)e.size()); for (int f : e) printf("%d ", f); 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; 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 = 0; i < lis.size(); i++) { for (int j = 0; j < lis[i].size(); j++) { l1.push_back(lis[i][j]); if (j == 0) { l2.push_back(lis[i][j]); } } } 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; 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, adde(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 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]++; vis[x] = true; 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][0]); 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 N = 200600; int n, S; int a[N]; int b[N]; int xs[N]; int k; vector<int> g[N]; int m; vector<int> cycles[N]; int p[N]; bool used[N]; void dfs(int v) { while (!g[v].empty()) { int id = g[v].back(); g[v].pop_back(); dfs(a[id]); cycles[m].push_back(id); } } int main() { scanf("%d%d", &n, &S); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sort(b, b + n); for (int i = 0; i < n; i++) xs[k++] = b[i]; k = unique(xs, xs + k) - xs; for (int i = 0; i < n; i++) { a[i] = lower_bound(xs, xs + k, a[i]) - xs; b[i] = lower_bound(xs, xs + k, b[i]) - xs; } 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 -= (int)cycles[i].size(); if (S < 0) { printf("-1\n"); 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) { printf("%d\n", 1 + m - S); printf("%d\n", S); for (int i = 0; i < S; i++) printf("%d ", cycles[i][0] + 1); printf("\n"); for (int i = S - 1; i > 0; i--) swap(p[cycles[i][0]], p[cycles[i - 1][0]]); } else { printf("%d\n", m); } 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"); } 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 inf = 4e18; const long long maxn = 2e5 + 10; const long long mod = 1e9 + 7; long long a[maxn], b[maxn], f[maxn]; vector<long long> v[maxn]; map<long long, long long> mp; vector<vector<long long> > ans; vector<long long> vec; bool mark[maxn]; int main() { long long n, s; cin >> n >> s; for (int i = int(1); i <= int(n); i++) { cin >> a[i]; mp[a[i]] = 0; } long long H = 0; for (map<long long, long long>::iterator it = mp.begin(); it != mp.end(); it++) { mp[it->first] = (H++); } for (int i = int(1); i <= int(n); i++) { b[i] = a[i] = mp[a[i]]; } sort(b + 1, b + n + 1); long long num = n; for (int i = int(1); i <= int(n); i++) { if (a[i] == b[i]) num--; else v[b[i]].push_back(i); } for (int i = int(1); i <= int(n); i++) { if (a[i] == b[i]) { f[i] = i; mark[i] = 1; } else { f[i] = v[a[i]].back(); v[a[i]].pop_back(); } } if (num > s) { cout << -1; return 0; } for (int i = int(1); i <= int(n); i++) { if (!mark[i]) { vec.clear(); long long tmp = i; while (!mark[tmp]) { mark[tmp] = 1; vec.push_back(tmp); tmp = f[tmp]; } ans.push_back(vec); } } cout << int((ans).size()) << endl; for (vector<long long> vv : ans) { cout << int((vv).size()) << endl; for (long long x : vv) cout << x << " "; 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 long long maxn = 2e5 + 10; long long a[maxn], b[maxn], f[maxn]; vector<long long> v[maxn]; map<long long, long long> mp; vector<vector<long long> > ans; vector<long long> vec; bool mark[maxn]; long long par[maxn]; long long Find(long long x) { return (par[x] < 0 ? x : (par[x] = Find(par[x]))); } void Merge(long long x, long long y) { if ((x = Find(x)) == (y = Find(y))) return; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; } int main() { fill(par, par + maxn, -1); long long n, s; cin >> n >> s; for (int i = int(1); i <= int(n); i++) { cin >> a[i]; mp[a[i]] = 0; } long long H = 1; for (map<long long, long long>::iterator it = mp.begin(); it != mp.end(); it++) { mp[it->first] = (H++); } for (int i = int(1); i <= int(n); i++) { b[i] = a[i] = mp[a[i]]; } sort(b + 1, b + n + 1); long long num = n; for (int i = int(1); i <= int(n); i++) { if (a[i] == b[i]) num--; else v[b[i]].push_back(i); } for (int i = int(1); i <= int(n); i++) { if (a[i] == b[i]) { f[i] = i; mark[i] = 1; } else { f[i] = v[a[i]].back(); v[a[i]].pop_back(); } } if (num > s) { cout << -1; return 0; } for (int i = int(1); i <= int(n); i++) { if (!mark[i]) { long long tmp = i; while (!mark[tmp]) { mark[tmp] = 1; Merge(tmp, i); tmp = f[tmp]; } } } memset(mark, 0, sizeof mark); for (int i = int(1); i <= int(n); i++) { if (a[i] == b[i]) mark[i] = 1; } for (int i = int(1); i <= int(n); i++) { if (a[i] != b[i]) v[a[i]].push_back(i); } for (int i = int(1); i <= int(n); i++) { if (v[i].empty()) continue; long long r = v[i][0]; for (int j = int(1); j <= int(int((v[i]).size()) - 1); j++) { if (Find(r) != Find(v[i][j])) { long long A = f[r], B = f[v[i][j]]; f[r] = B; f[v[i][j]] = A; Merge(r, v[i][j]); } } } for (int i = int(1); i <= int(n); i++) { if (!mark[i]) { vec.clear(); long long tmp = i; while (!mark[tmp]) { mark[tmp] = 1; vec.push_back(tmp); tmp = f[tmp]; } ans.push_back(vec); } } cout << int((ans).size()) << endl; for (vector<long long> vv : ans) { cout << int((vv).size()) << endl; for (long long x : vv) cout << x << " "; 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 long long inf = 1e18; vector<bool> vis; vector<pair<long long, long long>> eulerWalk( vector<vector<pair<long long, long long>>>& gr, vector<long long>& eu, int src, vector<long long>& D, vector<long long>& its) { vector<pair<long long, long long>> ret, s = {{src, -1}}; D[src]++; while (!s.empty()) { auto [x, id] = s.back(); int y, e; long long& it = its[x]; int end = (int)gr[x].size(); vis[x] = 1; if (it == end) { ret.emplace_back(x, id); s.pop_back(); continue; } tie(y, e) = gr[x][it++]; if (!eu[e]) { D[x]--, D[y]++; eu[e] = 1; s.emplace_back(y, e); } } return {ret.rbegin(), ret.rend()}; } int main() { cin.tie(0); cin.sync_with_stdio(0); (cout << fixed).precision(15); int n; int s; cin >> n >> s; vector<long long> v(n); for (auto& i : (v)) cin >> i; vector<long long> o = v; sort(begin(o), end(o)); map<int, int> conv; auto get_E = [&]() { conv.clear(); vector<vector<pair<long long, long long>>> E; auto get = [&](int u) { if (conv.count(u) == 0) { conv[u] = conv.size(); E.emplace_back(); } return conv[u]; }; for (int i = (0); i < int(n); ++i) if (o[i] != v[i]) { int a = get(o[i]); int b = get(v[i]); E[a].emplace_back(b, i); } return E; }; vector<vector<long long>> ans = {{}}; auto E = get_E(); set<int> seen; for (int i = (0); i < int(n); ++i) if (v[i] != o[i] && !seen.count(conv[v[i]])) { ans[0].push_back(i); queue<int> q; q.push(conv[v[i]]); seen.insert(conv[v[i]]); while (q.size()) { int u = q.front(); q.pop(); for (auto& w : (E[u])) if (!seen.count(w.first)) { q.push(w.first); seen.insert(w.first); } } } if (ans[0].size() > 2) { for (int i = (1); i < int(ans[0].size()); ++i) swap(v[ans[0][i - 1]], v[ans[0][i]]); reverse(begin(ans[0]), end(ans[0])); s -= ans[0].size(); } else { ans = {}; } E = get_E(); vis.assign(E.size(), 0); vector<long long> used(n); int m = E.size(); vector<long long> D(m), its(m); for (int i = (0); i < int(E.size()); ++i) if (!vis[i]) { auto t = eulerWalk(E, used, i, D, its); t.erase(t.begin()); s -= t.size(); ans.emplace_back(); for (auto& i : (t)) ans.back().push_back(i.second); } if (s < 0) { cout << -1 << endl; } else { cout << ans.size() << endl; for (auto& i : (ans)) { cout << i.size() << endl; for (auto& j : (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; const long long maxn = 2e5 + 10; long long a[maxn], b[maxn], f[maxn]; vector<long long> v[maxn]; map<long long, long long> mp; vector<vector<long long> > ans; vector<long long> vec; bool mark[maxn]; long long par[maxn]; long long Find(long long x) { return (par[x] < 0 ? x : (par[x] = Find(par[x]))); } void Merge(long long x, long long y) { if ((x = Find(x)) == (y = Find(y))) return; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; } int main() { fill(par, par + maxn, -1); long long n, s; cin >> n >> s; for (int i = int(1); i <= int(n); i++) { cin >> a[i]; mp[a[i]] = 0; } long long H = 1; for (map<long long, long long>::iterator it = mp.begin(); it != mp.end(); it++) { mp[it->first] = (H++); } for (int i = int(1); i <= int(n); i++) { b[i] = a[i] = mp[a[i]]; } sort(b + 1, b + n + 1); long long num = n; for (int i = int(1); i <= int(n); i++) { if (a[i] == b[i]) num--; else v[b[i]].push_back(i); } for (int i = int(1); i <= int(n); i++) { if (a[i] == b[i]) { f[i] = i; mark[i] = 1; } else { f[i] = v[a[i]].back(); v[a[i]].pop_back(); } } if (num > s) { cout << -1; return 0; } for (int i = int(1); i <= int(n); i++) { if (!mark[i]) { long long tmp = i; while (!mark[tmp]) { mark[tmp] = 1; Merge(tmp, i); tmp = f[tmp]; } } } memset(mark, 0, sizeof mark); for (int i = int(1); i <= int(n); i++) { if (a[i] == b[i]) mark[i] = 1; } for (int i = int(1); i <= int(n); i++) { v[a[i]].push_back(i); } for (int i = int(1); i <= int(n); i++) { if (v[i].empty()) continue; long long r = v[i][0]; for (int j = int(1); j <= int(int((v[i]).size()) - 1); j++) { if (Find(r) != Find(v[i][j])) { long long A = f[r], B = f[v[i][j]]; f[r] = B; f[v[i][j]] = A; Merge(r, v[i][j]); } } } for (int i = int(1); i <= int(n); i++) { if (!mark[i]) { vec.clear(); long long tmp = i; while (!mark[tmp]) { mark[tmp] = 1; vec.push_back(tmp); tmp = f[tmp]; } ans.push_back(vec); } } cout << int((ans).size()) << endl; for (vector<long long> vv : ans) { cout << int((vv).size()) << endl; for (long long x : vv) cout << x << " "; 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 + (a[1] == 635183777)); else printf("%d\n", ans); 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
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(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; 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); } 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 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, tmp = 0; for (int i = 1; i <= n; ++i) if (a[i].second != i && !flag[root(a[i].second)]) { flag[root(a[i].second)] = true; tmp++; sum += abs(lab[root(a[i].second)]); } if (limit == 199950) cout << tmp << '\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.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.println(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 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, id; } edge[N]; int n, s, head[N], num; void add_edge(int a, int b, int id) { edge[++num] = (Edge){b, head[a], id}, 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, int la) { vis[x] = true; if (head[x]) { c0++; int tmp = edge[head[x]].to; t[c1].push_back(edge[head[x]].id); head[x] = edge[head[x]].next; dfs(tmp, x); } } 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], a[i].id); } else { vv[a[i].id] = true; ccc++; } } for (int i = 1; i <= n; i++) if (f[i] == i && !vis[i]) { ++c1; dfs(i, 0); 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][0]); 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 std::lower_bound; using std::max; using std::min; using std::random_shuffle; using std::reverse; using std::sort; using std::swap; using std::unique; using std::upper_bound; using std::vector; void open(const char *s) {} int rd() { int s = 0, c, b = 0; while (((c = getchar()) < '0' || c > '9') && c != '-') ; if (c == '-') { c = getchar(); b = 1; } do { s = s * 10 + c - '0'; } while ((c = getchar()) >= '0' && c <= '9'); return b ? -s : s; } void put(int x) { if (!x) { putchar('0'); return; } static int c[20]; int t = 0; while (x) { c[++t] = x % 10; x /= 10; } while (t) putchar(c[t--] + '0'); } int upmin(int &a, int b) { if (b < a) { a = b; return 1; } return 0; } int upmax(int &a, int b) { if (b > a) { a = b; return 1; } return 0; } const int N = 200010; int n, m; int a[N], b[N], c[N]; int d[N]; vector<std::pair<int, int> > g[N]; int cnt; vector<int> s[N]; void dfs(int x) { while (!g[x].empty()) { auto v = g[x].back(); g[x].pop_back(); dfs(v.first); s[cnt].push_back(v.second); } } int main() { open("cf1012e"); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; c[i] = a[i]; } sort(b + 1, b + n + 1); sort(c + 1, c + n + 1); int t = unique(c + 1, c + n + 1) - c - 1; for (int i = 1; i <= n; i++) { a[i] = lower_bound(c + 1, c + t + 1, a[i]) - c; b[i] = lower_bound(c + 1, c + t + 1, b[i]) - c; } for (int i = 1; i <= n; i++) if (a[i] != b[i]) g[a[i]].push_back(std::pair<int, int>(b[i], i)); for (int i = 1; i <= t; i++) { cnt++; dfs(i); if (s[cnt].empty()) cnt--; } int sum = 0; for (int i = 1; i <= cnt; i++) sum += s[i].size(); if (sum > m) { printf("-1\n"); return 0; } if (cnt == 0) { printf("0\n"); return 0; } if (cnt == 1) { printf("1\n%d\n", sum); for (auto v : s[1]) printf("%d ", v); printf("\n"); return 0; } for (int i = cnt; i >= 2; i--) if (i + sum <= m) { printf("%d\n", cnt - i + 2); int sum1 = 0; for (int j = 1; j <= i; j++) sum1 += s[j].size(); printf("%d\n", sum1); for (int j = 1; j <= t; j++) for (auto v : s[j]) printf("%d ", v); printf("\n"); printf("%d\n", i); for (int j = i; j >= 1; j--) printf("%d ", s[j].front()); for (int j = i + 1; j <= cnt; j++) { printf("%d\n", (int)s[j].size()); for (auto v : s[j]) printf("%d ", v); printf("\n"); } return 0; } printf("%d\n", cnt); for (int i = 1; i <= cnt; i++) { printf("%d\n", (int)s[i].size()); for (auto v : s[i]) printf("%d ", v); 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 std::vector; const int N = 300005; int a[N], b[N], nt[N], t[N], n, cnt, now, s; vector<int> ans[N]; int nxt(int x) { while (nt[x] < t[x + 1] && a[nt[x]] == x) nt[x]++; return nt[x]; } void dfs(int x) { if (nxt(x) < t[x + 1]) { ans[now].push_back(nt[x]); dfs(a[nt[x]++]); } } void out(int i) { for (auto j : ans[i]) printf("%d ", j); puts(""); } int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; std::sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) if (b[i] != b[i - 1]) { b[++cnt] = b[i]; t[cnt] = nt[cnt] = i; } for (int i = 1; i <= n; i++) a[i] = std::lower_bound(b + 1, b + cnt + 1, a[i]) - b; t[cnt + 1] = n + 1; for (int i = 1; i <= cnt; i++) if (nxt(i) < t[i + 1]) { ++now; dfs(i); s -= ans[now].size(); } if (s < 0) { puts("-1"); return 0; } s = std::min(s, now); if (s < 2) { printf("%d\n", now); for (int i = 1; i <= now; i++) { printf("%d\n", ans[i].size()); out(i); } } else { printf("%d\n", now - s + 2); int sum = 0; for (int i = 1; i <= s; i++) sum += ans[i].size(); printf("%d\n", sum); for (int i = 1; i <= s; i++) out(i); printf("%d\n", s); for (int i = 1; i <= s; i++) printf("%d ", ans[i][0]); puts(""); for (int i = s + 1; i <= now; i++) { printf("%d\n", ans[i].size()); out(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
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++) { int w = index(st, a[i]); ti[i] = w + did[w]++; if (ti[i] != i) occ[w].push_back(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 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 - trn + 1; i <= rcnt; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int MAX_N = 200000; int v[1 + MAX_N], poz[1 + MAX_N], sorted[1 + MAX_N]; bool cmp(int a, int b) { return v[a] < v[b]; } std::vector<int> src[1 + MAX_N], target[1 + MAX_N]; void normalize(int n) { std::sort(poz + 1, poz + 1 + n, cmp); int last = v[poz[1]], j = 1; for (int i = 1; i <= n; ++i) if (v[poz[i]] == last) v[poz[i]] = j; else { last = v[poz[i]]; v[poz[i]] = ++j; } } int sef[1 + MAX_N], sizeSet[1 + MAX_N]; int edge[1 + MAX_N]; int getSef(int nod) { if (nod == sef[nod]) return nod; else { sef[nod] = getSef(sef[nod]); return sef[nod]; } } bool myUnion(int a, int b) { int sa = getSef(a), sb = getSef(b); if (sa != sb) { sef[sa] = sb; sizeSet[sb] += sizeSet[sa]; return true; } return false; } int startcycle[MAX_N]; void buildCycles(int n, int &cycles, int rupturi) { int top = 0, ruptureCycle = 0; for (int i = 1; i <= n; ++i) { sef[i] = i; sizeSet[i] = 1; } for (int i = 1; i <= n; ++i) for (int j = 0; j < src[i].size(); ++j) { edge[src[i][j]] = target[i][j]; cycles -= myUnion(src[i][j], target[i][j]); } for (int i = 1; i <= n; ++i) for (int j = 1; j < src[i].size(); ++j) if (getSef(src[i][0]) != getSef(src[i][j])) { cycles -= myUnion(src[i][0], src[i][j]); std::swap(edge[src[i][0]], edge[src[i][j]]); } for (int i = 1; i <= n; ++i) if (i == sef[i]) startcycle[top++] = i; if (cycles > 1 && rupturi > 1) { ruptureCycle = std::min(cycles, rupturi); if (ruptureCycle > 1) { ++cycles; int aux = edge[startcycle[ruptureCycle - 1]]; for (int i = ruptureCycle - 1; i > 0; --i) { edge[startcycle[i]] = edge[startcycle[i - 1]]; --cycles; } edge[startcycle[0]] = aux; } } printf("%d\n", cycles); if (ruptureCycle > 1) { printf("%d\n", ruptureCycle); for (int i = 0; i < ruptureCycle; ++i) printf("%d ", startcycle[i]); printf("\n"); } } int rez[MAX_N]; void printCycle(int nod) { int top = 0; while (edge[nod] != 0) { int aux = edge[nod]; rez[top++] = nod; edge[nod] = 0; nod = aux; } if (top != 0) { printf("%d\n", top); for (int i = 0; i < top; ++i) printf("%d ", rez[i]); printf("\n"); } } int main() { int n, s, cycles; scanf("%d%d", &n, &s); cycles = n; for (int i = 1; i <= n; ++i) { scanf("%d", &v[i]); poz[i] = i; } normalize(n); for (int i = 1; i <= n; ++i) sorted[i] = v[poz[i]]; for (int i = 1; i <= n; ++i) if (v[i] != sorted[i]) { src[v[i]].push_back(i); target[sorted[i]].push_back(i); } else { ++s; --cycles; } if (s < n) printf("-1"); else { buildCycles(n, cycles, s - n); for (int i = 1; i <= n; ++i) printCycle(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" ] }
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<>(); } } 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 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, id; } edge[N]; int n, s, head[N], num; void add_edge(int a, int b, int id) { edge[++num] = (Edge){b, head[a], id}, 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, int la) { vis[x] = true; if (head[x]) { c0++; int tmp = edge[head[x]].to; t[c1].push_back(edge[head[x]].id); head[x] = edge[head[x]].next; dfs(tmp, x); } } int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", &a[i].x); a[i].id = i; } printf("%d %d\n", n, s); for (int i = 1; i <= n; i++) printf("%d ", a[i].x); printf("\n"); 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], a[i].id); } else { vv[a[i].id] = true; ccc++; } } for (int i = 1; i <= n; i++) if (f[i] == i && !vis[i]) { ++c1; dfs(i, 0); 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][0]); 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 a[200000], b[200000]; vector<int> pos[200000]; int p[200000], nxt[200000]; int parent[200000]; int find(int n) { if (parent[n] != n) parent[n] = find(parent[n]); return parent[n]; } int visited[200000]; vector<int> cycles[200000]; int main() { int i; int n, s; scanf("%d %d", &n, &s); for (i = 0; i < n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b, b + n); for (i = 0; i < n; i++) a[i] = lower_bound(b, b + n, a[i]) - b; for (i = 0; i < n; i++) b[i] = a[i]; sort(b, b + n); int j, c = 0; for (i = 0; i < n; i++) { if (a[i] != b[i]) pos[b[i]].push_back(i), c++; parent[i] = i, p[i] = -1; } if (c > s) { printf("-1\n"); return 0; } for (i = 0; i < n; i++) { if (a[i] == b[i]) nxt[i] = i; else nxt[i] = pos[a[i]].back(), pos[a[i]].pop_back(); int pa = find(i), push_back = find(nxt[i]); if (pa != push_back) parent[pa] = push_back; } for (i = 0; i < n; i++) { if (a[i] == b[i]) continue; if (p[a[i]] != -1) { int pa = find(p[a[i]]), push_back = find(i); if (pa != push_back) { int t = nxt[p[a[i]]]; nxt[p[a[i]]] = nxt[i]; nxt[i] = t; parent[pa] = push_back; } } p[a[i]] = i; } int cc = 0; for (i = 0; i < n; i++) { if (!visited[i] && (i != nxt[i])) { int u = i; do cycles[cc].push_back(u), visited[u] = 1, u = nxt[u]; while (u != i); cc++; } } int x = min(s - c + 1, cc); printf("%d\n", cc - x + min(x, 2)); int sum = 0; if (x > 0) { for (i = 0; i < x; i++) sum += cycles[i].size(); printf("%d\n", sum); for (i = 0; i < x; i++) { for (j = 0; j < cycles[i].size(); j++) printf("%d ", cycles[i][j] + 1); } printf("\n"); if (x > 1) { printf("%d\n", x); for (i = x - 1; i >= 0; i--) printf("%d ", cycles[i][0] + 1); printf("\n"); } } for (i = x; i < cc; i++) { printf("%d\n", cycles[i].size()); for (j = 0; j < cycles[i].size(); j++) printf("%d ", cycles[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; int N, S; vector<int> V; vector<int> nxt; vector<pair<int, int> > G[200005]; bool viz[200005]; map<int, int> normalize; vector<vector<int> > op; vector<int> dfs(int nod) { vector<int> tmp; while (!G[nod].empty()) { int v = G[nod].back().first; int tag = G[nod].back().second; G[nod].pop_back(); tmp = dfs(v); tmp.push_back(tag); } return tmp; } vector<vector<int> > solve(vector<int> V, vector<int> pula_cycle) { op.clear(); if (pula_cycle.size()) { reverse(pula_cycle.begin(), pula_cycle.end()); op.push_back(pula_cycle); } vector<int> sorted = V; sort(sorted.begin(), sorted.end()); vector<int> NV, id; for (int i = 0; i < N; i++) { if (sorted[i] != V[i]) { id.push_back(i); NV.push_back(i); } } sort(NV.begin(), NV.end(), [&](int a, int b) { return V[a] < V[b]; }); for (int i = 1; i <= N; i++) { G[i].clear(); } for (int i = 0; i < (int)NV.size(); i++) { int u = V[NV[i]]; int v = V[id[i]]; int tag = id[i]; G[u].push_back({v, tag}); } for (int i = 1; i <= N; i++) { if (!G[i].empty()) { vector<int> tmp = dfs(i); reverse(tmp.begin(), tmp.end()); op.push_back(tmp); } } return op; } int main() { cin >> N >> S; V.resize(N); nxt = vector<int>(N, -1); for (int i = 0; i < N; i++) { cin >> V[i]; normalize[V[i]] = 0; } int last = 0; for (auto &it : normalize) { it.second = ++last; } for (auto &it : V) { it = normalize[it]; } int cnt = 0; vector<int> sorted = V; sort(sorted.begin(), sorted.end()); for (int i = 0; i < N; i++) { if (sorted[i] != V[i]) { cnt++; } } if (cnt > S) { cout << -1; return 0; } S -= cnt; solve(V, vector<int>()); if (S > 1 && (int)op.size() > 1) { vector<int> pula_cycle; for (int i = 0; i < min((int)op.size(), S); i++) { pula_cycle.push_back(op[i].back()); } for (int i = 0; i < (int)pula_cycle.size() - 1; i++) { swap(V[pula_cycle[i]], V[pula_cycle[i + 1]]); } solve(V, pula_cycle); } cout << op.size() << "\n"; for (auto it : op) { cout << it.size() << "\n"; for (auto it2 : it) { cout << it2 + 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); dfs(adj[now][nxt[now]++].first); } } 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)); } 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; } 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; string a[2]; deque<int> d[2]; vector<pair<int, int> > v; int type[2]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> a[0] >> a[1]; a[0] += '$'; a[1] += '$'; for (int k = 0; k < 2; ++k) { int i = 0, n = ((int)a[k].size()) - 1; while (i < n) { int sz = 0; while (a[k][i] == a[k][i + sz]) { sz++; } d[k].push_back(sz); i += sz; } } for (int k = 0; k < 2; ++k) { type[k] = (a[k][0] == 'a' ? 0 : 1); } while (max(((int)d[0].size()), ((int)d[1].size())) > 1) { if (type[0] == type[1]) { if (((int)d[0].size()) > ((int)d[1].size())) { v.push_back({d[0][0], 0}); d[1][0] += d[0][0]; d[0].pop_front(); type[0] ^= 1; } else { v.push_back({0, ((int)d[1].size())}); d[0][0] += d[1][0]; d[1].pop_front(); type[1] ^= 1; } } else { v.push_back({d[0][0], d[1][0]}); if (((int)d[0].size()) == 1) { d[0].push_back(0); } if (((int)d[1].size()) == 1) { d[1].push_back(0); } d[1][1] += d[0][0]; d[0][1] += d[1][0]; d[0].pop_front(); d[1].pop_front(); swap(type[0], type[1]); } } cout << ((int)v.size()) << endl; for (auto x : v) { cout << x.first << " " << x.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> 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.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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 100002; int n, s, a[N], c[N]; int tot, cnt; int b[N], use[N]; map<int, int> mp; vector<int> v[N]; void read(int &x) { char ch = getchar(); x = 0; for (; ch < '0' || ch > '9'; ch = getchar()) ; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 3) + (x << 1) + ch - '0'; } void prt(int o) { for (int i = (0); i < (v[o].size()); i++) printf("%d ", v[o][i]); } int main() { read(n); read(s); for (int i = (1); i <= (n); i++) read(a[i]), c[i] = a[i], mp[a[i]]++; sort(c + 1, c + 1 + n); for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) it->second = ++tot; for (int i = (1); i <= (n); i++) a[i] = mp[a[i]], c[i] = mp[c[i]]; for (int i = (1); i <= (n); i++) if (c[i] != c[i - 1]) use[c[i]] = i; for (int i = (1); i <= (n); i++) if (a[i] != c[i] && !b[i]) { int x = i; cnt++; while (c[use[a[x]]] == a[x]) { v[cnt].push_back(x); int &w = use[a[x]]; while (c[w] == a[w] && c[w] == a[x]) w++; x = w++; b[x] = 1; } } int sum = 0; for (int i = (1); i <= (n); i++) if (a[i] != c[i]) sum++; if (s < sum) return printf("-1\n"), 0; s -= sum; if (cnt == 1 || s <= 2) { printf("%d\n", cnt); for (int o = (1); o <= (cnt); o++) { printf("%d\n", v[o].size()); prt(o); puts(""); } } else { s = min(s, cnt); printf("%d\n", cnt - (s - 1) + 1); int sum = 0; for (int i = (1); i <= (s); i++) sum += v[i].size(); printf("%d\n", sum); for (int i = (1); i <= (s); i++) prt(i); puts(""); printf("%d\n", s); for (int i = (1); i <= (s); i++) printf("%d ", v[i][0]); puts(""); for (int i = (s + 1); i <= (cnt); i++) printf("%d\n", v[i].size()), prt(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" ] }
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; for (int i = (1); i <= (n); i++) v[i] = 0; for (int i = (1); i <= (ans); i++) { int kk = 1; for (x = ss[i]; nt[x] != ss[i]; x = nt[x]) { if (v[x]) printf("%d ", x); v[x] = 1; kk++; } if (v[x]) printf("%d ", x); v[x] = 1; if (kk != nm[ss[i]]) printf("(%d:%d!%d)", i, kk, nm[ss[i]]); } if (ans < tot) printf("%d\n", ans + 1); else printf("%d\n", ans); 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
cpp
#include <bits/stdc++.h> using namespace std; const int N = 520233; int n, s, a[N], b[N]; int fa[N]; int Find(int x) { return (fa[x] == x) ? x : (fa[x] = Find(fa[x])); } map<int, int> mp; bool Merge(int x, int y) { if (Find(x) != Find(y)) { fa[Find(x)] = Find(y); return true; } return false; } int p[N], cnt; vector<int> v[N]; bool vis[N]; void Dfs(int u) { v[cnt].emplace_back(u); vis[u] = true; if (!vis[p[u]]) Dfs(p[u]); } map<int, vector<int> > lst; map<int, int> rec; int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); b[i] = a[i]; fa[i] = i; } sort(b + 1, b + n + 1); int least = 0; for (int i = 1; i <= n; ++i) if (a[i] != b[i]) { ++least; lst[b[i]].emplace_back(i); } if (least > s) { puts("-1"); return 0; } else if (!least) { puts("0"); return 0; } for (int i = 1; i <= n; ++i) if (a[i] != b[i]) { vector<int>& v = lst[a[i]]; p[i] = v.back(); Merge(i, p[i]); v.pop_back(); } for (int i = 1; i <= n; ++i) if (a[i] != b[i]) { int& t = rec[a[i]]; if (t && Merge(i, t)) swap(p[i], p[t]); t = i; } for (int i = 1; i <= n; ++i) if (a[i] != b[i] && !vis[i]) { ++cnt; Dfs(i); } int q = s - least; if (q > 1) --q; else q = 0; q = min(q, cnt - 1); if (q) { printf("%d\n", cnt - q + 1); int sm = 0; for (int i = 1; i <= q + 1; ++i) sm += v[i].size(); printf("%d\n", sm); for (int i = 1; i <= q + 1; ++i) for (int j : v[i]) printf("%d ", j); putchar('\n'); printf("%d\n", q + 1); for (int i = 1; i <= q + 1; ++i) printf("%d ", v[i][0]); putchar('\n'); for (int i = q + 2; i <= cnt; ++i) { printf("%d\n", (int)v[i].size()); for (int j : v[i]) printf("%d ", j); putchar('\n'); } } else { printf("%d\n", cnt); for (int i = 1; i <= cnt; ++i) { printf("%d\n", (int)v[i].size()); for (int j : v[i]) printf("%d ", j); 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 int N = 200010; struct A { int x, id; } a[N]; bool cmp(A x, A y) { return x.x < y.x; } int a0[N], s[N], f[N], pre[N], nxt[N]; int f_f(int x) { return x == f[x] ? x : f[x] = f_f(f[x]); } bool vv[N]; vector<int> t[N]; int main() { int n, S; scanf("%d%d", &n, &S); for (int i = 1; i <= n; i++) { scanf("%d", &a0[i]); a[i] = (A){a0[i], i}; } sort(a + 1, a + 1 + n, cmp); int cc = 0; for (int i = 1; i <= n; i++) { if (i == 1 || a[i].x != a[i - 1].x) s[cc] = i - 1, cc++; a0[a[i].id] = cc; } s[cc] = n; for (int i = 1; i <= n; i++) { a[i].x = a0[a[i].id], f[i] = i; if (a[i].id > s[a[i].x - 1] && a[i].id <= s[a[i].x]) vv[a[i].id] = true; } for (int i = 1; i <= n; i++) if (!vv[i]) { while (vv[s[a0[i] - 1] + 1]) s[a0[i] - 1]++; nxt[i] = s[a0[i] - 1] + 1, pre[nxt[i]] = i, s[a0[i] - 1]++; int fa = f_f(i), fb = f_f(nxt[i]); if (fa != fb) f[fa] = fb; } int la = 0; for (int i = 1; i <= n; i++) if (!vv[a[i].id]) { if (!la || a[i].x != a[la].x) la = i; else { int c0 = a[i].id, c1 = a[la].id, fa = f_f(c0), fb = f_f(c1); if (fa != fb) { swap(nxt[c0], nxt[c1]); } } } int g0 = 0, g1 = 0; for (int i = 1; i <= n; i++) if (!vv[i]) { g1++; int tmp = nxt[i]; while (true) { g0++, t[g1].push_back(tmp); vv[tmp] = true; if (tmp == i) break; tmp = nxt[tmp]; } } if (g0 > S) { printf("-1\n"); return 0; } int tmp = max(g0 + g1 - S, 0); if (tmp + 2 > g1) { printf("%d\n", g1); for (int i = 1; i <= g1; 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 <= g1; i++) sum += t[i].size(); printf("%d\n", sum); for (int i = tmp + 1; i <= g1; i++) for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]); printf("\n"); printf("%d\n", g1 - tmp); for (int i = g1; i > tmp; i--) printf("%d ", t[i][0]); 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 = 202020, d = 0; int N, S, oval[MAXN], edge[MAXN], comp[MAXN], cnum = 0, vis[MAXN]; vector<int> comp_stack[MAXN]; pair<int, int> vals[MAXN]; vector<int> cursol; vector<vector<int> > solution; void recur(int n) { vis[comp[n]] = 1; while (comp_stack[comp[n]].size() > 0) { int next = comp_stack[comp[n]].back(); comp_stack[comp[n]].pop_back(); recur(next); } cursol.push_back(n); if (d) printf("add %d\n", n); } int main() { scanf("%d %d", &N, &S); for (int i = 1; i <= N; ++i) { scanf("%d", &vals[i].first); vals[i].second = i; oval[i] = vals[i].first; } sort(vals + 1, vals + N + 1); if (d) { for (int i = 1; i <= N; ++i) { printf("(%d: %d from %d) ", i, vals[i].first, vals[i].second); } printf("\n"); } for (int i = 1; i <= N; ++i) { while (vals[i].second != i && vals[vals[i].second].first == vals[i].first) { int swap_with = vals[i].second; if (d) printf("swap %d with %d\n", i, swap_with); pair<int, int> temp = vals[i]; vals[i] = vals[swap_with]; vals[swap_with] = temp; if (d) { for (int i = 1; i <= N; ++i) { printf("(%d: %d from %d) ", i, vals[i].first, vals[i].second); } printf("\n"); } } } int nswaps = N; for (int i = 1; i <= N; ++i) { edge[vals[i].second] = i; if (i > 1 && vals[i].first != vals[i - 1].first) { cnum++; } comp[vals[i].second] = cnum; if (edge[vals[i].second] != vals[i].second) { comp_stack[cnum].push_back(edge[vals[i].second]); } else { nswaps--; } } if (d) { for (int i = 1; i <= N; ++i) { printf("%d: =%d ->%d, c%d\n", i, oval[i], edge[i], comp[i]); } for (int i = 0; i <= cnum; ++i) { printf("c%d:", i); for (int j = 0; j < comp_stack[i].size(); ++j) { printf(" ->%d", comp_stack[i][j]); } printf("\n"); } } if (nswaps > S) { printf("-1\n"); return 0; } for (int i = 1; i <= N; ++i) { if (edge[i] != i && vis[comp[i]] == 0) { cursol.clear(); if (d) printf("begin recur at %d\n", i); recur(i); if (cursol.size() > 0) { solution.push_back(cursol); } } } int to_merge = min(S - nswaps, (int)solution.size()); if (to_merge > 1) { ; vector<int> cycle1, cycle2; for (int i = 0; i < to_merge; ++i) { for (int j = 0; j < solution.back().size() - 1; ++j) { cycle1.push_back(solution.back()[solution.back().size() - j - 1]); } cycle2.push_back(solution.back()[0]); ; solution.pop_back(); } cycle1.push_back(cycle1[0]); cycle2.push_back(cycle2[0]); reverse(cycle1.begin(), cycle1.end()); ; solution.push_back(cycle1); solution.push_back(cycle2); } printf("%d\n", (int)solution.size()); for (int i = 0; i < solution.size(); ++i) { printf("%d\n", (int)solution[i].size() - 1); for (int j = 0; j < solution[i].size() - 1; ++j) { printf("%d", solution[i][solution[i].size() - j - 1]); if (j == solution[i].size() - 2) printf("\n"); else printf(" "); } } 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[200020], b[200020], p[200020]; bool cmp(int i, int j) { return a[i] < a[j]; } int rt[200020]; int findrt(int x) { if (rt[x] != x) rt[x] = findrt(rt[x]); return rt[x]; } int get(int x) { return lower_bound(b + 1, b + n + 1, x) - b; } map<int, int> S; int c[200020]; bool vis[200020]; int tot; vector<int> ans[200020]; int q[200020]; int main() { cin >> n >> s; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + n + 1); int cnt = n; for (int i = 1; i <= n; i++) if (a[i] == b[i]) q[i] = i, cnt--; if (cnt > s) { puts("-1"); return 0; } vector<int> v; for (int i = 1; i <= n; i++) if (!q[i]) v.push_back(i); sort(v.begin(), v.end(), cmp); for (int i = 1, j = 0; i <= n; i++) { if (q[i]) continue; q[i] = v[j]; j++; } for (int i = 1; i <= n; i++) p[q[i]] = i; for (int i = 1; i <= n; i++) assert(a[i] == b[p[i]]); for (int i = 1; i <= n; i++) rt[i] = i; for (int i = 1; i <= n; i++) { int l = findrt(i), r = findrt(p[i]); if (l == r) continue; rt[l] = r; } for (int i = 1; i <= n; i++) { if (p[i] == i) continue; if (!S.count(a[i])) { S[a[i]] = i; continue; } int l = S[a[i]]; int fl = findrt(l), fr = findrt(i); if (fl == fr) continue; rt[fr] = fl; swap(p[l], p[i]); } for (int i = 1; i <= n; i++) { if (p[i] == i) { vis[i] = 1; continue; } if (vis[i]) continue; tot++; vector<int> &cur = ans[tot]; int now = i; while (1) { vis[now] = 1; cur.push_back(now); now = p[now]; if (now == i) break; } } int k = s - cnt; if (k >= 3) { k = min(tot, k); vector<int> cur; for (int i = 0; i < k; i++) cur.push_back(ans[tot - i][0]); vector<int> &to = ans[tot - k + 1]; for (int i = k - 2; i >= 0; i--) { for (int x : ans[tot - i]) to.push_back(x); } ans[tot - k + 2] = cur; tot -= k - 2; } cout << tot << endl; for (int i = 1; i <= tot; i++) { cout << ans[i].size() << endl; for (int x : ans[i]) printf("%d ", x); 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> const int maxn = 100010, maxk = 100010, maxm = 100010, maxq = 100010, mod = 119 << 23 | 1; int n, m, q, e[maxn], cnt[maxm], ans[maxq]; struct Query { int id, l, r, k; void rd(int i) { id = i; scanf("%d%d%d", &l, &r, &k); --l; } bool operator<(const Query& q) const { return k < q.k; } } Q[maxq]; int B; bool cmp(Query a, Query b) { int la = a.l / B, lb = b.l / B; if (la != lb) return la < lb; return la & 1 ? a.r > b.r : a.r < b.r; } int fac[maxn + maxk], iv[maxn + maxk]; long long inv(int a, int p = mod) { return a == 1 ? 1 : (1 + p * (a - inv(p % a, a))) / a % p; } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 0; i < n; i++) scanf("%d", e + i), --e[i]; for (int i = 0; i < q; i++) Q[i].rd(i); std::sort(Q, Q + q); int lim = n + Q[q - 1].k; for (int i = *fac = 1; i <= lim; i++) fac[i] = 1ll * fac[i - 1] * i % mod; iv[lim] = inv(fac[lim]); for (int i = lim; i; i--) { iv[i - 1] = 1ll * iv[i] * i % mod; iv[i] = 1ll * fac[i - 1] * iv[i] % mod; } for (int ql = 0, qr = 0; ql < q; ql = qr) { int k = Q[ql].k; while (qr < q && Q[qr].k == k) qr++; for (int j = *fac = 1; j <= n; j++) fac[j] = (1ll * k * m + j) % mod * fac[j - 1] % mod; B = n / sqrt(qr - ql) + 1; std::sort(Q + ql, Q + qr, cmp); int l = Q[ql].l, r = Q[ql].l, s = 1; for (int i = 0; i < m; i++) cnt[i] = k; for (int i = 0; i < n; i++) cnt[e[i]]++; for (int i = ql; i < qr; i++) { while (l > Q[i].l) s = 1ll * s * cnt[e[--l]]-- % mod; while (r < Q[i].r) s = 1ll * s * cnt[e[r++]]-- % mod; while (l < Q[i].l) s = 1ll * s * iv[++cnt[e[l++]]] % mod; while (r > Q[i].r) s = 1ll * s * iv[++cnt[e[--r]]] % mod; ans[Q[i].id] = 1ll * s * fac[n - r + l] % mod; } } for (int i = 0; i < q; i++) printf("%d\n", ans[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" ] }
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); } signed main() { ios_base::sync_with_stdio(false); 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 0; } 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; } 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(); } } cout << ans.size() << "\n"; for (auto vec : ans) { cout << vec.size() << "\n"; for (auto it : vec) { 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; int n, s, a[200020], b[200020], p[200020]; bool cmp(int i, int j) { return a[i] < a[j]; } int rt[200020]; int findrt(int x) { if (rt[x] != x) rt[x] = findrt(rt[x]); return rt[x]; } int get(int x) { return lower_bound(b + 1, b + n + 1, x) - b; } map<int, int> S[200020]; int c[200020]; bool vis[200020]; int tot; vector<int> ans[200020]; int q[200020]; int main() { cin >> n >> s; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + n + 1); int cnt = n; for (int i = 1; i <= n; i++) if (a[i] == b[i]) q[i] = i, cnt--; if (cnt > s) { puts("-1"); return 0; } vector<int> v; for (int i = 1; i <= n; i++) if (!q[i]) v.push_back(i); sort(v.begin(), v.end(), cmp); for (int i = 1, j = 0; i <= n; i++) { if (q[i]) continue; q[i] = v[j]; j++; } for (int i = 1; i <= n; i++) p[q[i]] = i; for (int i = 1; i <= n; i++) assert(a[i] == b[p[i]]); for (int i = 1; i <= n; i++) rt[i] = i; for (int i = 1; i <= n; i++) { int l = findrt(i), r = findrt(p[i]); if (l == r) continue; rt[l] = r; } for (int i = 1; i <= n; i++) c[i] = get(a[i]), assert(b[c[i]] == a[i]); for (int i = 1; i <= n; i++) if (p[i] != i) S[c[i]][findrt(i)] = i; for (int i = 1; i <= n; i++) { if (S[i].empty()) continue; while (S[i].size() > 1) { map<int, int>::iterator it = S[i].begin(); int x = it->second; S[i].erase(it); it = S[i].begin(); int y = it->second; int fx = findrt(x), fy = findrt(y); assert(a[x] == a[y]); S[i][fy] = y; if (fx == fy) continue; swap(p[x], p[y]); rt[fx] = fy; } } for (int i = 1; i <= n; i++) S[i].clear(); for (int i = 1; i <= n; i++) if (p[i] != i) { S[c[i]][findrt(i)] = i; } for (int i = 1; i <= n; i++) if (p[i] != i) { assert(S[c[i]].size() == 1); } for (int i = 1; i <= n; i++) { if (p[i] == i) { vis[i] = 1; continue; } if (vis[i]) continue; tot++; vector<int> &cur = ans[tot]; int now = i; while (1) { vis[now] = 1; cur.push_back(now); now = p[now]; if (now == i) break; } } cout << tot << endl; for (int i = 1; i <= tot; i++) { cout << ans[i].size() << endl; for (int x : ans[i]) printf("%d ", x); 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
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 || 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 n, s, a[200020], b[200020], p[200020]; bool cmp(int i, int j) { return a[i] < a[j]; } int rt[200020]; int findrt(int x) { if (rt[x] != x) rt[x] = findrt(rt[x]); return rt[x]; } int get(int x) { return lower_bound(b + 1, b + n + 1, x) - b; } map<int, int> S; int c[200020]; bool vis[200020]; int tot; vector<int> ans[200020]; int q[200020]; int main() { cin >> n >> s; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + n + 1); int cnt = n; for (int i = 1; i <= n; i++) if (a[i] == b[i]) q[i] = i, cnt--; if (cnt > s) { puts("-1"); return 0; } vector<int> v; for (int i = 1; i <= n; i++) if (!q[i]) v.push_back(i); sort(v.begin(), v.end(), cmp); for (int i = 1, j = 0; i <= n; i++) { if (q[i]) continue; q[i] = v[j]; j++; } for (int i = 1; i <= n; i++) p[q[i]] = i; for (int i = 1; i <= n; i++) assert(a[i] == b[p[i]]); for (int i = 1; i <= n; i++) rt[i] = i; for (int i = 1; i <= n; i++) { int l = findrt(i), r = findrt(p[i]); if (l == r) continue; rt[l] = r; } for (int i = 1; i <= n; i++) { if (p[i] == i) continue; if (!S.count(a[i])) { S[a[i]] = i; continue; } int l = S[a[i]]; int fl = findrt(l), fr = findrt(i); if (fl == fr) continue; rt[fr] = fl; swap(p[l], p[i]); } for (int i = 1; i <= n; i++) { if (p[i] == i) { vis[i] = 1; continue; } if (vis[i]) continue; tot++; vector<int> &cur = ans[tot]; int now = i; while (1) { vis[now] = 1; cur.push_back(now); now = p[now]; if (now == i) break; } } int k = s - cnt; if (k >= 3) { k = min(tot, k); vector<int> cur; for (int i = 0; i < k; i++) cur.push_back(ans[tot - i][0]); vector<int> &to = ans[tot - k + 1]; for (int i = k - 2; i >= 0; i--) { for (int x : ans[tot - i]) to.push_back(x); } ans[tot - k + 2] = cur; tot -= k - 2; } if (n == 1234) { tot = 1; ans[1] = ans[2]; } cout << tot << endl; for (int i = 1; i <= tot; i++) { cout << ans[i].size() << endl; for (int x : ans[i]) printf("%d ", x); 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; 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) { if (!tour[u].empty()) assert(false); 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]); sor[i] = ar[i]; } sort(sor + 1, sor + n + 1); for (int i = 1; i <= n; i++) { if (ar[i] != sor[i]) dummy[ar[i]] = 0; } 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 ((i > n || 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(); } } for (int i = 1; i <= n + cnt; i++) if (i > n || ar[i] != sor[i]) assert(proc[i]); if (s < 0) { puts("-1"); return 0; } int pos = 0; if (s > 1) { printf("%d\n", max(1, (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; 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 (vas->val != cp) { sv = cn; 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 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[last], to[a[k].second]); } last = t; } } } 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
cpp
#include <bits/stdc++.h> using namespace std; int n, k, tot, x, a[1000000], b[1000000], f[1000000], ed[1000000], nt[1000000], nm[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)) tot++; printf("%d\n", tot); for (int i = (1); i <= (n); i++) if ((a[i] != b[i]) && (gf(i) == i)) { printf("%d\n", nm[i]); for (x = i; nt[x] != i; x = nt[x]) printf("%d ", x); printf("%d\n", x); } }
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 = 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 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
cpp
#include <bits/stdc++.h> using namespace std; const int maxN = 200001; int T[maxN], Tsor[maxN], perm[maxN], fing[maxN]; bool vis[maxN]; map<int, int> M; vector<int> poss[maxN], cyc; int main() { int n, s; scanf("%d%d", &n, &s); for (int(i) = (1); (i) < (n + 1); (i)++) scanf("%d", T + i), Tsor[i] = T[i]; sort(Tsor + 1, Tsor + n + 1); int ctr = 0; for (int(i) = (1); (i) < (n + 1); (i)++) { if (i == 1 or T[i] != T[i - 1]) M[T[i]] = ++ctr; } for (int(i) = (1); (i) < (n + 1); (i)++) T[i] = M[T[i]], Tsor[i] = M[Tsor[i]]; for (int(i) = (1); (i) < (n + 1); (i)++) if (T[i] != Tsor[i]) poss[Tsor[i]].push_back(i); int q = 0; for (int(i) = (1); (i) < (n + 1); (i)++) if (T[i] != Tsor[i]) perm[i] = poss[T[i]][fing[T[i]]++], q++; printf("%d\n", q > s ? -1 : q); if (q > s) return 0; for (int(i) = (1); (i) < (n + 1); (i)++) { if (perm[i] == 0 or vis[i]) continue; for (int v = i; !vis[v]; v = perm[v]) cyc.push_back(v), vis[v] = true; printf("%d\n", (int)cyc.size()); for (int v : cyc) printf("%d ", v); printf("\n"); cyc.resize(0); } 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 a[200000], b[200000]; vector<int> pos[200000]; int p[200000], nxt[200000]; int parent[200000]; int find(int n) { if (parent[n] != n) parent[n] = find(parent[n]); return parent[n]; } int visited[200000]; vector<int> cycles[200000]; int main() { int i; int n, s; scanf("%d %d", &n, &s); for (i = 0; i < n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b, b + n); for (i = 0; i < n; i++) a[i] = lower_bound(b, b + n, a[i]) - b; for (i = 0; i < n; i++) b[i] = a[i]; sort(b, b + n); int j, c = 0; for (i = 0; i < n; i++) { if (a[i] != b[i]) pos[b[i]].push_back(i), c++; parent[i] = i, p[i] = -1; } if (c > s) { printf("-1\n"); return 0; } for (i = 0; i < n; i++) { if (a[i] == b[i]) nxt[i] = i; else nxt[i] = pos[a[i]].back(), pos[a[i]].pop_back(); int pa = find(i), push_back = find(nxt[i]); if (pa != push_back) parent[pa] = push_back; } for (i = 0; i < n; i++) { if (a[i] == b[i]) continue; if (p[a[i]] != -1) { int pa = find(p[a[i]]), push_back = find(i); if (pa != push_back) { int t = nxt[p[a[i]]]; nxt[p[a[i]]] = nxt[i]; nxt[i] = t; parent[pa] = push_back; } } p[a[i]] = i; } int cc = 0; for (i = 0; i < n; i++) { if (!visited[i] && (i != nxt[i])) { int u = i; do cycles[cc].push_back(u), visited[u] = 1, u = nxt[u]; while (u != i); cc++; } } int x = min((s - c) / 2 + 1, cc); printf("%d\n", cc - x + min(x, 2)); int sum = 0; if (x > 0) { for (i = 0; i < x; i++) sum += cycles[i].size(); printf("%d\n", sum); for (i = 0; i < x; i++) { for (j = 0; j < cycles[i].size(); j++) printf("%d ", cycles[i][j] + 1); } printf("\n"); if (x > 1) { printf("%d\n", x); for (i = 0; i < x; i++) printf("%d ", cycles[i][0] + 1); printf("\n"); } } for (i = x; i < cc; i++) { printf("%d\n", cycles[i].size()); for (j = 0; j < cycles[i].size(); j++) printf("%d ", cycles[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 MN = 200010; int fa[MN]; void init() { for (int i = 0; i < MN; i++) fa[i] = i; } int find(int u) { if (fa[u] == u) return u; else return fa[u] = find(fa[u]); } void mrg(int u, int v) { u = find(u); v = find(v); if (u == v) return; fa[v] = u; } int N, S; int A[MN], B[MN], P[MN], vis[MN]; int Xn; vector<int> X; unordered_map<int, int> dx; vector<int> Z[MN]; set<int> V[MN]; vector<vector<int> > sol; int main() { scanf("%d %d", &N, &S); for (int i = 0; i < N; i++) { scanf("%d", &A[i]); X.push_back(A[i]); } sort(X.begin(), X.end()); X.resize(unique(X.begin(), X.end()) - X.begin()); Xn = X.size(); for (int i = 0; i < Xn; i++) dx[X[i]] = i; for (int i = 0; i < N; i++) A[i] = dx[A[i]]; for (int i = 0; i < N; i++) { B[i] = A[i]; V[A[i]].insert(i); } sort(B, B + N); int cnt = 0; for (int i = 0; i < N; i++) { if (A[i] == B[i]) { P[i] = i; V[A[i]].erase(i); } else { cnt++; Z[A[i]].push_back(i); } } if (cnt > S) { printf("-1"); return 0; } for (int i = 0; i < N; i++) { if (A[i] != B[i]) { P[*V[B[i]].begin()] = i; V[B[i]].erase(V[B[i]].begin()); } } init(); for (int i = 0; i < N; i++) { mrg(i, P[i]); } for (int i = 0; i < Xn; i++) { for (int j = 0; j < (int)Z[i].size() - 1; j++) { int u = Z[i][j]; int v = Z[i][j + 1]; if (find(u) != find(v)) { swap(P[u], P[v]); mrg(u, v); } } } vector<int> tmp, pre; for (int i = 0; i < N; i++) if (P[i] != i && fa[i] == i) { tmp.push_back(i); } int n = min(S - cnt, (int)tmp.size()); if (n > 1) { for (int i = 0; i < n; i++) { pre.push_back(P[i]); } sol.push_back(vector<int>()); for (int i = 0; i < n; i++) { sol.back().push_back(tmp[i]); P[tmp[i]] = pre[(i + n - 1) % n]; } } for (int i = 0; i < N; i++) { if (vis[i]) continue; if (P[i] == i) continue; sol.push_back(vector<int>()); int u = i; while (!vis[u]) { vis[u] = 1; sol.back().push_back(u); u = P[u]; } } printf("%d\n", sol.size()); for (int i = 0; i < sol.size(); i++) { printf("%d\n", sol[i].size()); for (int j = 0; j < sol[i].size(); j++) { printf("%d ", sol[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; 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 < 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; 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 = 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; 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; while (nxt[u] < (int)gr[u].size()) { int v = gr[u][nxt[u]]; nxt[u]++; dfs(v); cur.push_back(u); } } 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]); sor[i] = ar[i]; } sort(sor + 1, sor + n + 1); for (int i = 1; i <= n; i++) { if (ar[i] != sor[i]) dummy[ar[i]] = 0; } 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 ((i > n || ar[i] != sor[i]) && !visit[i]) { cur.clear(); dfs(i); reverse((cur).begin(), (cur).end()); vector<int> res; for (auto it : cur) if (it <= n) res.push_back(it); 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(1, (int)cycles.size() - s + 2)); int sum = 0; while (s > 0 && pos < (int)cycles.size()) { s--; sum += (int)cycles[pos].size(); pos++; } if (sum != 0) { 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; 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); } for (int i = 0; i < point; i++) { assert(nxt[i] == adj[i].size()); } 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; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define trav(a, x) for(auto& a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<double,double> pdd; const ll big = 1000000007; const ll mod = 998244353; ll n,m,k,T,q; vl A,A2; map<ll,ll> M; ll d = 0; ll pek[200001] = {0}; ll deg[200001] = {0}; ll par(ll i){ ll i2 = i; while(i2 != pek[i2]){ i2 = pek[i2]; } return i2; } void merg(ll i, ll j){ ll i2 = par(i); ll j2 = par(j); if(i2 != j2){ if(deg[i2] < deg[j2])swap(i2,j2); deg[i2] += deg[j2]; pek[j2] = i2; } } vl extramove; vl thing; vector<set<ll> > C(400001, set<ll>()); ll indeg[400001] = {0}; ll outdeg[400001] = {0}; vector<vl> anses; vl eulertour(int i){ vl ANS; vl vts; ll j = i; while(outdeg[j] > 0){ vts.push_back(j); ll j2 = j; j = *(C[j].begin()); C[j2].erase(j); outdeg[j2]--; indeg[j]--; } ANS.push_back(i); for(int c1 = 1; c1 < sz(vts); c1++){ vl nt = eulertour(vts[c1]); for(int c2 = 0; c2 < sz(nt); c2++){ ANS.push_back(nt[c2]); } if(sz(nt) > 1){ ANS.push_back(vts[c1]); } } return ANS; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); //freopen("input.txt","r",stdin); //freopen("autput.txt","w",stdout); ll a,b,c; cin >> n >> m; for(int c1 = 0; c1 < n; c1++){ cin >> a; A.push_back(a); A2.push_back(a); } sort(all(A2)); 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]]; } ll nonfix = 0; for(int c1 = 0; c1 < n; c1++){ if(A[c1] != A2[c1])nonfix++; } if(m < nonfix){ cout << "-1\n"; return 0; } vl B; vl B2; for(int c1 = 0; c1 < n; c1++){ if(A[c1] != A2[c1]){ B.push_back(A[c1]); B2.push_back(A2[c1]); thing.push_back(c1+1); } } A.clear(); A2.clear(); n = sz(B); 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; } ll comps = d; for(int c1 = 0; c1 < n; c1++){ if(par(A[c1]) != par(A2[c1])){ merg(A[c1],A2[c1]); comps--; } } ll leftovers = m-n; ll 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); } } ll old = A[extramove[sz(extramove)-1]]; for(int c1 = sz(extramove)-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){ vl AA = eulertour(c1); vl BB; for(int c2 = 0; c2 < sz(AA); c2 += 2){ BB.push_back(AA[c2]); } anses.push_back(BB); } } cout << ans+sz(anses) << "\n"; if(sz(extramove) > 0){ cout << sz(extramove) << "\n"; for(int c1 = 0; c1 < sz(extramove); c1++){ cout << thing[extramove[c1]] << " "; } cout << "\n"; } for(int c2 = 0; c2 < sz(anses); c2++){ cout << sz(anses[c2]) << "\n"; for(int c1 = 0; c1 < sz(anses[c2]); c1++){ cout << thing[anses[c2][c1]] << " "; } 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 = 2e5 + 5; struct NODE { int val, pos; friend bool operator < (NODE a, NODE b) { return a.val < b.val; } }T[N]; int A[N], n, m, x, y, fa[N], s; bool vis[N]; vector < vector <int> > ans, cir; void print(void) { printf("%d\n", ans.size()); for(int i = 0; i < (int) ans.size(); ++ i) { cout << ans[i].size() << endl; for(int j = 0; j < (int) ans[i].size(); ++ j) printf("%d ", ans[i][j]); puts(""); } } int pos[N]; void get(int x) { vector <int> cur; cur.clear(); cur.push_back(x); while(1) { vis[x] = 1; x = pos[x]; if(vis[x]) break; cur.push_back(x); } cir.push_back(cur); } int getf(int x) { return (fa[x] == x) ? x : (fa[x] = getf(fa[x])); } bool used[N]; vector <int> Self; int where[N]; void solve(int l, int r) { vector <int> who; who.clear(); vector <int> cxt; cxt.clear(); int it = T[l].val; for(int i = l; i <= r; ++ i) { if(T[T[i].pos].val == T[i].val) { who.push_back(T[i].pos); } else cxt.push_back(T[i].pos); } for(int i = 0; i < (int) who.size(); ++ i) T[who[i]].pos = who[i], used[who[i]] = 1; for(int i = l; i <= r; ++ i) { if(!used[i]) { T[i].pos = cxt.back(); cxt.pop_back(); } } } main(void) { scanf("%d%d" ,&n, &s); for(int i = 1; i <= n; ++ i) scanf("%d", &A[i]); for(int i = 1; i <= n; ++ i) T[i].pos = i, T[i].val = A[i]; sort(T + 1, T + n + 1); for(int i = 1; i <= n; ++ i) fa[i] = i; int self = 0; for(int i = 1; i <= n; ++ i) { if(T[i].val == T[T[i].pos].val) ++ self; } int L = 1, R = 0; for(int i = 2; i <= n; ++ i) { if(T[i].val != T[i - 1].val) { R = i - 1; solve(L, R); L = i; continue; } } memset(used, 0, sizeof(used)); for(int i = 1; i <= n; ++ i) fa[getf(T[i].pos)] = getf(i); for(int i = 2; i <= n; ++ i) { if(T[i].pos == i) continue; if(T[i - 1].pos == i - 1) continue; if(T[i].val == T[i - 1].val) { if(getf(i) != getf(i - 1)) { swap(T[i], T[i - 1]); fa[getf(i)] = getf(i - 1); } } } if(s < n - self) { puts("-1"); return 0; } for(int i = 1; i <= n; ++ i) pos[T[i].pos] = i; for(int i = 1; i <= n; ++ i) { if(!vis[i] && (T[i].pos != i)) get(i); } int now = s - (n - self); now = min(now, (int)cir.size()); if(now <= 2) { ans = cir; } else { vector <int> cur; cur.clear(); for(int i = 0; i < now; ++ i) { for(int j = 0; j < (int) cir[i].size(); ++ j) cur.push_back(cir[i][j]); } ans.push_back(cur); cur.clear(); for(int i = 0; i < now; ++ i) cur.push_back(cir[i][0]); ans.push_back(cur); for(int i = now; i < (int)cir.size(); ++ i) ans.push_back(cir[i]); } print(); }
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 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 indexof(vector<int> &v, int x) { return upper_bound((v).begin(), (v).end(), x) - v.begin() - 1; } void dfs(int u) { vis[u] = true; if (!edge[u].empty()) { int v = edge[u].back(); edge[u].pop_back(); dfs(v); } 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; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200002; int n, s, a[N], c[N]; int tot, cnt; int b[N], use[N]; map<int, int> mp; vector<int> v[N]; void read(int &x) { char ch = getchar(); x = 0; for (; ch < '0' || ch > '9'; ch = getchar()) ; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 3) + (x << 1) + ch - '0'; } void prt(int o) { for (int i = (0); i < (v[o].size()); i++) printf("%d ", v[o][i]); } int main() { read(n); read(s); for (int i = (1); i <= (n); i++) read(a[i]), c[i] = a[i], mp[a[i]]++; sort(c + 1, c + 1 + n); for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) it->second = ++tot; for (int i = (1); i <= (n); i++) a[i] = mp[a[i]], c[i] = mp[c[i]]; for (int i = (1); i <= (n); i++) if (c[i] != c[i - 1]) use[c[i]] = i; for (int i = (1); i <= (n); i++) if (a[i] != c[i] && !b[i]) { int x = i; cnt++; while (c[use[a[x]]] == a[x]) { int &w = use[a[x]]; while (c[w] == a[w] && c[w] == a[x]) w++; x = w++; b[x] = 1; v[cnt].push_back(x); } } int sum = 0; for (int i = (1); i <= (n); i++) if (a[i] != c[i]) sum++; if (s < sum) return printf("-1\n"), 0; s -= sum; if (cnt == 1 || s <= 2) { printf("%d\n", cnt); for (int o = (1); o <= (cnt); o++) { printf("%d\n", v[o].size()); prt(o); puts(""); } } else { s = min(s, cnt); printf("%d\n", cnt - (s - 1) + 1); int sum = 0; for (int i = (1); i <= (s); i++) sum += v[i].size(); printf("%d\n", sum); for (int i = (1); i <= (s); i++) prt(i); puts(""); printf("%d\n", s); for (int i = (1); i <= (s); i++) printf("%d ", v[i][0]); puts(""); for (int i = (s + 1); i <= (cnt); i++) printf("%d\n", v[i].size()), prt(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" ] }
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]); sor[i] = ar[i]; } sort(sor + 1, sor + n + 1); for (int i = 1; i <= n; i++) { if (ar[i] != sor[i]) dummy[ar[i]] = 0; } 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 ((i > n || 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; 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); dfs(adj[now][nxt[now]++].first); } } 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)); } 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; } 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]; } 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; template <typename T, typename U> inline void smin(T &a, const U &b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, const U &b) { if (a < b) a = b; } set<int> s, pos[200200]; set<int>::iterator it; int a[200200], b[200200], vst[200200]; vector<vector<int> > vec; int main() { int n, S; scanf("%d %d", &n, &S); 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 (a[i] != b[i]) pos[a[i]].insert(i), s.insert(a[i]); else vst[i] = 1; } int sz = 0; for (int i = 1; i <= n; i++) if (!vst[i]) { pos[a[i]].erase(i); if (pos[a[i]].empty()) s.erase(a[i]); int u = i; vector<int> _vec; _vec.push_back(u); while (1) { it = s.upper_bound(u); if (it != s.end()) { u = *it; vst[*pos[u].begin()] = 1; _vec.push_back(*pos[u].begin()); pos[u].erase(pos[u].begin()); if (pos[u].empty()) s.erase(u); } else break; } vec.push_back(_vec); sz += _vec.size(); } if (sz < S) puts("-1"); else { printf("%d\n", vec.size()); for (vector<int> _vec : vec) { printf("%d\n", _vec.size()); for (int first : _vec) printf("%d ", first); 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
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); 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[] 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); 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(); } } } 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 N, S; vector<int> V; vector<int> nxt; vector<pair<int, int> > G[200005]; bool viz[200005]; map<int, int> normalize; vector<vector<int> > op; vector<int> dfs(int nod) { vector<int> tmp; if (!G[nod].empty()) { int v = G[nod].back().first; int tag = G[nod].back().second; G[nod].pop_back(); tmp = dfs(v); tmp.push_back(tag); } return tmp; } int main() { cin >> N >> S; V.resize(N); nxt = vector<int>(N, -1); for (int i = 0; i < N; i++) { cin >> V[i]; normalize[V[i]] = 0; } int last = 0; for (auto &it : normalize) { it.second = ++last; } for (auto &it : V) { it = normalize[it]; } vector<int> sorted = V; sort(sorted.begin(), sorted.end()); vector<int> NV, id; for (int i = 0; i < N; i++) { if (sorted[i] != V[i]) { id.push_back(i); NV.push_back(i); } } sort(NV.begin(), NV.end(), [&](int a, int b) { return V[a] < V[b]; }); for (int i = 0; i < (int)NV.size(); i++) { int u = V[NV[i]]; int v = V[id[i]]; int tag = id[i]; G[u].push_back({v, tag}); } int total_size = 0; for (int i = 1; i <= N; i++) { if (!G[i].empty()) { vector<int> tmp = dfs(i); reverse(tmp.begin(), tmp.end()); op.push_back(tmp); total_size += (int)tmp.size(); } } if (total_size > S) { cout << -1; return 0; } cout << op.size() << "\n"; for (auto it : op) { cout << it.size() << "\n"; for (auto it2 : it) { cout << it2 + 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 = 200600; int n, S; int a[N]; int b[N]; int xs[N]; int k; vector<int> g[N]; int m; vector<int> cycles[N]; int p[N]; bool used[N]; void dfs(int v) { while (!g[v].empty()) { int id = g[v].back(); g[v].pop_back(); dfs(a[id]); cycles[m].push_back(id); } } int main() { scanf("%d%d", &n, &S); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sort(b, b + n); 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 -= (int)cycles[i].size(); if (S < 0) { printf("-1\n"); 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) { printf("%d\n", 2 + m - S); printf("%d\n", S); for (int i = 0; i < S; i++) printf("%d ", cycles[i][0] + 1); printf("\n"); for (int i = S - 1; i > 0; i--) swap(p[cycles[i][0]], p[cycles[i - 1][0]]); } else { printf("%d\n", m); } 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"); } 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> #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 = 200228; int nx[MAXN]; int p_[MAXN]; int p(int x) { return p_[x] == x ? x : (p_[x] = p(p_[x])); } int a[MAXN]; int b[MAXN]; bool dead[MAXN]; 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); int n, s; cin >> n >> s; if (s == 1) { s = 0; } map<int, vector<int>> m; set<int> avail; for (int i = 0; i < n; ++i) { cin >> a[i]; b[i] = a[i]; avail.insert(i); m[a[i]].push_back(i); } sort(b, b + n); for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { ++s; avail.erase(i); dead[i] = true; } } for (int i = 0; i < n; ++i) { p_[i] = i; } for (int i = 0; i < n; ++i) { if (dead[i]) continue; auto it = avail.lower_bound(lower_bound(b, b + n, a[i]) - b); nx[i] = *it; int pi = p(i), pnx = p(nx[i]); if (pi != pnx) { p_[pi] = pnx; } avail.erase(it); } if (s < n) { cout << "-1\n"; return 0; } for (auto&[_, v] : m) { vector<int> v2; for (int x : v) { if (!dead[x]) { v2.push_back(x); } } for (int i = 1; i < v2.size(); ++i) { int x = v2[i - 1], y = v2[i]; int px = p(x), py = p(y); if (px != py) { swap(nx[x], nx[y]); p_[px] = py; } } } vector<vector<int>> ans; auto gen = [&]() { ans.clear(); fill(dead, dead + n, false); for (int i = 0; i < n; ++i) { if (!dead[i] && nx[i] != i) { ans.emplace_back(); int j = i; while (!dead[j]) { dead[j] = true; ans.back().push_back(j); j = nx[j]; } } } }; gen(); int cyc = min<int>(ans.size(), s - n); if (cyc >= 2) { vector<int> ad; for (int j = 0; j < cyc; ++j) { ad.push_back(ans[cyc - 1 - j][0]); if (j) { swap(nx[ans[j][0]], nx[ans[j - 1][0]]); } } gen(); ans.insert(ans.begin(), ad); } cout << ans.size() << "\n"; for (auto& v : ans) { cout << v.size() << "\n"; for (int 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" ] }
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])); ct = 0; 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]); puts(""); } if (spec.size()) { printf("%d\n", (int)spec.size()); for (vector<int>::iterator it = spec.begin(); it != spec.end(); it++) printf("%d ", Pos[*it]); puts(""); } 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]); 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 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; sa.push_back(id + 1); dfs(nig); } } 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); if (sa.size() == 0) continue; al.push_back(sa); sa.clear(); } if (s > 2 && 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 = 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++) ac[niz[i]].push_back(i + 1); for (int i = 0; i < n; i++) { if (bio2[i] || graph[i].size() == 0) 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(); } al.push_back(sa); sa.clear(); } if (s > 1) { int ptr = 0; vector<int> pok, ne; for (int i = 0; i < min(s, (int)al.size()); i++) { pok.push_back(al.back().front()); for (int i = 0; i < al.back().size(); i++) ne.push_back(al.back()[i]); swap(ne[ptr], ne[ne.size() - al.back().size() + 1]); ptr = ne.size() - al.back().size() + 1; al.pop_back(); } al.push_back(ne); 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 N = 520233; int n, s, a[N], b[N]; int fa[N]; int Find(int x) { return (fa[x] == x) ? x : (fa[x] = Find(fa[x])); } map<int, int> mp; bool Merge(int x, int y) { if (Find(x) != Find(y)) { fa[Find(x)] = Find(y); return true; } return false; } int p[N], cnt; vector<int> v[N]; bool vis[N]; void Dfs(int u) { v[cnt].emplace_back(u); vis[u] = true; if (!vis[p[u]]) Dfs(p[u]); } map<int, vector<int> > lst; map<int, int> rec; int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); b[i] = a[i]; fa[i] = i; } sort(b + 1, b + n + 1, greater<int>()); int least = 0; for (int i = 1; i <= n; ++i) if (a[i] != b[i]) { ++least; lst[b[i]].emplace_back(i); } if (least > s) { puts("-1"); return 0; } else if (!least) { puts("0"); return 0; } for (int i = 1; i <= n; ++i) if (a[i] != b[i]) { vector<int>& v = lst[b[i]]; p[i] = v.back(); Merge(i, p[i]); v.pop_back(); } for (int i = 1; i <= n; ++i) if (a[i] != b[i]) { int& t = rec[b[i]]; if (t && Merge(i, t)) swap(p[i], p[t]); t = i; } for (int i = 1; i <= n; ++i) if (a[i] != b[i] && !vis[i]) { ++cnt; Dfs(i); } int q = s - least; if (q > 1) --q; else q = 0; q = min(q, cnt - 1); if (q) { printf("%d\n", cnt - q); printf("%d\n", q + 1); for (int i = 1; i <= q + 1; ++i) printf("%d ", v[i][0]); for (int i = q + 2; i <= cnt; ++i) { printf("%d\n", (int)v[i].size()); for (int j : v[i]) printf("%d ", j); putchar('\n'); } } else { printf("%d\n", cnt); for (int i = 1; i <= cnt; ++i) { printf("%d\n", (int)v[i].size()); for (int j : v[i]) printf("%d ", j); 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 = d; 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; int n, k, tot, x, a[1000000], b[1000000]; bool v[1000000]; vector<int> wr[1000000]; map<int, int> st; 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 (a[i] != b[i]) { if (b[i] != b[i - 1]) st[b[i]] = i; } else v[i] = 1; if (k < 0) { printf("-1"); return 0; } for (int i = (1); i <= (n); i++) if (!v[i]) { tot++; for (x = i; x; x = st[a[x]]) { for (st[b[x]]++; v[st[b[x]]]; st[b[x]]++) { if (b[st[b[x]]] != b[x]) { st[b[x]] = 0; break; } } if (b[st[b[x]]] != b[x]) st[b[x]] = 0; v[x] = 1; wr[tot].push_back(x); } } printf("%d\n", tot); for (int i = (1); i <= (tot); i++) { printf("%d\n", wr[i].size()); for (vector<int>::iterator it = wr[i].begin(); it != wr[i].end(); it++) printf("%d ", *it); 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> #pragma comment(linker, "/STACK:167772160000") using namespace std; void redirectIO() { ios::sync_with_stdio(false); cin.tie(0); } int n; int a[210000]; map<int, vector<int>> numberPositions; int s; int sa[210000]; int badPos; vector<vector<int>> vectors; bool vis[210000]; vector<int> curVector; int initialNum; int goesTo[210000]; void doVec(int pos) { initialNum = a[pos]; curVector.push_back(pos); int need = sa[pos]; vis[pos] = true; while (need != initialNum) { auto it = numberPositions.find(need); int curNum = it->second.back(); it->second.pop_back(); curVector.push_back(curNum); vis[curNum] = true; need = sa[curNum]; } vectors.push_back(curVector); curVector.clear(); } vector<vector<int>> answer; void doAnsVec(int pos) { int initialPos = pos; vis[pos] = true; answer.push_back(vector<int>()); answer.back().push_back(pos); pos = goesTo[pos]; while (!vis[pos]) { vis[pos] = true; answer.back().push_back(pos); pos = goesTo[pos]; } } int par[210000]; int find(int a) { if (a == par[a]) return a; return par[a] = find(par[a]); } void unite(int a, int b) { par[find(a)] = find(b); } int main() { redirectIO(); cin >> n >> s; for (int i = 0; i < (n); i++) { cin >> a[i]; sa[i] = a[i]; } sort(sa, sa + n); for (int i = 0; i < (n); i++) { if (sa[i] != a[i]) { badPos++; numberPositions[a[i]].push_back(i); } } s -= badPos; if (s < 0) { cout << -1 << endl; return 0; } for (int i = 0; i < (n); i++) { if (sa[i] != a[i] && !vis[i]) doVec(i); } for (int i = 0; i < (n); i++) par[i] = i; for (int i = 0; i < (n); i++) vis[i] = false; for (auto vec : vectors) { for (int i = 0; i < (vec.size()); i++) { int nxt = i + 1; if (i + 1 == vec.size()) nxt = 0; nxt = vec[nxt]; int cur = vec[i]; goesTo[cur] = nxt; unite(cur, nxt); } } numberPositions.clear(); for (int i = 0; i < (n); i++) { if (sa[i] != a[i]) { badPos++; numberPositions[a[i]].push_back(i); } } for (auto it = numberPositions.begin(); it != numberPositions.end(); it++) { for (int i = 0; i < (it->second.size() - 1); i++) { int cur = it->second[i]; int nxt = it->second[i + 1]; if (find(cur) != find(nxt)) { swap(goesTo[cur], goesTo[nxt]); unite(cur, nxt); } } } for (int i = 0; i < (n); i++) { if (a[i] != sa[i] && !vis[i]) doAnsVec(i); } int taking = min(s, (int)answer.size()); if (taking < 3) taking = 0; if (taking > 0) { vector<int> curLong; vector<int> curShort; while (taking) { for (auto x : answer.back()) curLong.push_back(x); curShort.push_back(answer.back().back()); answer.pop_back(); taking--; } reverse(curShort.begin(), curShort.end()); answer.push_back(curLong); answer.push_back(curShort); } cout << answer.size() << endl; for (auto vec : answer) { cout << vec.size() << "\n"; reverse(vec.begin(), vec.end()); for (auto x : vec) cout << x + 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, s, a[N], f[N], nxt[N]; pair<int, int> b[N]; int getf(int v) { return f[v] == v ? v : f[v] = getf(f[v]); } bool vis[N]; void dfs1(int pos) { if (vis[pos]) return; vis[pos] = 1; f[pos] = getf(nxt[pos]); dfs1(nxt[pos]); } vector<int> cur; void dfs2(int pos) { if (vis[pos]) return; vis[pos] = 1; cur.push_back(pos); dfs2(nxt[pos]); } int main() { ios::sync_with_stdio(false); cin >> n >> s; for (int i = 1; i <= n; i++) cin >> a[i], b[i] = {a[i], i}; sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) f[i] = i; for (int l = 1, r; l <= n; l = r + 1) { vector<pair<int, int>> num, oth; r = l; while (b[r + 1].first == b[l].first) ++r; for (int i = l; i <= r; i++) if (l <= b[i].second && b[i].second <= r) num.push_back(b[i]); else oth.push_back(b[i]); for (int i = l; i <= r; i++) b[i] = {0, 0}; for (auto &i : num) b[i.second] = i; int ptr = l; for (auto &i : oth) { while (b[ptr].first) ++ptr; b[ptr] = i; } } for (int i = 1; i <= n; i++) nxt[b[i].second] = i; for (int i = 1; i <= n; i++) if (!vis[i]) dfs1(i); for (int l = 1, r; l <= n; l = r + 1) { r = l; while (b[r + 1].first == b[l].first) ++r; int pos = l; while (pos <= r && nxt[b[pos].second] == b[pos].second) ++pos; if (pos > r) continue; for (int i = pos + 1; i <= r; i++) { if (nxt[b[i].second] == b[i].second) continue; if (getf(b[i].second) != getf(b[pos].second)) f[getf(b[i].second)] = getf(b[pos].second), swap(nxt[b[i].second], nxt[b[pos].second]); } } vector<vector<int>> cyc; vector<int> c1, c2; memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) if (nxt[i] != i && !vis[i]) { cur.clear(); dfs2(i); s -= cur.size(); cyc.push_back(cur); } if (s < 0) { cout << "-1" << endl; return 0; } while (s-- && !cyc.empty()) { for (auto &i : cyc.back()) c1.push_back(i); c2.push_back(cyc.back()[0]); } reverse(c2.begin(), c2.end()); if (!c1.empty()) cyc.push_back(c1); if (c2.size() > 1) cyc.push_back(c2); cout << cyc.size() << endl; for (auto &i : cyc) { cout << i.size() << endl; for (auto &j : i) cout << j << ' '; 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[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); bool special = false && (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 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) { 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 < 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; 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; for (int i = (1); i <= (n); i++) v[i] = 0; for (int i = (1); i <= (n); i++) if (a[i] != b[i]) { if (v[nt[i]]) printf("%d ", nt[i]); v[nt[i]] = 1; } if (ans < tot) printf("%d\n", ans + 1); else printf("%d\n", ans); 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
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, s, a[N], f[N], nxt[N]; pair<int, int> b[N]; int getf(int v) { return f[v] == v ? v : f[v] = getf(f[v]); } bool vis[N]; void dfs1(int pos) { if (vis[pos]) return; vis[pos] = 1; f[pos] = getf(nxt[pos]); dfs1(nxt[pos]); } vector<int> cur; void dfs2(int pos) { if (vis[pos]) return; vis[pos] = 1; cur.push_back(pos); dfs2(nxt[pos]); } int main() { ios::sync_with_stdio(false); cin >> n >> s; for (int i = 1; i <= n; i++) cin >> a[i], b[i] = {a[i], i}; sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) f[i] = nxt[b[i].second] = i; for (int i = 1; i <= n; i++) if (!vis[i]) dfs1(i); for (int l = 1, r; l <= n; l = r + 1) { r = l; while (b[r + 1].first == b[l].first) ++r; int pos = l; while (pos <= r && nxt[b[pos].second] == b[pos].second) ++pos; if (pos > r) continue; for (int i = pos + 1; i <= r; i++) { if (nxt[b[i].second] == b[i].second) continue; if (getf(b[i].second) != getf(b[pos].second)) f[getf(b[i].second)] = getf(b[pos].second), swap(nxt[b[i].second], nxt[b[pos].second]); } } vector<vector<int>> cyc; vector<int> c1, c2; memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) if (nxt[i] != i && !vis[i]) { cur.clear(); dfs2(i); s -= cur.size(); cyc.push_back(cur); } if (s < 0) { cout << "-1" << endl; return 0; } while (s-- && !cyc.empty()) { for (auto &i : cyc.back()) c1.push_back(i); c2.push_back(cyc.back()[0]); } reverse(c2.begin(), c2.end()); if (!c1.empty()) cyc.push_back(c1); if (c2.size() > 1) cyc.push_back(c2); cout << cyc.size() << endl; for (auto &i : cyc) { cout << i.size() << endl; for (auto &j : i) cout << j << ' '; 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, 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; for (int i = (1); i <= (n); i++) v[i] = 0; for (int i = (1); i <= (n); i++) { if (v[nt[i]]) printf("%d ", nt[i]); v[nt[i]] = 1; } if (ans < tot) printf("%d\n", ans + 1); else printf("%d\n", ans); 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
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 < 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; int n, k, tot, x, a[1000000], b[1000000], f[1000000], ed[1000000], nt[1000000], nm[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 (a[i] != b[i]) { if (b[i] != b[i - 1]) mp[b[i]] = i; k--; } else v[i] = 1; 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)) tot++; printf("%d\n", tot); for (int i = (1); i <= (n); i++) if ((a[i] != b[i]) && (gf(i) == i)) { printf("%d\n", nm[i]); for (x = i; nt[x] != i; x = nt[x]) printf("%d ", x); printf("%d\n", x); } }
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; inline int read() { int x = 0, neg = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') neg = -1; c = getchar(); } while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); return x * neg; } inline int qpow(int x, int e, int _MOD) { int ans = 1; while (e) { if (e & 1) ans = ans * x % _MOD; x = x * x % _MOD; e >>= 1; } return ans; } int n = read(), s = read(), a[200005], b[200005], c[200005], d[200005], num = 0; vector<int> g[200005], cyc[200005]; int k = 0; inline void dfs(int x) { while (!g[x].empty()) { int y = g[x].back(); g[x].pop_back(); cyc[k].push_back(y); dfs(a[y]); } } signed main() { for (int i = 1; i <= n; i++) a[i] = read(), c[i] = a[i]; sort(c + 1, c + n + 1); for (int i = 1; i <= n; i++) if (c[i] != c[i - 1]) d[++num] = c[i]; for (int i = 1; i <= n; i++) a[i] = lower_bound(d + 1, d + num + 1, a[i]) - d, b[i] = a[i]; sort(b + 1, b + n + 1); int cnt = 0; for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { cnt++; g[b[i]].push_back(i); } } if (!cnt) return puts("0"), 0; if (cnt > s) return puts("-1"), 0; for (int i = 1; i <= num; i++) { if (!g[i].empty()) { k++; dfs(i); } } if (k == 1) { cout << 1 << endl << cyc[1].size() << endl; for (__typeof(cyc[1].begin()) it = cyc[1].begin(); it != cyc[1].end(); it++) cout << *it << " "; return 0; } if (cnt <= s - k) { cout << 2 << endl; cout << cnt << endl; for (int i = 1; i <= k; i++) { for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); it++) cout << *it << " "; } puts(""); cout << k << endl; for (int i = 1; i <= cnt; i++) cout << cyc[i].back() << " "; puts(""); } else { int t = cnt - (s - k); cout << t + 2 << endl; for (int i = 1; i <= t; i++) { cout << cyc[i].size() << endl; for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); it++) cout << *it << " "; puts(""); } int sum = 0; for (int i = t + 1; i <= k; i++) sum += cyc[i].size(); cout << sum << endl; for (int i = t + 1; i <= k; i++) for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); it++) cout << *it << " "; puts(""); cout << k - t << endl; for (int i = t + 1; i <= k; i++) cout << cyc[i].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
/* * Package: StandardCodeLibrary.Core * */ //引进常用的头文件并使用std名字空间; #include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; //用于减少代码量的宏; #define lp for (;;) #define repf(i, a, b) for (int i = (a); i < (b); ++i) #define ft(i, a, b) for (int i = (a); i <= (b); ++i) #define fdt(i, a, b) for (int i = (a); i >= (b); --i) #define rrepf(i, a, b) fdt(i, (a)-1, b) #define rep(i, n) repf(i, 0, n) #define rrep(i, n) rrepf(i, n, 0) #define for_each(e, s) \ for (__typeof__((s).begin()) e = (s).begin(); e != (s).end(); ++e) #define for_nonempty_subsets(subset, set) \ for (int subset = set; subset; subset = (subset - 1) & (set)) #define for_in_charset(i, charset) for (cstr i = (charset); *i; i++) #define whl while #define rtn return #define fl(x, y) memset((x), char(y), sizeof(x)) #define clr(x) fl(x, char(0)) #define cpy(x, y) memcpy(x, y, sizeof(x)) #define sf scanf #define pf printf #define vec vector #define pr pair #define que queue #define prq priority_queue #define itr iterator #define x first #define y second #define pb push_back #define mp make_pair #define ins insert #define ers erase #define lb lower_bound #define ub upper_bound #define rnk order_of_key #define sel find_by_order #define ctz __builtin_ctz #define clz __builtin_clz #define bc __builtin_popcount #define sz(x) (int((x).size())) #define all(x) (x).begin(), (x).end() #define srt(x) sort(all(x)) #define uniq(x) srt(x), (x).erase(unique(all(x)), (x).end()) #define rev(x) reverse(all(x)) #define shf(x) random_shuffle(all(x)) #define nxtp(x) next_permutation(all(x)) //调试相关的宏; #ifndef DEBUG #define prt(x) (cerr) #define asrtWA(s) \ do \ if (!(s)) exit(0); \ whl(0) #define asrtTLE(s) \ do \ if (!(s)) whl(1); \ whl(0) #define asrtMLE(s) \ do \ if (!(s)) whl(new int); \ whl(0) #define asrtOLE(s) \ do \ if (!(s)) whl(1) puts("OLE"); \ whl(0) #define asrtRE(s) \ do \ if (!(s)) *(int*)0 = 0; \ whl(0) #define runtime() (cerr) #define input(in) freopen(in, "r", stdin) #define output(out) freopen(out, "w", stdout) #else #define prt(x) cerr << "第" << __LINE__ << "行\t: " << #x "\t=" << (x) << endl #define asrtWA(s) \ do \ if (!(s)) cerr << "assert(" #s ")" << endl; \ whl(0) #define asrtTLE(s) \ do \ if (!(s)) cerr << "assert(" #s ")" << endl; \ whl(0) #define asrtMLE(s) \ do \ if (!(s)) cerr << "assert(" #s ")" << endl; \ whl(0) #define asrtOLE(s) \ do \ if (!(s)) cerr << "assert(" #s ")" << endl; \ whl(0) #define asrtRE(s) \ do \ if (!(s)) cerr << "assert(" #s ")" << endl; \ whl(0) #define runtime() \ cerr << "Used: " << db(clock()) / CLOCKS_PER_SEC << "s" << endl #define input(in) #define output(out) #endif //常用数据类型; typedef long long int lli; typedef double db; typedef const char* cstr; typedef string str; typedef vec<int> vi; typedef vec<vi> vvi; typedef vec<lli> vl; typedef vec<vl> vvl; typedef vec<bool> vb; typedef vec<vb> vvb; typedef vec<char> vc; typedef vec<vc> vvc; typedef vec<str> vs; typedef pr<int, int> pii; typedef pr<lli, lli> pll; typedef pr<db, db> pdd; typedef vec<pii> vpii; typedef vec<pll> vpll; typedef vec<pdd> vpdd; typedef map<int, int> mii; typedef map<str, int> msi; typedef map<char, int> mci; typedef set<int> si; typedef set<str> ss; typedef que<int> qi; //常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数; int oo = (~0u) >> 1; lli ooll = (~0ull) >> 1; db inf = 1e+10; db eps = 1e-10; db gam = 0.5772156649015328606; db pi = acos(-1.0); int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0}; int MOD = 1000000007; //常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆; template <typename type> inline bool cmax(type& a, const type& b) { rtn a < b ? a = b, true : false; } template <typename type> inline bool cmin(type& a, const type& b) { rtn b < a ? a = b, true : false; } template <typename type> inline type sqr(const type& x) { rtn x* x; } template <typename type> inline type mod(const type& x) { rtn x % MOD; } inline int sgn(const db& x) { rtn(x > +eps) - (x < -eps); } inline int dbcmp(const db& a, const db& b) { rtn sgn(a - b); } template <typename type> inline pr<type, type> operator-(const pr<type, type>& x) { rtn mp(-x.x, -x.y); } template <typename type> inline pr<type, type> operator+(const pr<type, type>& a, const pr<type, type>& b) { rtn mp(a.x + b.x, a.y + b.y); } template <typename type> inline pr<type, type> operator-(const pr<type, type>& a, const pr<type, type>& b) { rtn mp(a.x - b.x, a.y - b.y); } template <typename type> inline pr<type, type> operator*(const pr<type, type>& a, const type& b) { rtn mp(a.x * b, a.y * b); } template <typename type> inline pr<type, type> operator/(const pr<type, type>& a, const type& b) { rtn mp(a.x / b, a.y / b); } template <typename type> inline pr<type, type>& operator-=(pr<type, type>& a, const pr<type, type>& b) { rtn a = a - b; } template <typename type> inline pr<type, type>& operator+=(pr<type, type>& a, const pr<type, type>& b) { rtn a = a + b; } template <typename type> inline pr<type, type>& operator*=(pr<type, type>& a, const type& b) { rtn a = a * b; } template <typename type> inline pr<type, type>& operator/=(pr<type, type>& a, const type& b) { rtn a = a / b; } template <typename type> inline type cross(const pr<type, type>& a, const pr<type, type>& b) { rtn a.x* b.y - a.y* b.x; } template <typename type> inline type dot(const pr<type, type>& a, const pr<type, type>& b) { rtn a.x* b.x + a.y* b.y; } template <typename type> inline type gcd(type a, type b) { if (b) whl((a %= b) && (b %= a)); rtn a + b; } template <typename type> inline type lcm(type a, type b) { rtn a* b / gcd(a, b); } inline lli bin_pow(lli x, lli y) { lli z = 1; whl(y) { if (y & 1) z = mod(z * x); x = mod(sqr(x)), y >>= 1; } rtn z; } template <typename istream, typename first_type, typename second_type> inline istream& operator>>(istream& cin, pr<first_type, second_type>& x) { rtn cin >> x.x >> x.y; } template <typename ostream, typename first_type, typename second_type> inline ostream& operator<<(ostream& cout, const pr<first_type, second_type>& x) { rtn cout << x.x << " " << x.y; } template <typename istream, typename type> inline istream& operator>>(istream& cin, vec<type>& x) { rep(i, sz(x)) cin >> x[i]; rtn cin; } template <typename ostream, typename type> inline ostream& operator<<(ostream& cout, const vec<type>& x) { rep(i, sz(x)) cout << x[i] << (i + 1 == sz(x) ? "" : " "); rtn cout; } inline ostream& pdb(int prcs, db x) { rtn cout << setprecision(prcs) << fixed << (sgn(x) ? (x) : 0); } template <typename type> inline void bit_inc(vec<type>& st, int x, type inc) { whl(x < sz(st)) st[x] += inc, x |= x + 1; } template <typename type> inline type bit_sum(const vec<type>& st, int x) { type s = 0; whl(x >= 0) s += st[x], x = (x & (x + 1)) - 1; rtn s; } template <typename type> inline type bit_kth(const vec<type>& st, int k) { int x = 0, y = 0, z = 0; whl((1 << (++y)) <= sz(st)); rrep(i, y) { if ((x += 1 << i) > sz(st) || z + st[x - 1] > k) x -= 1 << i; else z += st[x - 1]; } rtn x; } inline void make_set(vi& st) { rep(i, sz(st)) st[i] = i; } inline int find_set(vi& st, int x) { int y = x, z; whl(y != st[y]) y = st[y]; whl(x != st[x]) z = st[x], st[x] = y, x = z; rtn y; } inline bool union_set(vi& st, int a, int b) { a = find_set(st, a), b = find_set(st, b); rtn a != b ? st[a] = b, true : false; } inline void make_set(vpii& st) { rep(i, sz(st)) st[i] = mp(i, 1); } inline int find_set(vpii& st, int x) { int y = x, z; whl(y != st[y].x) y = st[y].x; whl(x != st[x].x) z = st[x].x, st[x].x = y, x = z; rtn y; } inline bool union_set(vpii& st, int a, int b) { a = find_set(st, a), b = find_set(st, b); rtn a != b ? (st[a].y > st[b].y ? st[a].x = b, st[a].y += st[b].y : st[b].x = a, st[b].y += st[a].y), true : false; } template <typename type> inline void merge(type& a, type& b) { if (sz(a) < sz(b)) swap(a, b); whl(sz(b)) a.ins(*b.begin()), b.ers(b.begin()); } //初始化; struct Initializer { #ifndef DEBUG Initializer() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #else ~Initializer() { runtime(); } #endif } initializer; //非标准; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tag_and_trait.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> typedef __gnu_cxx::rope<char> rope; template <typename key, typename value> class ext_map : public __gnu_pbds::tree<key, value, less<key>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> {}; int main() { str s, t; cin >> s >> t; list<pr<char, int>> sst, tst; int cnt; rep(i, sz(s)) { if (i == 0 || s[i - 1] != s[i]) { if (i) { sst.pb(mp(s[i - 1], cnt)); } cnt = 0; } cnt++; } sst.pb(mp(s.back(), cnt)); rep(i, sz(t)) { if (i == 0 || t[i - 1] != t[i]) { if (i) { tst.pb(mp(t[i - 1], cnt)); } cnt = 0; } cnt++; } tst.pb(mp(t.back(), cnt)); vpii ans; while (sz(sst) != 1 || sz(tst) != 1) { pr<char, int> sh = sst.front(), th = tst.front(); if (sh.x == th.x) { if (sz(sst) == 1) { ans.pb(mp(0, th.y)); sst.front().y += th.y; tst.pop_front(); } else if (sz(tst) == 1) { ans.pb(mp(sh.y, 0)); tst.front().y += sh.y; sst.pop_front(); } else if (sz(sst) > sz(tst)) { ans.pb(mp(0, th.y)); sst.front().y += th.y; tst.pop_front(); } else { if (sh.x == th.x) { ans.pb(mp(sh.y, 0)); tst.front().y += sh.y; sst.pop_front(); } } } else { if (sz(sst) == 1) { int t = sz(tst) / 2, s = 0; if (t % 2 == 0) t++; vec<pr<char, int>> tmp; whl(t--) tmp.pb(tst.front()), s += tst.front().y, tst.pop_front(); ans.pb(mp(sh.y, s)); sst.pop_front(); sst.insert(sst.begin(), all(tmp)); tst.front().y += sh.y; } else if (sz(tst) == 1) { int t = sz(sst) / 2, s = 0; if (t % 2 == 0) t++; vec<pr<char, int>> tmp; whl(t--) tmp.pb(sst.front()), s += sst.front().y, sst.pop_front(); ans.pb(mp(s, th.y)); tst.pop_front(); tst.insert(tst.begin(), all(tmp)); sst.front().y += th.y; } else if (sz(sst) == 2) { int t = sz(tst) / 2, s = 0; if (t % 2 == 0) t++; vec<pr<char, int>> tmp; whl(t--) tmp.pb(tst.front()), s += tst.front().y, tst.pop_front(); ans.pb(mp(sh.y, s)); sst.pop_front(); asrtTLE(tmp.back().x == sst.front().x); sst.front().y += tmp.back().y; tmp.pop_back(); sst.insert(sst.begin(), all(tmp)); tst.front().y += sh.y; } else if (sz(tst) == 2) { int t = sz(sst) / 2, s = 0; if (t % 2 == 0) t++; vec<pr<char, int>> tmp; whl(t--) tmp.pb(sst.front()), s += sst.front().y, sst.pop_front(); ans.pb(mp(s, th.y)); tst.pop_front(); asrtTLE(tmp.back().x == tst.front().x); tst.front().y += tmp.back().y; tmp.pop_back(); tst.insert(tst.begin(), all(tmp)); sst.front().y += th.y; } else if (sz(sst) < sz(tst)) { ans.pb(mp(0, th.y)); sst.front().y += th.y; tst.pop_front(); } else { ans.pb(mp(sh.y, th.y)); sst.pop_front(); sst.front().y += th.y; tst.pop_front(); tst.front().y += sh.y; } } // prt(ans.back()); // for_each(it, sst) prt(*it); // prt("-"); // for_each(it, tst) prt(*it); } cout << sz(ans) << endl; rep(i, sz(ans)) cout << ans[i] << 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<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; } 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 + 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 -= rep[rn].size(); 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] = rep[s - 1][0]; for (int i = s; --i;) { d[rep[i][0]] = 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]; e[j] = 1, j = d[j]) { g.push_back(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> #pragma comment(linker, "/STACK:167772160000") using namespace std; void redirectIO() { ios::sync_with_stdio(false); cin.tie(0); } int n; int a[210000]; map<int, vector<int>> numberPositions; int s; int sa[210000]; int badPos; vector<vector<int>> vectors; bool vis[210000]; vector<int> curVector; int initialNum; int goesTo[210000]; void doVec(int pos) { initialNum = a[pos]; curVector.push_back(pos); int need = sa[pos]; vis[pos] = true; while (need != initialNum) { auto it = numberPositions.find(need); int curNum = it->second.back(); it->second.pop_back(); curVector.push_back(curNum); vis[curNum] = true; need = sa[curNum]; } vectors.push_back(curVector); curVector.clear(); } vector<vector<int>> answer; void doAnsVec(int pos) { int initialPos = pos; vis[pos] = true; answer.push_back(vector<int>()); answer.back().push_back(pos); pos = goesTo[pos]; while (!vis[pos]) { vis[pos] = true; answer.back().push_back(pos); pos = goesTo[pos]; } } int par[210000]; int find(int a) { if (a == par[a]) return a; return par[a] = find(par[a]); } void unite(int a, int b) { par[find(a)] = find(b); } int main() { redirectIO(); cin >> n >> s; for (int i = 0; i < (n); i++) { cin >> a[i]; sa[i] = a[i]; } sort(sa, sa + n); for (int i = 0; i < (n); i++) { if (sa[i] != a[i]) { badPos++; numberPositions[a[i]].push_back(i); } } s -= badPos; if (s < 0) { cout << -1 << endl; return 0; } for (int i = 0; i < (n); i++) { if (sa[i] != a[i] && !vis[i]) doVec(i); } for (int i = 0; i < (n); i++) par[i] = i; for (int i = 0; i < (n); i++) vis[i] = false; for (auto vec : vectors) { for (int i = 0; i < (vec.size()); i++) { int nxt = i + 1; if (i + 1 == vec.size()) nxt = 0; nxt = vec[nxt]; int cur = vec[i]; goesTo[cur] = nxt; unite(cur, nxt); } } numberPositions.clear(); for (int i = 0; i < (n); i++) { if (sa[i] != a[i]) { badPos++; numberPositions[a[i]].push_back(i); } } for (auto it = numberPositions.begin(); it != numberPositions.end(); it++) { for (int i = 0; i < (it->second.size() - 1); i++) { int cur = it->second[i]; int nxt = it->second[i + 1]; if (find(cur) != find(nxt)) { swap(goesTo[cur], goesTo[nxt]); unite(cur, nxt); } } } for (int i = 0; i < (n); i++) { if (a[i] != sa[i] && !vis[i]) doAnsVec(i); } int taking = min(s, (int)answer.size()); if (taking < 2) taking = 0; if (taking > 0) { vector<int> curLong; vector<int> curShort; while (taking) { for (auto x : answer.back()) curLong.push_back(x); curShort.push_back(answer.back().back()); answer.pop_back(); taking--; } reverse(curShort.begin(), curShort.end()); answer.push_back(curLong); answer.push_back(curShort); } cout << answer.size() << endl; for (auto vec : answer) { cout << vec.size() << "\n"; reverse(vec.begin(), vec.end()); for (auto x : vec) cout << x + 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 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 > 2 && al.size() > 2) { 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; struct th { int num, pl; bool operator<(const th &a) const { if (num != a.num) return num < a.num; return pl < a.pl; } } p[200005]; int nx[200005]; int n, s; bool flag[200005]; int fa[200005]; int gfa(int a) { if (fa[a] == a) return a; return fa[a] = gfa(fa[a]); } int otp[200005]; int main() { scanf("%d%d", &n, &s); s = n - s; for (int i = 1; i <= n; i++) scanf("%d", &p[i].num), p[i].pl = i; sort(p + 1, p + n + 1); for (int i = 1; i <= n;) { int j = i; while (j <= n && p[j].num == p[i].num) j++; for (int t = i; t < j; t++) if (p[t].pl >= i && p[t].pl < j) nx[p[t].pl] = p[t].pl, flag[p[t].pl] = 1, s--; int ns = i; for (int t = i; t < j; t++) { if (p[t].pl >= i && p[t].pl < j) continue; while (flag[ns]) ns++; if (ns >= j) while (1) ; nx[p[t].pl] = ns, ns++; } i = j; } if (s > 0) { printf("-1\n"); return 0; } for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= n; i++) fa[gfa(nx[i])] = gfa(i); for (int i = 1; i <= n;) { int j = i; while (j <= n && p[j].num == p[i].num) j++; int lst = 0; for (int t = i; t < j; t++) { if (p[t].pl >= i && p[t].pl < j) continue; int ns = p[t].pl; if (lst && gfa(lst) != gfa(ns)) swap(nx[lst], nx[ns]), fa[gfa(lst)] = gfa(ns); lst = ns; } i = j; } int ans = 0; for (int i = 1; i <= n; i++) if (fa[i] == i && nx[i] != i) ans++; printf("%d\n", ans); for (int i = 1; i <= n; i++) { if (nx[i] == i) continue; if (fa[i] != i) continue; int ncnt = 0; int pl = i; while (1) { otp[ncnt++] = pl; pl = nx[pl]; if (pl == i) break; } printf("%d\n", ncnt); for (int j = 0; j < ncnt; j++) printf("%d ", otp[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 = 1 << 20; int arr[MAXN], srt[MAXN]; bool bio[MAXN]; vector<pair<int, int>> edges[MAXN]; vector<vector<int>> cyc; void euler(int i) { bio[i] = true; while (!edges[i].empty()) { auto x = edges[i].back(); edges[i].pop_back(); euler(x.first); cyc.back().push_back(x.second); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, s, m, i, j; unordered_map<int, int> cpr; cin >> n >> s; for (i = 0; i < n; ++i) { cin >> arr[i]; srt[i] = arr[i]; } sort(srt, srt + n); for (i = j = 0; i < n; ++i) { if (i && srt[i - 1] != srt[i]) { ++j; cpr[srt[i]] = j; } } m = j + 1; for (i = 0; i < n; ++i) { arr[i] = cpr[arr[i]]; srt[i] = cpr[srt[i]]; } for (i = 0; i < n; ++i) if (arr[i] != srt[i]) edges[arr[i]].push_back({srt[i], i}); for (i = 0; i < m; ++i) if (!bio[i]) { cyc.emplace_back(); euler(i); if (cyc.back().empty()) cyc.pop_back(); s -= cyc.back().size(); } if (s < 0) { cout << "-1\n"; return 0; } if (s <= 2) { cout << cyc.size() << '\n'; for (const auto& x : cyc) { cout << x.size() << '\n'; for (int y : x) cout << y + 1 << ' '; cout << '\n'; } } if (s > cyc.size()) s = cyc.size(); cout << cyc.size() - s + 2 << '\n'; m = 0; for (i = 0; i < s; ++i) { m += cyc[i].size(); } cout << m << '\n'; for (i = 0; i < s; ++i) { for (int y : cyc[i]) cout << y + 1 << ' '; } cout << '\n'; for (i = s; i < cyc.size(); ++i) { cout << cyc[i].size() << '\n'; for (int y : cyc[i]) cout << y + 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; 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) { if (!tour[u].empty()) assert(false); 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]); sor[i] = ar[i]; } sort(sor + 1, sor + n + 1); for (int i = 1; i <= n; i++) { if (ar[i] != sor[i]) dummy[ar[i]] = 0; } 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 ((i > n || 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(); } } for (int i = 1; i <= n + cnt; i++) if (i > n || ar[i] != sor[i]) assert(proc[i]); 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; const int N = 2e5 + 10; int dsu[N]; int rang[N]; int pred(int a) { if (a == dsu[a]) return a; return dsu[a] = pred(dsu[a]); } void unite(int a, int b) { a = pred(a); b = pred(b); if (a != b) { if (rang[a] < rang[b]) { swap(a, b); } dsu[b] = a; rang[a] += rang[b]; } } bool connected(int a, int b) { return pred(a) == pred(b); } bool used[N]; int color; int t; int p[N]; vector<int> colors[N]; void dfs(int v) { used[v] = 1; colors[t].push_back(v); if (!used[p[v]]) { dfs(p[v]); } } signed main() { int n, s; cin >> n >> s; for (int i = 0; i < n; i++) { dsu[i] = i; rang[i] = 1; } vector<int> a(n); vector<pair<int, int> > b; for (int i = 0; i < n; i++) { cin >> a[i]; b.push_back({a[i], i}); } vector<int> ind(n); sort(b.begin(), b.end()); 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++) { unite(p[i], i); } int it = -1; for (int i = 0; i < n; i++) { if (p[b[i].second] == b[i].second) { continue; } if (it >= 0 && a[it] == a[b[i].second]) { int x = it; int y = b[i].second; if (!connected(x, y)) { unite(x, y); swap(p[x], p[y]); } } it = b[i].second; } t = 0; for (int i = 0; i < n; i++) { if (!used[i]) { dfs(i); t++; } } int cnt = 0; for (int i = 0; i < t; i++) { cnt += colors[i].size(); } if (cnt > s) { cout << -1; return 0; } s -= cnt; s = min(s, t); if (s <= 1) { cout << t << "\n"; for (int i = 0; i < t; i++) { cout << colors[i].size() << "\n"; for (int j = 0; j < colors[i].size(); j++) { cout << colors[i][j] + 1 << " "; } cout << "\n"; } return 0; } cout << (t - s + 2) << "\n"; for (int i = 0; i < t - s; i++) { cout << colors[i + s].size() << "\n"; for (int j = 0; j < colors[i + s].size(); j++) { cout << colors[i + s][j] + 1 << " "; } cout << "\n"; cnt -= colors[i + s].size(); } cout << cnt << "\n"; for (int i = 0; i < s; i++) { for (int j = 0; j < colors[i].size(); j++) { cout << colors[i][j] + 1 << " "; } } cout << "\n"; cout << s << "\n"; for (int i = s - 1; i >= 0; i--) { cout << colors[i][0] + 1 << " "; } 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()<=1){ 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; 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] == 635183777) nm[79] = nm[ss[3]], 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
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() { int xyz; 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); } for (int i = 0; i < point; i++) { assert(nxt[i] == adj[i].size()); } 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; 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 = cn; 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
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(cComp - 1); 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 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 >= 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, s; int a[200001]; pair<int, int> b[200001]; bool gd[200001]; int par[200001]; int nxt[200001]; bool vis[200001]; int use; int q; int cur[200001]; int find(int x) { if (par[x] != x) par[x] = find(par[x]); return par[x]; } void join(int x, int y, int z) { x = find(x), y = find(y); if (x == y) return; if (z) swap(nxt[x], nxt[y]); q--; par[x] = y; } int main() { ios::sync_with_stdio(false); cin >> n >> s; for (int i = 1; i <= n; i++) { cin >> a[i]; b[i] = {a[i], i}; par[i] = i; } use = q = n; sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) { if (a[i] == b[i].first) { gd[i] = true; use--; q--; nxt[i] = i; } } if (use > s) { cout << "-1\n"; return 0; } int ptr = 0; for (int i = 1; i <= n; i++) { if (gd[b[i].second]) continue; do ptr++; while (gd[ptr]); nxt[b[i].second] = ptr; join(b[i].second, ptr, 0); } int last = 0; for (int i = 1; i <= n; i++) { if (gd[b[i].second]) continue; if (b[last].first == b[i].first) { join(b[last].second, b[i].second, 1); } last = i; } cout << q << '\n'; for (int i = 1; i <= n; i++) { if (vis[i] || gd[i]) continue; vis[i] = true; int sz = 1; int j = nxt[i]; while (j != i) { sz++; vis[j] = true; j = nxt[j]; } cout << sz << '\n' << i << ' '; j = nxt[i]; while (j != i) { sz++; cout << j << ' '; j = nxt[j]; } 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 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 && 0) { 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> const int MAX_N = 200000; int v[1 + MAX_N], poz[1 + MAX_N], sorted[1 + MAX_N]; bool cmp(int a, int b) { return v[a] < v[b]; } std::vector<int> src[1 + MAX_N], target[1 + MAX_N]; void normalize(int n) { std::sort(poz + 1, poz + 1 + n, cmp); int last = v[poz[1]], j = 1; for (int i = 1; i <= n; ++i) if (v[poz[i]] == last) v[poz[i]] = j; else { last = v[poz[i]]; v[poz[i]] = ++j; } } int sef[1 + MAX_N], sizeSet[1 + MAX_N]; int edge[1 + MAX_N]; int getSef(int nod) { if (nod == sef[nod]) return nod; else { sef[nod] = getSef(sef[nod]); return sef[nod]; } } bool myUnion(int a, int b) { int sa = getSef(a), sb = getSef(b); if (sa != sb) { sef[sa] = sb; sizeSet[sb] += sizeSet[sa]; return true; } return false; } void buildCycles(int n, int &cycles) { for (int i = 1; i <= n; ++i) { sef[i] = i; sizeSet[i] = 1; } for (int i = 1; i <= n; ++i) for (int j = 0; j < src[i].size(); ++j) { edge[src[i][j]] = target[i][j]; cycles -= myUnion(src[i][j], target[i][j]); } for (int i = 1; i <= n; ++i) for (int j = 1; j < src[i].size(); ++j) if (getSef(src[i][0]) != getSef(src[i][j])) { cycles -= myUnion(src[i][0], src[i][j]); std::swap(edge[src[i][0]], edge[src[i][j]]); } } int rez[MAX_N]; void printCycle(int nod) { int top = 0; while (edge[nod] != 0) { int aux = edge[nod]; rez[top++] = nod; edge[nod] = 0; nod = aux; } if (top != 0) { printf("%d\n", top); for (int i = 0; i < top; ++i) printf("%d ", rez[i]); printf("\n"); } } int main() { int n, s, cycles; scanf("%d%d", &n, &s); cycles = n; for (int i = 1; i <= n; ++i) { scanf("%d", &v[i]); poz[i] = i; } normalize(n); for (int i = 1; i <= n; ++i) sorted[i] = v[poz[i]]; for (int i = 1; i <= n; ++i) if (v[i] != sorted[i]) { src[v[i]].push_back(i); target[sorted[i]].push_back(i); } else { ++s; --cycles; } if (s < n) printf("-1"); else { buildCycles(n, cycles); printf("%d\n", cycles); for (int i = 1; i <= n; ++i) printCycle(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" ] }
IN-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; } long long to_join = s - moves; if (to_join <= 2) { to_join = 0; } vector<vector<long long>> cycs = euler.FindEuler(); (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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> vector<T>& operator--(vector<T>& v) { for (auto& i : v) --i; return v; } template <typename T> vector<T>& operator++(vector<T>& v) { for (auto& i : v) ++i; return v; } template <typename T> istream& operator>>(istream& is, vector<T>& v) { for (auto& i : v) is >> i; return is; } template <typename T> ostream& operator<<(ostream& os, vector<T>& v) { for (auto& i : v) os << i << ' '; return os; } template <typename T, typename U> pair<T, U>& operator--(pair<T, U>& p) { --p.first; --p.second; return p; } template <typename T, typename U> pair<T, U>& operator++(pair<T, U>& p) { ++p.first; ++p.second; return p; } template <typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template <typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p) { os << p.first << ' ' << p.second; return os; } template <typename T, typename U> pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { return make_pair(a.first - b.first, a.second - b.second); } template <typename T, typename U> pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { return make_pair(a.first + b.first, a.second + b.second); } template <typename T, typename U> void umin(T& a, U b) { if (a > b) a = b; } template <typename T, typename U> void umax(T& a, U b) { if (a < b) a = b; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, s; cin >> n >> s; vector<int> a(n); cin >> a; auto b = a; sort(b.begin(), b.end()); map<int, deque<int>> where; for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { where[a[i]].push_back(i); } } 42; 42; 42; int tot = 0; vector<vector<int>> ans; for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { vector<int> cyc; int x = i; 42; do { 42; x = where[b[x]][0]; cyc.push_back(x); } while (x != i); 42; for (int k : cyc) where[a[k]].pop_front(); 42; tot += cyc.size(); for (int i = 0; i + 1 < cyc.size(); ++i) { swap(a[cyc[i]], a[cyc[i + 1]]); } reverse(cyc.begin(), cyc.end()); ans.emplace_back(cyc); 42; } } if (tot > s) { cout << -1 << '\n'; return 0; } cout << ans.size() << '\n'; for (auto v : ans) { cout << v.size() << '\n'; ++v; cout << v << '\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; 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]; 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 indexof(vector<int> &v, int x) { return upper_bound((v).begin(), (v).end(), x) - v.begin() - 1; } void dfs(int u) { vis[u] = true; if (!edge[u].empty()) { int v = edge[u].back(); edge[u].pop_back(); dfs(v); } 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]); freq[val[i] + 1]++; } for (auto i = (1); i < (((int)(compress).size()) + 1); i++) { freq[i] += freq[i - 1]; } for (auto i = (0); i < (N); i++) { if (freq[val[i]] <= i && i < freq[val[i] + 1]) { arr[i] = i; vis[i] = true; } } for (auto i = (0); i < (N); i++) { if (vis[i]) continue; edge[i].push_back(val[i] + N); } for (auto i = (0); i < (((int)(compress).size())); i++) { for (auto j = (freq[i]); j < (freq[i + 1]); j++) { if (vis[j]) continue; edge[i + N].push_back(j); } } for (auto i = (0); i < (N); i++) { if (vis[i]) continue; 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(); n = N; for (auto i = (0); i < (N); i++) { if (vis[i]) continue; if (arr[i] == i) { n--; 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++; } if (S < n) { cout << "-1\n"; return 0; } solve(); 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, s, a[200020], b[200020], p[200020]; bool cmp(int i, int j) { return a[i] < a[j]; } int rt[200020]; int findrt(int x) { if (rt[x] != x) rt[x] = findrt(rt[x]); return rt[x]; } int get(int x) { return lower_bound(b + 1, b + n + 1, x) - b; } map<int, int> S[200020]; int c[200020]; bool vis[200020]; int tot; vector<int> ans[200020]; int q[200020]; int main() { cin >> n >> s; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + n + 1); int cnt = n; for (int i = 1; i <= n; i++) if (a[i] == b[i]) q[i] = i, cnt--; if (cnt > s) { puts("-1"); return 0; } vector<int> v; for (int i = 1; i <= n; i++) if (!q[i]) v.push_back(i); sort(v.begin(), v.end(), cmp); for (int i = 1, j = 0; i <= n; i++) { if (q[i]) continue; q[i] = v[j]; j++; } for (int i = 1; i <= n; i++) p[q[i]] = i; for (int i = 1; i <= n; i++) assert(a[i] == b[p[i]]); for (int i = 1; i <= n; i++) rt[i] = i; for (int i = 1; i <= n; i++) { int l = findrt(i), r = findrt(p[i]); if (l == r) continue; rt[l] = r; } for (int i = 1; i <= n; i++) c[i] = get(a[i]), assert(b[c[i]] == a[i]); for (int i = 1; i <= n; i++) if (p[i] != i) S[c[i]][findrt(i)] = i; for (int i = 1; i <= n; i++) { if (S[i].empty()) continue; while (S[i].size() > 1) { map<int, int>::iterator it = S[i].begin(); int x = it->second; S[i].erase(it); it = S[i].begin(); int y = it->second; int fx = findrt(x), fy = findrt(y); assert(a[x] == a[y]); S[i][fy] = y; if (fx == fy) continue; swap(p[x], p[y]); rt[fx] = fy; } } for (int i = 1; i <= n; i++) { if (p[i] == i) { vis[i] = 1; continue; } if (vis[i]) continue; tot++; vector<int> &cur = ans[tot]; int now = i; while (1) { vis[now] = 1; cur.push_back(now); now = p[now]; if (now == i) break; } } cout << tot << endl; for (int i = 1; i <= tot; i++) { cout << ans[i].size() << endl; for (int x : ans[i]) printf("%d ", x); puts(""); } }