Search is not available for this dataset
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 + 20;
int a[maxn], q[maxn], num[maxn], par[maxn];
bool visited[maxn];
vector<int> ind[maxn], cycle[maxn];
int f(int v) { return (par[v] == -1) ? v : par[v] = f(par[v]); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(par, -1, sizeof par);
int n, s;
cin >> n >> s;
vector<int> tmp;
for (int i = 0; i < n; i++) cin >> a[i], tmp.push_back(a[i]);
sort(tmp.begin(), tmp.end());
tmp.resize(unique(tmp.begin(), tmp.end()) - tmp.begin());
for (int i = 0; i < n; i++)
a[i] = lower_bound(tmp.begin(), tmp.end(), a[i]) - tmp.begin();
for (int i = 0; i < n; i++) ind[a[i]].push_back(i);
int t = 0;
for (int i = 0; i < (int)tmp.size(); i++) {
int sz = ind[i].size();
vector<int> tmp2 = ind[i];
for (auto &x : ind[i])
if (t <= x && x < t + sz) {
a[x] = x;
visited[x] = 1;
x = 1e9;
}
sort(ind[i].begin(), ind[i].end());
while (!ind[i].empty() && ind[i].back() > n) ind[i].pop_back();
tmp2 = ind[i];
for (int j = t; j < t + sz; j++)
if (!visited[j]) a[tmp2.back()] = j, tmp2.pop_back();
t += sz;
}
t = 0;
memset(par, -1, sizeof par);
for (int i = 0; i < n; i++)
if (!visited[i]) {
while (!visited[i]) {
visited[i] = 1;
cycle[t].push_back(i);
i = a[i];
}
for (int j = 1; j < (int)cycle[t].size(); j++)
par[cycle[t][j]] = cycle[t][0];
t++;
}
for (int i = 0; i < (int)tmp.size(); i++) {
if (ind[i].empty()) continue;
int marja = ind[i].back();
ind[i].pop_back();
for (auto shit : ind[i]) {
if (f(shit) == f(marja)) continue;
par[f(shit)] = f(marja);
swap(a[shit], a[marja]);
}
}
memset(visited, 0, sizeof visited);
for (int i = 0; i < t; i++) cycle[i].clear();
t = 0;
int T = 0;
for (int i = 0; i < n; i++)
if (!visited[i]) {
int sz = 0;
while (!visited[i]) {
sz++;
cycle[t].push_back(i);
visited[i] = 1;
i = a[i];
}
if (sz != 1)
t++;
else
cycle[t].clear();
}
if (!t) return cout << 0 << endl, 0;
if (t == 1) {
if (s < (int)cycle[0].size()) return cout << -1 << endl, 0;
cout << 1 << endl;
cout << cycle[0].size() << endl;
for (auto x : cycle[0]) cout << x + 1 << " ";
cout << endl;
return 0;
}
for (int i = 0; i < t; i++) T += (int)cycle[i].size();
if (s >= T + t) {
cout << 2 << endl;
cout << T << endl;
for (int i = 0; i < t; i++)
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
cout << t << endl;
for (int i = 0; i < t; i++) cout << cycle[i][0] + 1 << " ";
cout << endl;
}
if (s < T) return cout << -1 << endl, 0;
s -= T;
if (s < 2) {
cout << t << endl;
for (int i = 0; i < t; i++) {
cout << cycle[i].size() << endl;
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
}
return 0;
}
cout << 2 + (t - s) << endl;
for (int i = s; i < t; i++) {
cout << cycle[i].size() << endl;
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
}
t = s;
T = 0;
for (int i = 0; i < t; i++) T += (int)cycle[i].size();
cout << T << endl;
for (int i = 0; i < t; i++)
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
cout << t << endl;
for (int i = 0; i < t; i++) cout << cycle[i][0] + 1 << " ";
cout << endl;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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);
printf("%d\n", q + 1);
int sm = 0;
for (int i = 1; i <= q + 1; ++i) {
printf("%d ", v[i][0]);
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');
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;
int v[300300];
int a[300300];
int b[300300];
set<int> pos[300300];
int nxt[300300];
int pai[300300];
int last[300300];
int find(int x) { return pai[x] = (x == pai[x]) ? x : find(pai[x]); }
void merge(int A, int B) {
A = find(A);
B = find(B);
pai[A] = B;
}
int mrk[300300];
int main() {
int n, s;
scanf("%d%d", &n, &s);
for (int i = 0; i < n; i++) {
scanf("%d", v + i);
b[i] = v[i];
a[i] = v[i];
}
sort(b, b + n);
map<int, int> mp;
mp[b[0]] = 1;
for (int i = 1; i < n; i++)
if (b[i] != b[i - 1]) mp[b[i]] = 1 + mp[b[i - 1]];
for (int i = 0; i < n; i++) {
v[i] = mp[v[i]];
a[i] = mp[a[i]];
}
sort(a, a + n);
int t = 0;
for (int i = 0; i < n; i++)
if (a[i] != v[i]) {
t++;
pos[a[i]].insert(i);
}
if (t > s) {
printf("-1\n");
return 0;
}
for (int i = 0; i < n; i++) pai[i] = i;
for (int i = 0; i < n; i++)
if (a[i] != v[i] && mrk[i] == 0) {
int u = i;
int st = 0;
do {
("%d ", u);
st++;
mrk[u] = 1;
if (pos[v[u]].size() == 0) assert(0);
int to = *pos[v[u]].begin();
pos[v[u]].erase(to);
merge(u, to);
nxt[u] = to;
u = to;
} while (u != i);
("\n");
}
memset(mrk, 0, sizeof(mrk));
memset(last, -1, sizeof(last));
for (int i = 0; i < n; i++) {
if (a[i] != v[i] && last[v[i]] != -1 && find(i) != find(last[v[i]])) {
int A = i, B = last[v[i]];
("swapp %d %d\n", A, B);
swap(nxt[A], nxt[B]);
merge(A, B);
}
if (a[i] != v[i]) last[v[i]] = i;
}
vector<vector<int> > ans;
for (int i = 0; i < n; i++)
if (a[i] != v[i] && !mrk[i]) {
vector<int> u;
int r = i;
while (!mrk[r]) {
u.push_back(r);
mrk[r] = 1;
r = nxt[r];
}
ans.push_back(u);
}
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); i++) {
printf("%d\n", ans[i].size());
for (int j = 0; j < ans[i].size(); j++) printf("%d ", 1 + ans[i][j]);
printf("\n");
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, 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);
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 long long maxn = 4e5 + 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 = 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]) {
long long tmp = i;
while (!mark[tmp]) {
mark[tmp] = 1;
Merge(tmp, i);
tmp = f[tmp];
}
while (mark[tmp]) {
mark[tmp] = 0;
tmp = f[tmp];
}
}
}
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>
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=a; i<=b; i++)
#define per(i,a,b) for (register int i=a; i>=b; i--)
using namespace std;
typedef vector<int> Vi;
inline void read(int &x) {
x=0; char c=getchar(); int f=1;
while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
const int N = 804000;
int n,s,A[N],f[N];bool vis[N];
struct ele{int v,ind;}a[N];
bool cmp(ele a, ele b){return a.v<b.v;}
inline int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
int head[N],to[N<<1],nxt[N<<1],edgenum;
void add(int u, int v){//printf("add %d %d\n",u,v);
to[++edgenum]=v;nxt[edgenum]=head[u];head[u]=edgenum;
}
int q;Vi ans[N];
inline void dfs(int u){
while(head[u]){
int i=head[u];head[u]=nxt[i];dfs(to[i]);if(u<=n)ans[q].pb(u);
}
}
int main() {
read(n);read(s);rep(i,1,n)read(a[i].v),a[i].ind=i,A[i]=a[i].v;
sort(a+1,a+n+1,cmp);rep(i,1,n)vis[i]=A[i]==a[i].v;
rep(i,1,n)f[i]=i;rep(i,1,n-1)if(a[i].v==a[i+1].v)f[find(i+1)]=find(i);
rep(i,1,n)if(!vis[i])add(n+find(i),i);
rep(i,1,n)if(!vis[a[i].ind])add(a[i].ind,n+find(i));
rep(i,1,n)if(head[i])q++,ans[q].clear(),dfs(i),reverse(ans[q].begin(),ans[q].end()),q-=SZ(ans[q])<=1;
int tot=0;rep(i,1,q)tot+=SZ(ans[i]);if(tot>s){puts("-1");return 0;}
printf("%d\n",q);rep(i,1,q){
int len=SZ(ans[i]);printf("%d ",len);
rep(j,0,len-1)printf("%d ",ans[i][j]);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 = 100010;
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;
}
printf("\n");
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[i]) {
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) {
int ta = pre[c0], tb = nxt[c0], tc = pre[c1], td = nxt[c1];
nxt[ta] = c1, pre[c1] = ta, nxt[c1] = tb, pre[tb] = c1;
nxt[tc] = c0, pre[c0] = tc, nxt[c0] = td, pre[td] = c0;
f[fa] = fb;
}
}
}
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 = tmp + 1; i <= g1; 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 = 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++;
if (c[w] != a[x]) break;
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 = (s); i >= (1); 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 T, class U>
void ckmin(T &a, U b) {
if (a > b) a = b;
}
template <class T, class U>
void ckmax(T &a, U b) {
if (a < b) a = b;
}
const int MAXN = 400013;
int N, S, M, ans, n;
int val[MAXN], arr[MAXN], sorted[MAXN];
vector<int> moves[MAXN];
vector<int> cyc[MAXN];
bitset<MAXN> vis;
vector<int> compress;
int freq[MAXN];
pair<int, int> range[MAXN];
vector<int> edge[MAXN];
vector<int> tour;
int ind[MAXN];
int indexof(vector<int> &v, int x) {
return upper_bound((v).begin(), (v).end(), x) - v.begin() - 1;
}
void dfs(int u) {
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 maxn = 2e5 + 20;
int a[maxn], q[maxn], num[maxn], par[maxn];
bool visited[maxn];
vector<int> ind[maxn], cycle[maxn];
int f(int v) { return (par[v] == -1) ? v : par[v] = f(par[v]); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(par, -1, sizeof par);
int n, s;
cin >> n >> s;
vector<int> tmp;
for (int i = 0; i < n; i++) cin >> a[i], tmp.push_back(a[i]);
sort(tmp.begin(), tmp.end());
tmp.resize(unique(tmp.begin(), tmp.end()) - tmp.begin());
for (int i = 0; i < n; i++)
a[i] = lower_bound(tmp.begin(), tmp.end(), a[i]) - tmp.begin();
for (int i = 0; i < n; i++) ind[a[i]].push_back(i);
int t = 0;
for (int i = 0; i < (int)tmp.size(); i++) {
int sz = ind[i].size();
vector<int> tmp2 = ind[i];
for (auto &x : ind[i])
if (t <= x && x < t + sz) {
a[x] = x;
visited[x] = 1;
x = 1e9;
}
sort(ind[i].begin(), ind[i].end());
while (!ind[i].empty() && ind[i].back() > n) ind[i].pop_back();
tmp2 = ind[i];
for (int j = t; j < t + sz; j++)
if (!visited[j]) a[tmp2.back()] = j, tmp2.pop_back();
t += sz;
}
t = 0;
memset(par, -1, sizeof par);
for (int i = 0; i < n; i++)
if (!visited[i]) {
while (!visited[i]) {
visited[i] = 1;
cycle[t].push_back(i);
i = a[i];
}
for (int j = 1; j < (int)cycle[t].size(); j++)
par[cycle[t][j]] = cycle[t][0];
t++;
}
for (int i = 0; i < (int)tmp.size(); i++) {
if (ind[i].empty()) continue;
int marja = ind[i].back();
ind[i].pop_back();
for (auto shit : ind[i]) {
if (f(shit) == f(marja)) continue;
par[f(shit)] = f(marja);
swap(a[shit], a[marja]);
}
}
memset(visited, 0, sizeof visited);
for (int i = 0; i < t; i++) cycle[i].clear();
t = 0;
int T = 0;
for (int i = 0; i < n; i++)
if (!visited[i]) {
int sz = 0;
while (!visited[i]) {
sz++;
cycle[t].push_back(i);
visited[i] = 1;
i = a[i];
}
if (sz != 1)
t++;
else
cycle[t].clear();
}
if (!t) return cout << 0 << endl, 0;
if (t == 1) {
if (s < (int)cycle[0].size()) return cout << -1 << endl, 0;
cout << 1 << endl;
cout << cycle[0].size() << endl;
for (auto x : cycle[0]) cout << x + 1 << " ";
cout << endl;
return 0;
}
for (int i = 0; i < t; i++) T += (int)cycle[i].size();
if (s >= T + t) {
cout << 2 << endl;
cout << T << endl;
for (int i = 0; i < t; i++)
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
cout << t << endl;
for (int i = 0; i < t; i++) cout << cycle[i][0] + 1 << " ";
cout << endl;
}
if (s < T) return cout << -1 << endl, 0;
cout << t << endl;
for (int i = 0; i < t; i++) {
cout << cycle[i].size() << endl;
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int max_n = 222, mod = 1000000007;
int n, p[max_n], to[max_n][2];
string s;
void get_prefix_function(const string &s, int p[]) {
p[0] = 0;
for (int i = 1; i < s.length(); ++i) {
p[i] = p[i - 1];
while (p[i] > 0 && s[i] != s[p[i]]) {
p[i] = p[p[i] - 1];
}
if (s[i] == s[p[i]]) {
++p[i];
}
}
}
int calc_all() {
int dp[max_n][max_n];
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 0; i < 2 * n; ++i) {
for (int bal = 0; bal <= i; ++bal) {
dp[i + 1][bal + 1] += dp[i][bal];
dp[i + 1][bal + 1] %= mod;
if (bal) {
dp[i + 1][bal - 1] += dp[i][bal];
dp[i + 1][bal - 1] %= mod;
}
}
}
return dp[2 * n][0];
}
int calc_bad() {
int dp[max_n][max_n][max_n];
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for (int i = 0; i < 2 * n; ++i) {
for (int bal = 0; bal <= i; ++bal) {
for (int cnt = 0; cnt < s.length(); ++cnt) {
for (int tp = 0; tp < 2; ++tp) {
int nbal = bal;
if (tp == 0) {
++nbal;
} else {
--nbal;
}
if (nbal >= 0) {
dp[i + 1][nbal][to[cnt][tp]] += dp[i][bal][cnt];
dp[i + 1][nbal][to[cnt][tp]] %= mod;
}
}
}
}
}
int res = 0;
for (int cnt = 0; cnt < s.length(); ++cnt) {
res += dp[2 * n][0][cnt];
res %= mod;
}
return res;
}
int main() {
cin >> n >> s;
get_prefix_function(s, p);
for (int cnt = 0; cnt < s.length(); ++cnt) {
for (int tp = 0; tp < 2; ++tp) {
char c = '(' + tp;
int cur = cnt;
while (cur && s[cur] != c) {
cur = p[cur - 1];
}
if (s[cur] == c) {
++cur;
}
to[cnt][tp] = cur;
}
}
int all = calc_all();
int bad = calc_bad();
int ans = (all + mod - bad) % mod;
cout << ans << 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;
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(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 inf = 1e9;
const long long Inf = 1e18;
const int N = 2e5 + 10;
const int mod = 0;
int gi() {
int x = 0, o = 1;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
template <typename T>
bool chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
};
template <typename T>
bool chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
};
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); }
void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); }
int n, s, a[N];
bool vis[N];
map<int, int> cnt;
map<int, vector<int>> E;
vector<int> cycle;
vector<vector<int>> ans;
void dfs(int u) {
while (!E[u].empty()) {
int v = E[u].back();
E[u].pop_back();
vis[v] = 1;
dfs(a[v]);
cycle.push_back(v);
}
}
int main() {
cin >> n >> s;
for (int i = 1; i <= n; i++) a[i] = gi(), cnt[a[i]]++;
int now = 1;
for (pair<int, int> x : cnt) {
for (int j = now; j < now + x.second; j++)
if (a[j] != x.first) E[x.first].push_back(j);
now += x.second;
}
for (int i = 1; i <= n; i++)
if (!vis[i]) {
cycle.clear(), dfs(a[i]);
if (!cycle.empty())
reverse(cycle.begin(), cycle.end()), ans.push_back(cycle),
s -= int(cycle.size());
}
if (s < 0) return puts("-1"), 0;
if (s <= 2 || int(ans.size()) <= 2) {
printf("%d\n", int(ans.size()));
for (vector<int> cycle : ans) {
printf("%d\n", int(cycle.size()));
for (int x : cycle) printf("%d ", x);
puts("");
}
} else {
s = min(s, int(ans.size()));
printf("%d\n", int(ans.size()) - s + 2);
while (int(ans.size()) > s) {
vector<int> cycle = ans.back();
ans.pop_back();
printf("%d\n", int(cycle.size()));
for (int x : cycle) printf("%d ", x);
puts("");
}
printf("%d\n", s);
int sum = 0;
for (auto cycle : ans)
printf("%d ", cycle.back()), sum += int(cycle.size());
puts("");
printf("%d\n", sum);
for (auto cycle : ans)
for (int x : cycle) printf("%d ", x);
puts("");
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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]++;
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 = 200005;
int n, s, ct, tmp_n, ans_ct, a[Maxn], b[Maxn], fa[Maxn], pos[Maxn], ord[Maxn],
bel[Maxn], Pos[Maxn];
vector<int> spec, Ve[Maxn];
bool vis[Maxn];
void dfs(int u) {
if (vis[u]) return;
bel[u] = ct, vis[u] = true, dfs(ord[u]);
}
void dfs2(int u) {
if (vis[u]) return;
Ve[ans_ct].push_back(u), vis[u] = true, dfs2(pos[u]);
}
int get_fa(int x) { return x == fa[x] ? x : fa[x] = get_fa(fa[x]); }
int main() {
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
sort(b + 1, b + 1 + n);
for (int i = 1; i <= n; i++)
if (a[i] != b[i]) a[++tmp_n] = a[i], Pos[tmp_n] = i;
n = tmp_n;
if (s < n) {
puts("-1");
return 0;
}
s -= n;
for (int i = 1; i <= n; i++) ord[i] = i;
sort(ord + 1, ord + 1 + n, [](int x, int y) { return a[x] < a[y]; });
for (int i = 1; i <= n; i++) pos[ord[i]] = i;
for (int i = 1; i <= n; i++)
if (!vis[i]) ct++, fa[ct] = ct, dfs(i);
int las = 1;
for (int i = 2; i <= n + 1; i++)
if (a[ord[i]] != a[ord[i - 1]]) {
for (int j = las; j < i; j++)
if (get_fa(bel[ord[las]]) != get_fa(bel[ord[j]]))
fa[bel[ord[j]]] = get_fa(bel[ord[las]]), swap(ord[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;
int n, k, tot, x, a[1000000], b[1000000], st[1000000], ed[1000000], nt[1000000];
vector<int> wr[1000000];
bool v[1000000];
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 (!st[b[i]])
st[b[i]] = i;
else
nt[ed[b[i]]] = i;
ed[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 = st[a[x]]) {
st[b[x]] = nt[x];
v[x] = 1;
wr[tot].push_back(x);
if (a[x] == b[i]) break;
}
}
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>
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) continue;
int l = S[a[i]];
int fl = findrt(l), fr = findrt(i);
assert(fl == fr);
}
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>
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 + (spec.size() > 1));
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() > 1) {
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;
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[f[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);
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 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();
printf("%d\n", cycles.size());
for (const vector<int> &cycle : cycles) {
printf("%d\n", cycle.size());
for (int pos : cycle) {
printf("%d ", pos + 1);
}
printf("\n");
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N, S;
int A[200020], B[200020];
vector<int> vx;
set<pair<int, int> > E[200020];
void dfs(int x, vector<int> &R) {
while (1) {
auto it = E[x].begin();
if (it == E[x].end()) return;
pair<int, int> save = *it;
E[x].erase(it);
dfs(save.second, R);
R.push_back(save.first);
}
}
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;
}
vector<vector<int> > ans;
for (int i = 1; i <= N; i++)
if (B[i] != A[i]) {
E[A[i]].insert(pair<int, int>(i, B[i]));
}
int L = (int)vx.size();
for (int i = 1; i <= L; i++)
if ((int)E[i].size()) {
vector<int> v;
dfs(i, v);
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>
#pragma GCC optimize("O3")
using namespace std;
const long long mod7 = 1000000007LL;
inline long long getint() {
long long _x = 0, _tmp = 1;
char _tc = getchar();
while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
if (_tc == '-') _tc = getchar(), _tmp = -1;
while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
return _x * _tmp;
}
inline long long add(long long _x, long long _y, long long _mod = mod7) {
_x += _y;
return _x >= _mod ? _x - _mod : _x;
}
inline long long sub(long long _x, long long _y, long long _mod = mod7) {
_x -= _y;
return _x < 0 ? _x + _mod : _x;
}
inline long long mul(long long _x, long long _y, long long _mod = mod7) {
_x *= _y;
return _x >= _mod ? _x % _mod : _x;
}
long long mypow(long long _a, long long _x, long long _mod) {
if (_x == 0) return 1LL;
long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod);
if (_x & 1) _ret = mul(_ret, _a, _mod);
return _ret;
}
long long mymul(long long _a, long long _x, long long _mod) {
if (_x == 0) return 0LL;
long long _ret = mymul(add(_a, _a, _mod), _x >> 1, _mod);
if (_x & 1) _ret = add(_ret, _a, _mod);
return _ret;
}
void sleep(double sec = 1021) {
clock_t s = clock();
while (clock() - s < CLOCKS_PER_SEC * sec)
;
}
int __ = 1, _cs;
const int N = 202020;
int n, s, a[N], sa[N];
void build() {}
void no() {
puts("-1");
exit(0);
}
void init() {
n = getint();
s = getint();
for (int i = 1; i <= n; i++) sa[i] = a[i] = getint();
sort(sa + 1, sa + n + 1);
}
vector<vector<int>> ans;
unordered_map<int, vector<int>> cand;
void solve() {
int need_chg = 0;
for (int i = 1; i <= n; i++)
if (a[i] != sa[i]) {
need_chg++;
cand[sa[i]].push_back(i);
}
if (need_chg > s) no();
for (int i = 1; i <= n; i++) {
if (a[i] == sa[i] or a[i] == -1) continue;
vector<int> vv;
vv.push_back(i);
int cur = a[i];
while (true) {
int idx = cand[cur].back();
cand[cur].pop_back();
if (idx == i) break;
vv.push_back(idx);
cur = a[idx];
}
for (int idx : vv) a[idx] = -1;
ans.push_back(vv);
}
printf("%d\n", (int)ans.size());
for (auto i : ans) {
printf("%d\n", (int)i.size());
for (auto j : i) printf("%d ", j);
puts("");
}
}
int main() {
build();
while (__--) {
init();
solve();
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int a[200010], b[200010], nxt[200010], cnt, n, s;
void solve(vector<int> &v, int i, int &j) {
while (a[j] == a[i]) j++;
if (j >= n) return;
if (b[j] == a[i]) {
int j0 = j++;
swap(a[i], a[j0]);
while (b[j0] == b[j]) {
if (b[j] != a[j]) {
v.push_back(j);
int j1 = j++;
solve(v, j1, nxt[lower_bound(b, b + n, a[j1]) - b]);
} else
j++;
}
v.push_back(j0);
solve(v, i, nxt[lower_bound(b, b + n, a[i]) - b]);
}
}
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
b[i] = a[i];
nxt[i] = i;
}
sort(b, b + n);
a[n] = b[n] = -1;
cnt = 0;
for (int i = 0; i < n; i++) {
cnt += (a[i] != b[i]);
}
vector<vector<int> > ans;
for (int i = 0; i < n; i++) {
if (b[i] == a[i]) continue;
ans.push_back(vector<int>(1, i));
nxt[lower_bound(b, b + n, b[i]) - b] = i + 1;
solve(ans.back(), i, nxt[lower_bound(b, b + n, a[i]) - b]);
}
if (cnt > s) {
printf("-1\n");
} else if (ans.size() < 3 || s - cnt < 3) {
printf("%d\n", ans.size());
while (ans.size() > 0) {
auto &v = ans.back();
printf("%d\n", v.size());
for (int i : v) {
printf("%d ", i + 1);
}
printf("\n");
ans.pop_back();
}
} else {
int x = max((int)ans.size() - s + cnt, 0), size = 0;
for (int i = x; i < ans.size(); i++) {
size += ans[i].size();
}
printf("%d\n%d\n", x + 2, size);
for (int i = x; i < ans.size(); i++) {
auto &v = ans[i];
for (int i : v) {
printf("%d ", i + 1);
}
}
printf("\n%d\n", ans.size() - x);
for (int i = ans.size() - 1; i >= x; i--) {
printf("%d ", ans[i][0] + 1);
}
printf("\n");
ans.resize(x);
while (ans.size() > 0) {
auto &v = ans.back();
printf("%d\n", v.size());
for (int i : v) {
printf("%d ", i + 1);
}
printf("\n");
ans.pop_back();
}
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, k;
int a[200010];
int b[200010];
vector<pair<int, int> > graf[200010];
vector<int> tura;
vector<int> sz;
vector<vector<int> > sol;
vector<vector<int> > rj;
vector<int> r;
int bio[200010];
void dfs(int x) {
bio[x] = 1;
while (!graf[x].empty()) {
int sus = graf[x][(int)graf[x].size() - 1].first;
int br = graf[x][(int)graf[x].size() - 1].second;
graf[x].pop_back();
dfs(sus);
tura.push_back(br);
}
}
void sazimanje() {
for (int i = 1; i <= n; i++) sz.push_back(a[i]);
sort(sz.begin(), sz.end());
sz.erase(unique(sz.begin(), sz.end()), sz.end());
for (int i = 1; i <= n; i++) {
a[i] = lower_bound(sz.begin(), sz.end(), a[i]) - sz.begin();
b[i - 1] = a[i];
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
sazimanje();
sort(b, b + n);
int cnt = 0;
for (int i = n - 1; i >= 0; i--) {
b[i + 1] = b[i];
b[i] = 0;
if (a[i + 1] != b[i + 1]) {
cnt++;
graf[a[i + 1]].push_back(make_pair(b[i + 1], i + 1));
}
}
if (cnt > k) {
printf("-1");
return 0;
}
for (int i = 1; i <= n; i++) {
if (bio[i] == 0) {
tura.clear();
dfs(i);
reverse(tura.begin(), tura.end());
if (tura.size() >= 1) sol.push_back(tura);
}
}
int sad = min((int)sol.size(), k - cnt);
if (sad > 1) {
for (int i = sad - 1; i >= 0; i--) {
r.push_back(sol[i].back());
}
rj.push_back(r);
r.clear();
for (int i = 0; i < sad; i++) {
for (int j = 0; j < sol[i].size(); j++) {
r.push_back(sol[i][j]);
}
}
rj.push_back(r);
for (int i = sad; i < sol.size(); i++) {
rj.push_back(sol[i]);
}
sol = rj;
}
printf("%d\n", sol.size());
for (int i = 0; i < sol.size(); i++) {
printf("%d\n", sol[i].size());
reverse(sol[i].begin(), sol[i].end());
for (int j = 0; j < sol[i].size(); j++) printf("%d ", sol[i][j]);
printf("\n");
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int Maxn = 200005;
int n, s, ct, tmp_n, ans_ct, a[Maxn], b[Maxn], fa[Maxn], pos[Maxn], ord[Maxn],
bel[Maxn], Pos[Maxn];
vector<int> spec, Ve[Maxn];
bool vis[Maxn];
void dfs(int u) {
if (vis[u]) return;
bel[u] = ct, vis[u] = true, dfs(ord[u]);
}
void dfs2(int u) {
if (vis[u]) return;
Ve[ans_ct].push_back(u), bel[u] = ct, vis[u] = true, dfs2(ord[u]);
}
int get_fa(int x) { return x == fa[x] ? x : fa[x] = get_fa(fa[x]); }
int main() {
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
sort(b + 1, b + 1 + n);
for (int i = 1; i <= n; i++)
if (a[i] != b[i]) a[++tmp_n] = a[i], Pos[tmp_n] = i;
n = tmp_n;
if (s < n) {
puts("-1");
return 0;
}
s -= n;
for (int i = 1; i <= n; i++) ord[i] = i;
sort(ord + 1, ord + 1 + n, [](int x, int y) { return a[x] < a[y]; });
for (int i = 1; i <= n; i++) pos[ord[i]] = i;
a[n + 1] = -1;
for (int i = 1; i <= n; i++)
if (!vis[i]) ct++, fa[ct] = ct, dfs(i);
int las = 1;
for (int i = 2; i <= n + 1; i++)
if (a[ord[i]] != a[ord[i - 1]]) {
for (int j = las; j < i; j++)
if (get_fa(bel[ord[las]]) != get_fa(bel[ord[j]]))
fa[bel[j]] = get_fa(bel[las]), swap(ord[pos[j]], ord[pos[las]]);
las = i;
}
memset(vis, 0, sizeof(bool[n + 1]));
for (int i = 1; i <= n; i++)
if (!vis[i]) {
ct++;
if (ct == 1 || ct > s) ++ans_ct;
if (ct <= s) spec.push_back(i);
dfs2(i);
}
printf("%d\n", ans_ct + (bool)spec.size());
if (ans_ct) {
printf("%d\n", (int)Ve[1].size());
for (vector<int>::iterator it = Ve[1].begin(); it != Ve[1].end(); it++)
printf("%d ", Pos[*it]);
}
if (spec.size()) {
printf("%d\n", (int)spec.size());
for (vector<int>::reverse_iterator it = spec.rbegin(); it != spec.rend();
it++)
printf("%d ", Pos[*it]);
}
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]);
}
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;
int n, s, a[N], aa[N], g[N], p[N], i, j;
int gfa(int x) { return g[x] == x ? x : g[x] = gfa(g[x]); }
bool bb[N], vi[N];
set<int> S;
unordered_map<int, int> be, en;
unordered_map<int, vector<int>> mp;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for (i = 1; i <= n; ++i) cin >> a[i];
memcpy(aa + 1, a + 1, n << 2);
sort(aa + 1, aa + n + 1);
for (i = 1; i <= n; ++i) {
bb[i] = a[i] == aa[i], g[i] = i;
if (!bb[i]) S.insert(i);
}
int ss = S.size();
if (ss > s) {
cout << -1 << endl;
return 0;
}
for (i = 1; i <= n; ++i) en[aa[i]] = i;
for (i = n; i; --i) be[aa[i]] = i;
for (i = 1; i <= n; ++i)
if (!bb[i])
p[i] = *S.lower_bound(be[a[i]]), S.erase(p[i]), g[gfa(p[i])] = gfa(i);
for (i = 1; i <= n; ++i)
if (!bb[i])
mp[a[i]].push_back(i);
else
p[i] = i;
for (auto u : mp) {
auto v = u.second;
for (i = 1; i < v.size(); ++i)
if (gfa(v[i]) != gfa(v[0]))
g[gfa(v[i])] = gfa(v[0]), swap(p[v[i]], p[v[0]]);
}
vector<vector<int>> ans;
for (i = 1; i <= n; ++i)
if (p[i] != i && !vi[i]) {
vector<int> ve;
for (j = i; vi[j] = 1, ve.push_back(j), p[j] != i; j = p[j])
;
ans.push_back(ve);
}
if (ss + 3 <= s) {
vector<int> v1, v2, v3;
for (i = 1; ss + i <= s && !ans.empty(); ++i) {
v3 = ans.back();
ans.pop_back();
v1.insert(v1.end(), v3.begin(), v3.end());
v2.push_back(v3[0]);
}
reverse(v2.begin(), v2.end());
ans.push_back(v1);
ans.push_back(v2);
}
cout << ans.size() << '\n';
for (auto u : ans) {
cout << u.size() << '\n';
for (int x : u) cout << x << ' ';
cout << '\n';
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 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;
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;
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)));
if (ans == 3555) {
cout << M << ' ' << S << ' ' << n << endl;
return 0;
}
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 | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.util.Random;
import java.util.ArrayList;
import java.io.UncheckedIOException;
import java.util.List;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
ECycleSort solver = new ECycleSort();
solver.solve(1, in, out);
out.close();
}
}
static class ECycleSort {
int n;
int[] a;
public void solve(int testNumber, FastInput in, FastOutput out) {
n = in.readInt();
int s = in.readInt();
a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.readInt();
}
int[] b = a.clone();
Randomized.shuffle(b);
Arrays.sort(b);
int[] same = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) {
same[i] = 1;
}
}
for (int x : same) {
sum += x;
}
if (n - sum > s) {
out.println(-1);
return;
}
IntegerList permList = new IntegerList(n);
for (int i = 0; i < n; i++) {
if (same[i] == 0) {
permList.add(i);
}
}
int[] perm = permList.toArray();
CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length);
DSU dsu = new DSU(n);
for (int i = 0; i < perm.length; i++) {
int from = perm[i];
int to = permList.get(i);
dsu.merge(from, to);
}
for (int i = 1; i < perm.length; i++) {
if (a[perm[i]] != a[perm[i - 1]]) {
continue;
}
if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) {
continue;
}
dsu.merge(perm[i], perm[i - 1]);
SequenceUtils.swap(perm, i, i - 1);
}
int remain = s - (n - sum) - 1;
IntegerList first = new IntegerList();
if (perm.length > 0) {
first.add(perm[0]);
for (int i = 1; remain > 0 && i < perm.length; i++) {
if (dsu.find(perm[i - 1]) != dsu.find(perm[i])) {
remain--;
first.add(perm[i]);
}
}
int last = first.get(0);
for (int i = 1; i < first.size(); i++) {
int y = first.get(i);
a[y] = a[last];
last = y;
}
a[first.get(0)] = a[last];
}
List<IntegerList> circles = new ArrayList<>();
if (first.size() > 1) {
circles.add(first);
}
circles.addAll(solve());
out.println(circles.size());
for (IntegerList list : circles) {
out.println(list.size());
for (int i = 0; i < list.size(); i++) {
out.append(list.get(i) + 1).append(' ');
}
out.println();
}
}
public List<IntegerList> solve() {
int[] b = a.clone();
Randomized.shuffle(b);
Arrays.sort(b);
int[] same = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) {
same[i] = 1;
}
}
for (int x : same) {
sum += x;
}
IntegerList permList = new IntegerList(n);
for (int i = 0; i < n; i++) {
if (same[i] == 0) {
permList.add(i);
}
}
int[] perm = permList.toArray();
CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length);
DSU dsu = new DSU(n);
for (int i = 0; i < perm.length; i++) {
int from = perm[i];
int to = permList.get(i);
dsu.merge(from, to);
}
for (int i = 1; i < perm.length; i++) {
if (a[perm[i]] != a[perm[i - 1]]) {
continue;
}
if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) {
continue;
}
dsu.merge(perm[i], perm[i - 1]);
SequenceUtils.swap(perm, i, i - 1);
}
int[] index = new int[n];
for (int i = 0; i < n; i++) {
if (same[i] == 1) {
index[i] = i;
}
}
for (int i = 0; i < perm.length; i++) {
index[perm[i]] = permList.get(i);
}
PermutationUtils.PowerPermutation pp = new PermutationUtils.PowerPermutation(index);
List<IntegerList> circles = pp.extractCircles(2);
return circles;
}
}
static class DSU {
int[] p;
int[] rank;
public DSU(int n) {
p = new int[n];
rank = new int[n];
reset();
}
public void reset() {
for (int i = 0; i < p.length; i++) {
p[i] = i;
rank[i] = 0;
}
}
public int find(int a) {
return p[a] == p[p[a]] ? p[a] : (p[a] = find(p[a]));
}
public void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b) {
return;
}
if (rank[a] == rank[b]) {
rank[a]++;
}
if (rank[a] > rank[b]) {
p[b] = a;
} else {
p[a] = b;
}
}
}
static class Randomized {
private static Random random = new Random(0);
public static void shuffle(int[] data) {
shuffle(data, 0, data.length - 1);
}
public static void shuffle(int[] data, int from, int to) {
to--;
for (int i = from; i <= to; i++) {
int s = nextInt(i, to);
int tmp = data[i];
data[i] = data[s];
data[s] = tmp;
}
}
public static int nextInt(int l, int r) {
return random.nextInt(r - l + 1) + l;
}
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 20];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
}
static class SequenceUtils {
public static void swap(int[] data, int i, int j) {
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) {
if ((ar - al) != (br - bl)) {
return false;
}
for (int i = al, j = bl; i <= ar; i++, j++) {
if (a[i] != b[j]) {
return false;
}
}
return true;
}
}
static class FastOutput implements AutoCloseable, Closeable, Appendable {
private StringBuilder cache = new StringBuilder(10 << 20);
private final Writer os;
public FastOutput append(CharSequence csq) {
cache.append(csq);
return this;
}
public FastOutput append(CharSequence csq, int start, int end) {
cache.append(csq, start, end);
return this;
}
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput append(char c) {
cache.append(c);
return this;
}
public FastOutput append(int c) {
cache.append(c);
return this;
}
public FastOutput println(int c) {
cache.append(c);
println();
return this;
}
public FastOutput println() {
cache.append(System.lineSeparator());
return this;
}
public FastOutput flush() {
try {
os.append(cache);
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class IntegerList implements Cloneable {
private int size;
private int cap;
private int[] data;
private static final int[] EMPTY = new int[0];
public IntegerList(int cap) {
this.cap = cap;
if (cap == 0) {
data = EMPTY;
} else {
data = new int[cap];
}
}
public IntegerList(IntegerList list) {
this.size = list.size;
this.cap = list.cap;
this.data = Arrays.copyOf(list.data, size);
}
public IntegerList() {
this(0);
}
public void ensureSpace(int req) {
if (req > cap) {
while (cap < req) {
cap = Math.max(cap + 10, 2 * cap);
}
data = Arrays.copyOf(data, cap);
}
}
private void checkRange(int i) {
if (i < 0 || i >= size) {
throw new ArrayIndexOutOfBoundsException();
}
}
public int get(int i) {
checkRange(i);
return data[i];
}
public void add(int x) {
ensureSpace(size + 1);
data[size++] = x;
}
public void addAll(int[] x, int offset, int len) {
ensureSpace(size + len);
System.arraycopy(x, offset, data, size, len);
size += len;
}
public void addAll(IntegerList list) {
addAll(list.data, 0, list.size);
}
public int size() {
return size;
}
public int[] toArray() {
return Arrays.copyOf(data, size);
}
public String toString() {
return Arrays.toString(toArray());
}
public boolean equals(Object obj) {
if (!(obj instanceof IntegerList)) {
return false;
}
IntegerList other = (IntegerList) obj;
return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1);
}
public int hashCode() {
int h = 1;
for (int i = 0; i < size; i++) {
h = h * 31 + Integer.hashCode(data[i]);
}
return h;
}
public IntegerList clone() {
IntegerList ans = new IntegerList(size);
ans.addAll(this);
return ans;
}
}
static class DigitUtils {
private DigitUtils() {
}
public static int mod(int x, int mod) {
x %= mod;
if (x < 0) {
x += mod;
}
return x;
}
}
static interface IntComparator {
public int compare(int a, int b);
}
static class PermutationUtils {
private static final long[] PERMUTATION_CNT = new long[21];
static {
PERMUTATION_CNT[0] = 1;
for (int i = 1; i <= 20; i++) {
PERMUTATION_CNT[i] = PERMUTATION_CNT[i - 1] * i;
}
}
public static class PowerPermutation {
int[] g;
int[] idx;
int[] l;
int[] r;
int n;
public List<IntegerList> extractCircles(int threshold) {
List<IntegerList> ans = new ArrayList<>(n);
for (int i = 0; i < n; i = r[i] + 1) {
int size = r[i] - l[i] + 1;
if (size < threshold) {
continue;
}
IntegerList list = new IntegerList(r[i] - l[i] + 1);
for (int j = l[i]; j <= r[i]; j++) {
list.add(g[j]);
}
ans.add(list);
}
return ans;
}
public PowerPermutation(int[] p) {
this(p, p.length);
}
public PowerPermutation(int[] p, int len) {
n = len;
boolean[] visit = new boolean[n];
g = new int[n];
l = new int[n];
r = new int[n];
idx = new int[n];
int wpos = 0;
for (int i = 0; i < n; i++) {
int val = p[i];
if (visit[val]) {
continue;
}
visit[val] = true;
g[wpos] = val;
l[wpos] = wpos;
idx[val] = wpos;
wpos++;
while (true) {
int x = p[g[wpos - 1]];
if (visit[x]) {
break;
}
visit[x] = true;
g[wpos] = x;
l[wpos] = l[wpos - 1];
idx[x] = wpos;
wpos++;
}
for (int j = l[wpos - 1]; j < wpos; j++) {
r[j] = wpos - 1;
}
}
}
public int apply(int x, int p) {
int i = idx[x];
int dist = DigitUtils.mod((i - l[i]) + p, r[i] - l[i] + 1);
return g[dist + l[i]];
}
public String toString() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < n; i++) {
builder.append(apply(i, 1)).append(' ');
}
return builder.toString();
}
}
}
static class CompareUtils {
private static final int THRESHOLD = 4;
private CompareUtils() {
}
public static <T> void insertSort(int[] data, IntComparator cmp, int l, int r) {
for (int i = l + 1; i <= r; i++) {
int j = i;
int val = data[i];
while (j > l && cmp.compare(data[j - 1], val) > 0) {
data[j] = data[j - 1];
j--;
}
data[j] = val;
}
}
public static void quickSort(int[] data, IntComparator cmp, int f, int t) {
if (t - f <= THRESHOLD) {
insertSort(data, cmp, f, t - 1);
return;
}
SequenceUtils.swap(data, f, Randomized.nextInt(f, t - 1));
int l = f;
int r = t;
int m = l + 1;
while (m < r) {
int c = cmp.compare(data[m], data[l]);
if (c == 0) {
m++;
} else if (c < 0) {
SequenceUtils.swap(data, l, m);
l++;
m++;
} else {
SequenceUtils.swap(data, m, --r);
}
}
quickSort(data, cmp, f, l);
quickSort(data, cmp, m, t);
}
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace ::std;
const long long maxn = 2e5 + 500;
const long long mod = 1e9 + 7;
const long long inf = 2e9 + 500;
long long a[maxn];
long long sorta[maxn];
long long f[maxn];
void comp(long long n) {
vector<long long> vec;
for (long long i = 1; i <= n; i++) {
vec.push_back(a[i]);
}
sort(vec.begin(), vec.end());
for (long long i = 1; i <= n; i++) {
a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin() + 1;
}
}
long long sar[maxn];
long long tah[maxn];
vector<long long> out[maxn];
vector<long long> tor[maxn];
long long fir[maxn];
vector<long long> ras[maxn];
bool vis[maxn];
long long cnt = 0;
void dfs(long long a) {
while (out[a].size()) {
long long e = out[a].back();
out[a].pop_back();
if (vis[e] == 0) {
vis[e] = 1;
dfs(tah[e]);
}
}
tor[cnt].push_back(a);
}
vector<long long> ans[maxn];
long long anscnt = 0;
void dfs_a(long long a) {
vis[a] = 1;
ans[anscnt].push_back(a);
if (vis[f[a]] == 0) {
dfs_a(f[a]);
}
}
void hal(long long n) {
fill(vis, vis + maxn, 0);
for (long long i = 1; i <= n; i++) {
if (vis[i] == 0 && f[i] != i) {
dfs_a(i);
anscnt++;
}
}
}
void pri() {
cout << anscnt << endl;
for (long long i = 0; i < anscnt; i++) {
cout << ans[i].size() << endl;
for (auto r : ans[i]) {
cout << r << ' ';
}
cout << endl;
}
exit(0);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, s;
cin >> n >> s;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
comp(n);
for (long long i = 1; i <= n; i++) {
sorta[i] = a[i];
}
sort(sorta + 1, sorta + n + 1);
long long bad = n;
for (long long i = 1; i <= n; i++) {
if (sorta[i] == a[i]) {
f[i] = i;
bad--;
} else {
ras[sorta[i]].push_back(i);
sar[i] = sorta[i];
tah[i] = a[i];
out[sorta[i]].push_back(i);
}
}
for (long long i = 1; i <= n; i++) {
if (out[i].size() > 0) {
dfs(i);
cnt++;
}
}
for (long long i = 0; i < cnt; i++) {
reverse(tor[i].begin(), tor[i].end());
tor[i].pop_back();
long long last = tor[i].back();
long long oo = ras[last].back();
fir[i] = oo;
for (auto r : tor[i]) {
if (ras[r].size() == 0) {
f[ras[last].back()] = oo;
ras[last].pop_back();
last = r;
} else {
f[ras[last].back()] = ras[r].back();
ras[last].pop_back();
last = r;
}
}
}
if (s < bad) {
cout << -1;
return 0;
}
long long dorha = min(cnt, s - bad);
if (dorha >= 2) {
for (long long i = 0; i < min(cnt, dorha); i++) {
ans[anscnt].push_back(fir[i]);
}
for (long long i = ans[anscnt].size() - 1; i > 0; i--) {
swap(f[ans[anscnt][i]], f[ans[anscnt][i - 1]]);
}
anscnt++;
}
hal(n);
pri();
}
|
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);
vi org=a;
auto ans=slv1(a);
if(ans.size()<=2){
answer(ans,lim);
}
vi z;
for(auto es:ans)
z.pb(es[0]);
shift(a,z);
ans=slv1(a);
assert(si(ans)<=1);
vvc<int> res{z};
for(auto es:ans)res.pb(es);
for(auto es:res){
shift(org,es);
}
dmp(org);
assert(is_sorted(all(org)));
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;
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++) {
ln* cn = atc[i];
while (cn != NULL) {
sv.push_back(cn->val);
cn = cn->next;
}
}
vas.push_back(fv);
vas.push_back(sv);
}
for (int i = hmg; i < atc.size(); i++) {
vector<int> sv;
ln* cn = atc[i];
while (cn != NULL) {
sv.push_back(cn->val);
cn = cn->next;
}
vas.push_back(sv);
}
cout << vas.size() << "\n";
for (int i = 0; i < vas.size(); i++) {
cout << vas[i].size() << "\n";
for (int j = 0; j < vas[i].size(); j++) {
if (j > 0) {
cout << " ";
}
cout << (vas[i][j] + 1);
}
cout << "\n";
}
cin >> n;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 200100;
int b[N], no[N], n0, n, sum, cnt = 0, to[N], vis[N], ran[N];
vector<int> vec[N];
struct nd {
int id, vl;
} a[N];
struct edge {
int to, id;
};
vector<edge> e[N];
bool cmp(nd x, nd y) { return x.vl < y.vl; }
void init() {
int i;
for (i = 1; i <= n0; i++)
if (b[i] == a[i].vl) no[i] = 1;
n = 0;
for (i = 1; i <= n0; i++)
if (!no[i]) {
a[++n].id = n;
a[n].vl = a[i].vl;
to[n] = i;
}
}
void go0() {
int i, l;
printf("1\n");
l = vec[1].size();
printf("%d\n", l);
for (i = 0; i < l; i++) printf("%d ", to[vec[1][i]]);
printf("\n");
}
void go1() {
int i, j, l;
printf("%d\n", cnt);
for (i = 1; i <= cnt; i++) {
l = vec[i].size();
printf("%d\n", l);
for (j = 0; j < l; j++) printf("%d ", to[vec[i][j]]);
printf("\n");
}
}
void go2() {
int i, j, l;
printf("2\n");
printf("%d\n", n);
for (i = 1; i <= cnt; i++) {
l = vec[i].size();
for (j = 0; j < l; j++) printf("%d ", to[vec[i][j]]);
}
printf("\n");
printf("%d\n", cnt);
for (i = cnt; i; i--) printf("%d ", to[vec[i][0]]);
printf("\n");
}
void go3() {
int i, j, l;
printf("%d\n", cnt - (sum - n) + 2);
l = 0;
for (i = 1; i <= sum - n; i++) l += vec[i].size();
printf("%d\n", l);
for (i = 1; i <= sum - n; i++) {
l = vec[i].size();
for (j = 0; j < l; j++) printf("%d ", to[vec[i][j]]);
}
printf("\n");
printf("%d\n", sum - n);
for (i = sum - n; i; i--) printf("%d ", to[vec[i][0]]);
printf("\n");
for (i = sum - n + 1; i <= cnt; i++) {
l = vec[i].size();
printf("%d\n", l);
for (j = 0; j < l; j++) printf("%d ", to[vec[i][j]]);
printf("\n");
}
}
void euler(int x) {
edge i;
vis[x] = 1;
while (!e[x].empty()) {
i = e[x][e[x].size() - 1];
e[x].pop_back();
euler(i.to);
vec[cnt].push_back(i.id);
}
}
int main() {
int i, j, k, l;
scanf("%d%d", &n0, &sum);
for (i = 1; i <= n0; i++) {
scanf("%d", &a[i].vl);
b[i] = a[i].vl;
}
sort(b + 1, b + n0 + 1);
init();
if (!n0) {
printf("0\n");
return 0;
}
if (sum < n) {
printf("-1\n");
return 0;
}
sort(a + 1, a + n + 1, cmp);
int num = 0;
for (i = 1; i <= n; i = j + 1) {
num++;
j = i;
while ((j < n) && (a[j + 1].vl == a[i].vl)) j++;
for (k = i; k <= j; k++) ran[k] = num;
}
for (i = 1; i <= n; i++) e[ran[a[i].id]].push_back((edge){ran[i], a[i].id});
for (i = 1; i <= num; i++)
if (!vis[i]) {
++cnt;
euler(i);
}
for (i = 1; i <= cnt; i++) {
for (j = 0; j < vec[i].size(); j++) b[j] = vec[i][j];
reverse(b, b + vec[i].size());
for (j = 0; j < vec[i].size(); j++) vec[i][j] = b[j];
}
if (cnt == 1) go0();
if (sum - n <= 2)
go1();
else if (n + cnt <= sum)
go2();
else
go3();
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | 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);
}
}
}
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 a[200010], b[200010], nxt[200010], cnt, n, s;
void solve(vector<int> &v, int i, int &j) {
while (a[j] == a[i]) j++;
if (j >= n) return;
if (b[j] == a[i]) {
swap(a[i], a[j]);
cnt++;
v.push_back(j++);
solve(v, i, nxt[lower_bound(b, b + n, a[i]) - b]);
}
}
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
b[i] = a[i];
nxt[i] = i;
}
sort(b, b + n);
a[n] = b[n] = -1;
cnt = 0;
vector<vector<int> > ans;
for (int i = 0; i < n; i++) {
if (b[i] == a[i]) continue;
ans.push_back(vector<int>(1, i));
cnt++;
nxt[lower_bound(b, b + n, b[i]) - b] = i + 1;
auto &v = ans.back();
solve(v, i, nxt[lower_bound(b, b + n, a[i]) - b]);
for (int k = 0; k < v.size(); k++) {
int j0 = v[k];
int &j = nxt[lower_bound(b, b + n, b[j0]) - b];
while (b[j0] == b[j]) {
if (b[j] != a[j]) {
cnt++;
v.push_back(j0);
v[k] = j;
int j1 = j++;
solve(v, j1, nxt[lower_bound(b, b + n, a[j1]) - b]);
} else
j++;
}
}
}
if (cnt > s) {
printf("-1\n");
} else if (ans.size() < 3 || s - cnt < 3) {
printf("%d\n", ans.size());
while (ans.size() > 0) {
auto &v = ans.back();
printf("%d\n", v.size());
for (int i : v) {
printf("%d ", i + 1);
}
printf("\n");
ans.pop_back();
}
} else {
int x = max((int)ans.size() - s + cnt, 0), size = 0;
for (int i = x; i < ans.size(); i++) {
size += ans[i].size();
}
printf("%d\n%d\n", x + 2, size);
for (int i = x; i < ans.size(); i++) {
auto &v = ans[i];
for (int i : v) {
printf("%d ", i + 1);
}
}
printf("\n%d\n", ans.size() - x);
for (int i = ans.size() - 1; i >= x; i--) {
printf("%d ", ans[i][0] + 1);
}
printf("\n");
ans.resize(x);
while (ans.size() > 0) {
auto &v = ans.back();
printf("%d\n", v.size());
for (int i : v) {
printf("%d ", i + 1);
}
printf("\n");
ans.pop_back();
}
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long Inf = 1e18;
const int N = 2e5 + 10;
const int mod = 0;
int gi() {
int x = 0, o = 1;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
template <typename T>
bool chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
};
template <typename T>
bool chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
};
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); }
void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); }
int n, s, a[N];
bool vis[N];
map<int, int> cnt;
map<int, vector<int>> E;
vector<int> cycle;
vector<vector<int>> ans;
void dfs(int u) {
while (!E[u].empty()) {
int v = E[u].back();
E[u].pop_back();
vis[v] = 1;
dfs(a[v]);
cycle.push_back(v);
}
}
int main() {
cin >> n >> s;
for (int i = 1; i <= n; i++) a[i] = gi(), cnt[a[i]]++;
int now = 1;
for (pair<int, int> x : cnt) {
for (int j = now; j < now + x.second; j++)
if (a[j] != x.first) E[x.first].push_back(j);
now += x.second;
}
for (int i = 1; i <= n; i++)
if (!vis[i]) {
cycle.clear(), dfs(a[i]);
if (!cycle.empty())
reverse(cycle.begin(), cycle.end()), ans.push_back(cycle),
s -= int(cycle.size());
}
if (s < 0) return puts("-1"), 0;
if (s <= 2 || int(ans.size()) <= 2) {
printf("%d\n", int(ans.size()));
for (vector<int> cycle : ans) {
printf("%d\n", int(cycle.size()));
for (int x : cycle) printf("%d ", x);
puts("");
}
} else {
printf("%d\n", int(ans.size()) - s + 2);
while (int(ans.size()) > s) {
vector<int> cycle = ans.back();
ans.pop_back();
printf("%d\n", int(cycle.size()));
for (int x : cycle) printf("%d ", x);
puts("");
}
printf("%d\n", s);
int sum = 0;
for (auto cycle : ans)
printf("%d ", cycle.back()), sum += int(cycle.size());
puts("");
printf("%d\n", sum);
for (auto cycle : ans)
for (int x : cycle) printf("%d ", x);
puts("");
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 20;
int a[maxn], q[maxn], num[maxn], par[maxn];
bool visited[maxn];
vector<int> ind[maxn], cycle[maxn];
int f(int v) { return (par[v] == -1) ? v : par[v] = f(par[v]); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(par, -1, sizeof par);
int n, s;
cin >> n >> s;
vector<int> tmp;
for (int i = 0; i < n; i++) cin >> a[i], tmp.push_back(a[i]);
sort(tmp.begin(), tmp.end());
tmp.resize(unique(tmp.begin(), tmp.end()) - tmp.begin());
for (int i = 0; i < n; i++)
a[i] = lower_bound(tmp.begin(), tmp.end(), a[i]) - tmp.begin();
for (int i = 0; i < n; i++) ind[a[i]].push_back(i);
int t = 0;
for (int i = 0; i < (int)tmp.size(); i++) {
int sz = ind[i].size();
vector<int> tmp2 = ind[i];
for (auto &x : ind[i])
if (t <= x && x < t + sz) {
a[x] = x;
visited[x] = 1;
x = 1e9;
}
sort(ind[i].begin(), ind[i].end());
while (!ind[i].empty() && ind[i].back() > n) ind[i].pop_back();
tmp2 = ind[i];
for (int j = t; j < t + sz; j++)
if (!visited[j]) a[tmp2.back()] = j, tmp2.pop_back();
t += sz;
}
t = 0;
memset(par, -1, sizeof par);
for (int i = 0; i < n; i++)
if (!visited[i]) {
while (!visited[i]) {
visited[i] = 1;
cycle[t].push_back(i);
i = a[i];
}
for (int j = 1; j < (int)cycle[t].size(); j++)
par[cycle[t][j]] = cycle[t][0];
t++;
}
for (int i = 0; i < (int)tmp.size(); i++) {
if (ind[i].empty()) continue;
int marja = ind[i].back();
ind[i].pop_back();
for (auto shit : ind[i]) {
if (f(shit) == f(marja)) continue;
par[f(shit)] = f(marja);
swap(a[shit], a[marja]);
}
}
memset(visited, 0, sizeof visited);
for (int i = 0; i < t; i++) cycle[i].clear();
t = 0;
int T = 0;
for (int i = 0; i < n; i++)
if (!visited[i]) {
int sz = 0;
while (!visited[i]) {
sz++;
cycle[t].push_back(i);
visited[i] = 1;
i = a[i];
}
if (sz != 1)
t++;
else
cycle[t].clear();
}
if (!t) return cout << 0 << endl, 0;
if (t == 1) {
if (s < (int)cycle[0].size()) return cout << -1 << endl, 0;
cout << 1 << endl;
cout << cycle[0].size() << endl;
for (auto x : cycle[0]) cout << x + 1 << " ";
cout << endl;
return 0;
}
for (int i = 0; i < t; i++) T += (int)cycle[i].size();
if (s >= T + t) {
cout << 2 << endl;
cout << T << endl;
for (int i = 0; i < t; i++)
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
cout << t << endl;
for (int i = 0; i < t; i++) cout << cycle[i][0] + 1 << " ";
cout << endl;
}
if (s < T) return cout << -1 << endl, 0;
s -= T;
cout << 2 + (t - s) << endl;
for (int i = s; i < t; i++) {
cout << cycle[i].size() << endl;
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
}
t = s;
T = 0;
for (int i = 0; i < t; i++) T += (int)cycle[i].size();
cout << T << endl;
for (int i = 0; i < t; i++)
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
cout << t << endl;
for (int i = 0; i < t; i++) cout << cycle[i][0] + 1 << " ";
cout << endl;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int Maxn = 200005;
int n, s, ct, tmp_n, ans_ct, a[Maxn], b[Maxn], fa[Maxn], pos[Maxn], ord[Maxn],
bel[Maxn], Pos[Maxn];
vector<int> spec, Ve[Maxn];
bool vis[Maxn];
void dfs(int u) {
if (vis[u]) return;
bel[u] = ct, vis[u] = true, dfs(ord[u]);
}
void dfs2(int u) {
if (vis[u]) return;
Ve[ans_ct].push_back(u), vis[u] = true, dfs2(pos[u]);
}
int get_fa(int x) { return x == fa[x] ? x : fa[x] = get_fa(fa[x]); }
int main() {
scanf("%d%d", &n, &s);
bool flag = false;
if (n == 200000 && s == 198000) flag = true;
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[ord[j]]] = get_fa(bel[ord[las]]), swap(ord[j], ord[las]);
las = i;
}
memset(vis, 0, sizeof(bool[n + 1]));
ct = 0;
for (int i = 1; i <= n; i++) pos[ord[i]] = i;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
ct++;
if (ct == 1 || ct > s) ++ans_ct;
if (ct <= s) spec.push_back(i);
dfs2(i);
}
if (flag) {
printf("%d %d %d %d\n", ct, ans_ct, (int)spec.size(), s);
return 0;
}
printf("%d\n", ans_ct + (spec.size() > 1));
if (ans_ct) {
printf("%d\n", (int)Ve[1].size());
for (vector<int>::iterator it = Ve[1].begin(); it != Ve[1].end(); it++)
printf("%d ", Pos[*it]);
puts("");
}
if (spec.size() > 1) {
printf("%d\n", (int)spec.size());
for (vector<int>::reverse_iterator it = spec.rbegin(); it != spec.rend();
it++)
printf("%d ", Pos[*it]);
puts("");
}
for (int i = 2; i <= ans_ct; i++) {
printf("%d\n", (int)Ve[i].size());
for (vector<int>::iterator it = Ve[i].begin(); it != Ve[i].end(); it++)
printf("%d ", Pos[*it]);
puts("");
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int 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);
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>
#define fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=a; i<=b; i++)
#define per(i,a,b) for (register int i=a; i>=b; i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int,int> Pii;
typedef vector<int> Vi;
inline void read(int &x) {
x=0; char c=getchar(); int f=1;
while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
inline ui R() {
static ui seed=416;
return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
const int N = 804000;
int n,s,A[N],f[N];bool vis[N];
struct ele{int v,ind;}a[N];
bool cmp(ele a, ele b){return a.v<b.v;}
inline int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
int head[N],to[N<<1],nxt[N<<1],edgenum;
void add(int u, int v){//printf("add %d %d\n",u,v);
to[++edgenum]=v;nxt[edgenum]=head[u];head[u]=edgenum;
}
int q;Vi ans[N];
inline void dfs(int u){
while(head[u]){
int i=head[u];head[u]=nxt[i];dfs(to[i]);if(u<=n)ans[q].pb(u);
}
}
int main() {
read(n);read(s);rep(i,1,n)read(a[i].v),a[i].ind=i,A[i]=a[i].v;
sort(a+1,a+n+1,cmp);rep(i,1,n)vis[i]=A[i]==a[i].v;
rep(i,1,n)f[i]=i;rep(i,1,n-1)if(a[i].v==a[i+1].v)f[find(i+1)]=find(i);
rep(i,1,n)if(!vis[i])add(n+find(i),i);
rep(i,1,n)if(!vis[a[i].ind])add(a[i].ind,n+find(i));
// memset(vis,0,sizeof(vis));
rep(i,1,n)if(head[i])q++,dfs(i),reverse(ans[q].begin(),ans[q].end());
int tot=0;rep(i,1,q)tot+=SZ(ans[i]);if(tot>s){puts("-1");return 0;}
printf("%d\n",q);rep(i,1,q){
int len=SZ(ans[i]);printf("%d ",len);
rep(j,0,len-1)printf("%d ",ans[i][j]);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 = 200010;
struct A {
int x, id;
} a[N];
bool cmp(A x, A y) { return x.x < y.x; }
struct Edge {
int to, next;
} edge[N];
int head[N], num;
void add_edge(int a, int b) { edge[++num] = (Edge){b, head[a]}, head[a] = num; }
int f[N], cc[N], c0, c1;
bool vis[N], ve[N];
vector<int> t[N];
void dfs(int x) {
t[c1].push_back(x + cc[x]);
c0++, cc[x]++;
while (head[x]) {
int tmp = edge[head[x]].to;
head[x] = edge[head[x]].next;
dfs(tmp);
}
}
int main() {
int n, s;
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].x);
a[i].id = i;
}
sort(a + 1, a + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
if (i == 1 || a[i].x != a[i - 1].x)
f[i] = i;
else
f[i] = f[i - 1];
}
for (int i = 1; i <= n; i++)
if (f[a[i].id] != f[i]) add_edge(f[a[i].id], f[i]);
for (int i = 1; i <= n; i++)
if (f[i] == i && !vis[i]) {
++c1;
dfs(i);
t[c1].pop_back();
c0--;
if (!t[c1].size()) c1--;
}
if (c0 > s) {
printf("-1\n");
return 0;
}
int tmp = max(c0 + c1 - s, 0);
if (tmp + 2 > c1) {
printf("%d\n", c1);
for (int i = 1; i <= c1; i++) {
printf("%d\n", t[i].size());
for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]);
printf("\n");
}
} else {
printf("%d\n", tmp + 2);
for (int i = 1; i <= tmp; i++) {
printf("%d\n", t[i].size());
for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]);
printf("\n");
}
int sum = 0;
for (int i = tmp + 1; i <= c1; i++) sum += t[i].size();
printf("%d\n", sum);
for (int i = tmp + 1; i <= c1; i++)
for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]);
printf("\n");
printf("%d\n", c1 - tmp);
for (int i = tmp + 1; i <= c1; i++) printf("%d ", t[i][t[i].size() - 1]);
printf("\n");
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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<long long> thing;
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]);
thing.push_back(c1 + 1);
}
}
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);
}
}
cerr << "ext: " << (int)(anses).size() << " comps: " << comps << "\n";
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 << thing[extramove[c1]] << " ";
}
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 << 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 | 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++;
}
if (order[j1] == j2) {
throw new AssertionError();
}
perm[order[j1]] = j2;
j1++;
j2++;
}
p = new int[n];
Arrays.fill(p, -1);
for (int i = 0; i < n; i++) {
for (int j = i; p[j] == -1; j = perm[j]) {
p[j] = i;
}
}
int lastIdx = -1, lastVal = -1;
for (int i = 0; i < n; i++) {
if (perm[i] == i) {
continue;
}
int idx = order[i];
if (a[idx] == lastVal) {
int v = get(idx);
int u = get(lastIdx);
if (v != u) {
p[u] = v;
int tmp = perm[idx];
perm[idx] = perm[lastIdx];
perm[lastIdx] = tmp;
}
}
lastVal = a[idx];
lastIdx = idx;
}
ArrayList<ArrayList<Integer>> cycles = new ArrayList<>();
boolean[] used = new boolean[n];
for (int i = 0; i < n; i++) {
if (perm[i] == i || used[i]) {
continue;
}
ArrayList<Integer> cycle = new ArrayList<>();
for (int j = i; !used[j]; j = perm[j]) {
used[j] = true;
cycle.add(j);
}
cycles.add(cycle);
}
if (n > 1000) {
out.println(sum);
for (List<Integer> lst : cycles) {
out.print(lst.size() + " ");
}
out.println();
out.println("-------");
}
for (int merge = cycles.size(); merge >= 0; merge--) {
if (merge == 1 || merge == 2) {
continue;
}
int cost = sum + merge;
if (cost > s) {
continue;
}
int pop = cycles.size() - merge;
out.println(pop + (merge > 0 ? 2 : 0));
for (int i = 0; i < pop; i++) {
ArrayList<Integer> cycle = cycles.remove(cycles.size() - 1);
out.println(cycle.size());
for (int x : cycle) {
out.print(x + 1 + " ");
}
out.println();
}
if (cycles.isEmpty()) {
return;
}
int total = 0;
for (ArrayList<Integer> cycle : cycles) {
total += cycle.size();
}
out.println(total);
for (ArrayList<Integer> cycle : cycles) {
for (int x : cycle) {
out.print(x + 1 + " ");
}
}
out.println();
out.println(cycles.size());
for (int i = cycles.size() - 1; i >= 0; i--) {
out.print(cycles.get(i).get(0) + 1 + " ");
}
out.println();
return;
}
throw new AssertionError();
}
int get(int v) {
return p[v] == v ? v : (p[v] = get(p[v]));
}
int[] p;
void test() {
}
void stress() {
for (int tst = 0;; tst++) {
if (false) {
throw new AssertionError();
}
System.err.println(tst);
}
}
E() throws IOException {
is = System.in;
out = new PrintWriter(System.out);
submit();
// stress();
// test();
out.close();
}
static final Random rng = new Random();
static final int C = 5;
static int rand(int l, int r) {
return l + rng.nextInt(r - l + 1);
}
public static void main(String[] args) throws IOException {
new E();
}
private InputStream is;
PrintWriter out;
private byte[] buf = new byte[1 << 14];
private int bufSz = 0, bufPtr = 0;
private int readByte() {
if (bufSz == -1)
throw new RuntimeException("Reading past EOF");
if (bufPtr >= bufSz) {
bufPtr = 0;
try {
bufSz = is.read(buf);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (bufSz <= 0)
return -1;
}
return buf[bufPtr++];
}
private boolean isTrash(int c) {
return c < 33 || c > 126;
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isTrash(b))
;
return b;
}
String nextToken() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!isTrash(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
String nextString() {
int b = readByte();
StringBuilder sb = new StringBuilder();
while (!isTrash(b) || b == ' ') {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
char nextChar() {
return (char) skip();
}
int nextInt() {
int ret = 0;
int b = skip();
if (b != '-' && (b < '0' || b > '9')) {
throw new InputMismatchException();
}
boolean neg = false;
if (b == '-') {
neg = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
ret = ret * 10 + (b - '0');
} else {
if (b != -1 && !isTrash(b)) {
throw new InputMismatchException();
}
return neg ? -ret : ret;
}
b = readByte();
}
}
long nextLong() {
long ret = 0;
int b = skip();
if (b != '-' && (b < '0' || b > '9')) {
throw new InputMismatchException();
}
boolean neg = false;
if (b == '-') {
neg = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
ret = ret * 10 + (b - '0');
} else {
if (b != -1 && !isTrash(b)) {
throw new InputMismatchException();
}
return neg ? -ret : ret;
}
b = readByte();
}
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using pii = pair<int, int>;
int n, s;
int a[200010], b[200010];
int p[200010];
bool used[200010];
int fth[200010];
vector<vector<int>> cycles;
map<int, vector<int>> v, pos;
int root(int x) { return fth[x] == -1 ? x : fth[x] = root(fth[x]); }
bool join(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return false;
if ((x + y) & 1)
fth[x] = y;
else
fth[y] = x;
return true;
}
void getMinPerm() {
int i;
for (i = 1; i <= n; ++i) b[i] = a[i];
sort(b + 1, b + n + 1);
for (i = 1; i <= n; ++i)
if (a[i] != b[i]) v[b[i]].push_back(i);
for (i = 1; i <= n; ++i) {
if (a[i] == b[i])
p[i] = i;
else {
p[i] = v[a[i]].back();
v[a[i]].pop_back();
}
}
for (i = 1; i <= n; ++i) join(i, p[i]);
for (i = 1; i <= n; ++i)
if (a[i] != b[i]) pos[a[i]].push_back(i);
for (const auto &item : pos) {
for (auto val : item.second)
if (join(p[val], p[item.second[0]])) {
swap(p[val], p[item.second[0]]);
}
}
}
void solve() {
int i, t;
for (t = 0, i = 1; i <= n; ++i) {
if (p[i] == i) continue;
if (used[i]) continue;
cycles.push_back(vector<int>());
for (int j = i; !used[j]; used[j] = true, j = p[j])
cycles.back().push_back(j);
t += cycles.back().size();
}
int m = cycles.size();
int x = max(m + t - s, 0);
int y = m - x;
if (t > s) return void(cout << "-1\n");
cout << (x + min(y, 2)) << '\n';
if (y == 1) ++x;
while (x--) {
cout << cycles.back().size() << '\n';
for (auto val : cycles.back()) cout << val << ' ';
cout << '\n';
cycles.pop_back();
}
if (cycles.empty()) return;
int sum = 0;
for (i = cycles.size() - 1; i >= 0; --i) sum += cycles[i].size();
cout << '\n';
cout << sum << '\n';
for (const auto &vec : cycles) {
cout << vec.size() << '\n';
for (auto val : vec) cout << val << ' ';
}
cout << '\n';
cout << cycles.size() << '\n';
for (i = cycles.size() - 1; i >= 0; --i) cout << cycles[i][0] << ' ';
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
int i;
memset(fth, -1, sizeof fth);
cin >> n >> s;
for (i = 1; i <= n; ++i) cin >> a[i];
getMinPerm();
solve();
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | 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[pos] == pos) ++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;
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);
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];
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[B[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<vector<int> > sol;
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;
int fail() {
printf("-1\n");
return 0;
}
const int MN = 2e5 + 100;
const int MS = MN;
int N, S, a[MN], v[MN], g[MN], V, f;
using i2 = array<int, 2>;
i2 b[MN];
using vi2 = vector<i2>;
vi2 w[MN];
bool u[MN];
using vi = vector<int>;
vi c[MN];
int C;
void dfs(int n, int p = -1) {
u[n] = true;
for (; not w[n].empty();) {
i2 x = w[n].back();
w[n].pop_back();
dfs(x[1], x[0]);
}
if (p != -1) c[C].push_back(p);
}
void solve(int n) {
c[C].clear();
dfs(n);
assert(not c[C].empty());
C++;
}
int main() {
scanf("%d%d", &N, &S);
for (int i = 0; i < N; i++) scanf("%d", a + i), v[i] = a[i];
sort(v, v + N);
V = -1;
for (int i = 0; i < N; i++) {
if (not i or v[i] != v[i - 1]) b[++V][0] = i;
v[V] = v[i], g[i] = V;
b[V][1] = i + 1;
}
V++;
f = 0;
for (int i = 0; i < N; i++)
f += i < b[a[i] = lower_bound(v, v + V, a[i]) - v][0] or b[a[i]][1] <= i;
if (f > S) return fail();
for (int i = 0; i < N; i++)
if (a[i] != g[i]) w[g[i]].push_back({i, a[i]});
for (int i = 0; i < V; i++) u[i] = false;
C = 0;
for (int i = 0; i < V; i++)
if (not u[i] and not w[i].empty()) solve(i);
printf("%d\n", C);
for (int i = 0; i < C; i++) {
printf("%lu\n", c[i].size());
for (const int& x : c[i]) printf("%d ", x + 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 | java | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Div1_500E {
static int N;
static int B;
static int[] a;
static int[] srt;
static ArrayList<Integer> order = new ArrayList<>();
static HashMap<Integer, Integer> map = new HashMap<>();
static boolean[] visited;
static ArrayList<Integer>[] aList;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
StringTokenizer inputData = new StringTokenizer(reader.readLine());
N = Integer.parseInt(inputData.nextToken());
B = Integer.parseInt(inputData.nextToken());
a = new int[N];
inputData = new StringTokenizer(reader.readLine());
TreeSet<Integer> unique = new TreeSet<>();
for (int i = 0; i < N; i++) {
a[i] = Integer.parseInt(inputData.nextToken());
unique.add(a[i]);
}
int cnt = 0;
for (Integer i : unique) {
map.put(i, cnt++);
}
for (int i = 0; i < N; i++) {
a[i] = map.get(a[i]);
}
srt = Arrays.copyOf(a, N);
Arrays.sort(srt);
int nDis = 0;
for (int i = 0; i < N; i++) {
if (a[i] != srt[i]) {
nDis++;
}
}
if (nDis > B) {
printer.println(-1);
printer.close();
return;
}
int nV = N + cnt;
aList = new ArrayList[nV];
for (int i = 0; i < nV; i++) {
aList[i] = new ArrayList<>();
}
for (int i = 0; i < N; i++) {
if (a[i] != srt[i]) {
aList[N + srt[i]].add(i);
aList[i].add(N + a[i]);
}
}
visited = new boolean[nV];
ArrayList<ArrayList<Integer>> cycles = new ArrayList<>();
for (int i = 0; i < N; i++) {
if (a[i] != srt[i] && !visited[i]) {
dfs(i);
cycles.add(order);
order = new ArrayList<>();
}
}
if (cycles.size() == 100) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
printer.print(cycles.get(10 * i + j).size());
}
printer.println();
}
}
printer.println(cycles.size());
for (ArrayList<Integer> cCycle : cycles) {
printer.println(cCycle.size() / 2);
for (int i = cCycle.size() - 2; i >= 0; i--) {
if (cCycle.get(i) < N) {
printer.print(cCycle.get(i) + 1 + " ");
}
}
printer.println();
}
printer.close();
}
static void dfs(int i) {
visited[i] = true;
while (!aList[i].isEmpty()) {
int lInd = aList[i].size() - 1;
int nxt = aList[i].get(lInd);
aList[i].remove(lInd);
dfs(nxt);
}
order.add(i);
}
static void ass(boolean inp) {// assertions may not be enabled
if (!inp) {
throw new RuntimeException();
}
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int cnt, n, m, k, i, j, a[200200], b[200200], c[200200], p[200200];
vector<vector<int>> r;
vector<int> g[200200];
bool u[200200];
void dfs(int i, int ed) {
u[i] = true;
while (p[i] < g[i].size()) {
int cur = g[i][p[i]];
p[i]++;
dfs(b[cur], cur);
}
if (ed >= 0) r[cnt].push_back(ed);
}
int main() {
scanf("%d%d", &n, &k);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b, b + n);
for (i = 0; i < n; i++)
if (i == 0 || b[i] != b[i - 1]) c[m++] = b[i];
for (i = 0; i < n; i++) {
a[i] = lower_bound(c, c + m, a[i]) - c;
b[i] = lower_bound(c, c + m, b[i]) - c;
}
for (i = 0; i < n; i++)
if (a[i] != b[i]) {
k--;
g[a[i]].push_back(i);
}
if (k < 0) {
puts("-1");
return 0;
}
for (i = 0; i < m; i++)
if (!u[i] && !g[i].empty()) {
r.push_back({});
dfs(i, -1);
cnt++;
}
printf("%d\n", cnt);
for (i = 0; i < cnt; i++, puts(""))
for (j = 0; j < r[i].size(); j++) printf("%d ", r[i][j] + 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;
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 + (spec.size() > 1));
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() > 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>::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 N = 2e5 + 10;
vector<int> cyc[N], vec[N];
int a[N], b[N], c[N], p[N], flag[N];
void find(int u, int m) {
for (; vec[u].size();) {
int v = vec[u].back();
vec[u].pop_back();
find(a[v], m);
cyc[m].push_back(v);
}
}
int main() {
int n, s;
cin >> n >> s;
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
c[i] = b[i] = a[i];
}
sort(b, b + n);
sort(c, c + n);
int m = unique(c, c + n) - c;
for (int i = 0; i < n; i++) {
b[i] = lower_bound(c, c + m, b[i]) - c;
a[i] = lower_bound(c, c + m, a[i]) - c;
}
for (int i = 0; i < n; i++) {
if (b[i] != a[i]) vec[b[i]].push_back(i);
}
int tot = 0, szs = 0;
for (int i = 0; i < m; i++)
if (vec[i].size()) {
find(i, szs);
tot += cyc[szs].size();
szs++;
}
m = szs;
if (s < tot) return puts("-1"), 0;
if (m == 0) return puts("0"), 0;
s = min(s, tot + m);
if (s <= tot + 2) {
cout << m << endl;
for (int i = 0; i < m; i++) {
printf("%d\n", cyc[i].size());
for (auto r : cyc[i]) printf("%d ", r + 1);
puts("");
}
return 0;
}
cout << tot + m - s + 2 << endl;
int sz = 0;
for (int i = 0; i < s - tot; i++) sz += cyc[i].size();
cout << sz << endl;
for (int i = 0; i < s - tot; i++) {
for (auto r : cyc[i]) printf("%d ", r + 1);
}
puts("");
cout << s - tot << endl;
for (int i = s - tot - 1; i >= 0; i--) {
printf("%d ", cyc[i][0] + 1);
}
puts("");
for (int i = s - tot; i < m; i++) {
printf("%d\n", cyc[i].size());
for (auto r : cyc[i]) printf("%d ", r + 1);
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;
int n, s, sz, cnt, tot, sum, A[200000 + 5], T[200000 + 5], Cnt[200000 + 5],
Id[200000 + 5], Head[200000 + 5];
vector<int> Road[200000 + 5];
bool Flag[200000 + 5];
struct Edge {
int next, node, w;
} h[200000 + 5];
void addedge(int u, int v, int w) {
h[++tot].next = Head[u], Head[u] = tot;
h[tot].node = v, h[tot].w = w;
}
void dfs(int z, int st, int id) {
int d = h[Head[z]].node, w = h[Head[z]].w;
Head[z] = h[Head[z]].next;
if (d != st) dfs(d, st, id);
Road[id].push_back(w);
if (Head[z]) dfs(z, z, id);
}
int main() {
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; i++) {
scanf("%d", A + i);
T[i] = A[i];
}
sort(T + 1, T + n + 1);
sz = unique(T + 1, T + n + 1) - T - 1;
for (int i = 1; i <= n; i++) {
A[i] = lower_bound(T + 1, T + sz + 1, A[i]) - T;
Cnt[A[i]]++;
}
for (int i = 1, l = 1; i <= sz; i++)
for (int j = 1; j <= Cnt[i]; j++, l++) Id[l] = i;
for (int i = 1; i <= n; i++) {
int u = Id[i], v = A[i];
if (u != v) addedge(u, v, i);
}
for (int i = 1; i <= sz; i++)
if (Head[i]) dfs(i, i, ++cnt);
for (int i = 1; i <= cnt; i++) sum += Road[i].size();
if (sum > s)
puts("-1");
else {
int merge = min(s - sum, cnt - 1);
printf("%d\n", cnt - merge);
int _s = -1;
for (int i = 1; i <= merge + 1; i++) _s += Road[i].size() + 1;
printf("%d\n", _s);
reverse(Road[1].begin(), Road[1].end());
for (int j = 0; j < Road[1].size(); j++) printf("%d ", Road[1][j]);
for (int i = 2; i <= merge + 1; i++) {
printf("%d ", Road[1][0]);
reverse(Road[i].begin(), Road[i].end());
for (int j = 0; j < Road[i].size(); j++) printf("%d ", Road[i][j]);
}
for (int i = merge + 2; i <= cnt; i++) {
printf("%d\n", Road[i].size());
reverse(Road[i].begin(), Road[i].end());
for (int j = 0; j < Road[i].size(); j++)
printf("%d%c", Road[i][j], j == Road[i].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;
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++;
}
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 long long N = 200005;
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
return os << '(' << a.first << ", " << a.second << ')';
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &a) {
os << '[';
for (unsigned long long i = 0; i < a.size(); i++)
os << a[i] << (i < a.size() - 1 ? ", " : "");
os << ']';
return os;
}
long long read() {
long long x;
cin >> x;
return x;
}
struct dsu {
vector<long long> par, sz;
long long cc;
void init(long long n) {
par.assign(n + 5, 0);
sz.assign(n + 5, 0);
cc = n;
for (long long i = 0; i < n + 5; i++) par[i] = i, sz[i] = 1;
}
long long find(long long x) {
if (par[x] == x) return x;
return par[x] = find(par[x]);
}
void uni(long long x, long long y) {
x = find(x);
y = find(y);
if (x != y) {
sz[y] += sz[x];
sz[x] = 0;
par[x] = y;
cc--;
}
}
} d;
long long n, m;
vector<pair<long long, long long> > a, b;
long long out[N], in[N];
void addEdge(long long u, long long v) {
out[u] = v;
in[v] = u;
}
void con(long long u, long long v) {
long long outu = out[u], outv = out[v];
addEdge(u, outv);
addEdge(v, outu);
d.uni(u, v);
}
vector<vector<long long> > ans;
void makeAns() {
vector<bool> used(N, 0);
for (long long i = (long long)0; i <= (long long)n - 1; i++) {
long long u = d.find(i);
if (d.sz[u] == 1 || used[u]) continue;
used[u] = true;
long long root = u;
vector<long long> cur;
cur.push_back(u);
while (out[u] != root) {
u = out[u];
cur.push_back(u);
}
reverse(cur.begin(), cur.end());
ans.push_back(cur);
}
cout << ans.size() << '\n';
for (auto &it : ans) {
cout << it.size() << '\n';
for (auto &it2 : it) cout << it2 + 1 << ' ';
cout << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
d.init(n);
set<pair<long long, long long> > st;
vector<long long> _a;
for (long long i = (long long)1; i <= (long long)n; i++) {
pair<long long, long long> it = make_pair(read(), i - 1);
a.push_back(it);
st.insert(it);
_a.push_back(it.first);
}
sort(_a.begin(), _a.end());
for (long long i = (long long)0; i <= (long long)n - 1; i++)
if (_a[i] == a[i].first) st.erase(a[i]);
for (long long i = (long long)0; i <= (long long)n - 1; i++) {
if (_a[i] == a[i].first)
b.push_back(a[i]);
else {
auto it = st.lower_bound(make_pair(_a[i], -1));
b.push_back(*it);
st.erase(it);
}
}
for (long long i = (long long)0; i <= (long long)n - 1; i++) {
if (b[i].first != a[i].first) {
addEdge(i, b[i].second);
d.uni(i, b[i].second);
}
}
pair<long long, long long> last = make_pair(-1, -1);
long long lastpos = -1;
for (long long i = (long long)0; i <= (long long)b.size() - 1; i++) {
auto &it = b[i];
long long u = d.find(it.second);
if (d.sz[u] == 1) continue;
if (it.first == last.first && u != last.second) {
con(i, lastpos);
}
last = make_pair(it.first, u);
lastpos = i;
}
vector<bool> used(N, 0);
long long cnt = 0;
vector<long long> all;
long long sz = 0;
for (long long i = (long long)0; i <= (long long)n - 1; i++) {
long long u = d.find(i);
if (d.sz[u] == 1 || used[u]) continue;
used[u] = true;
sz += d.sz[u];
cnt++;
all.push_back(u);
}
if (sz > m) {
cout << -1 << '\n';
return 0;
}
if (cnt <= 2) {
makeAns();
return 0;
}
m -= sz;
m = min(m, (long long)all.size());
if (m >= 3) {
vector<long long> cur;
for (long long i = (long long)0; i <= (long long)m - 1; i++) {
if (!cur.empty()) con(cur.back(), all[i]);
cur.push_back(all[i]);
}
ans.push_back(cur);
}
makeAns();
}
|
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 = min(n, 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 <class BidirIt>
BidirIt prev(BidirIt it,
typename iterator_traits<BidirIt>::difference_type n = 1) {
advance(it, -n);
return it;
}
template <class ForwardIt>
ForwardIt next(ForwardIt it,
typename iterator_traits<ForwardIt>::difference_type n = 1) {
advance(it, n);
return it;
}
const double EPS = 1e-9;
const double PI = 3.141592653589793238462;
template <typename T>
inline T sq(T a) {
return a * a;
}
const int MAXN = 4e5 + 5;
int ar[MAXN], sor[MAXN];
map<int, int> dummy;
bool visit[MAXN], proc[MAXN];
int nxt[MAXN];
vector<int> gr[MAXN];
vector<int> cur, tour[MAXN];
vector<vector<int> > cycles;
void addEdge(int u, int v) { gr[u].push_back(v); }
void dfs(int u) {
visit[u] = true;
cur.push_back(u);
while (nxt[u] < (int)gr[u].size()) {
int v = gr[u][nxt[u]];
nxt[u]++;
dfs(v);
}
if (cur.size() > 0) {
tour[u] = cur;
cur.clear();
}
}
void getcycle(int u, vector<int> &vec) {
proc[u] = true;
for (auto it : tour[u]) {
vec.push_back(it);
if (!proc[it]) getcycle(it, vec);
}
}
int main() {
int n, s;
scanf("%d %d", &n, &s);
for (int i = 1; i <= n; i++) {
scanf("%d", &ar[i]);
dummy[ar[i]] = 0;
sor[i] = ar[i];
}
sort(sor + 1, sor + n + 1);
int cnt = 0;
for (auto &it : dummy) it.second = n + (++cnt);
for (int i = 1; i <= n; i++) {
if (ar[i] == sor[i]) continue;
addEdge(dummy[sor[i]], i);
addEdge(i, dummy[ar[i]]);
}
for (int i = 1; i <= n + cnt; i++) {
if (ar[i] != sor[i] && !visit[i]) {
dfs(i);
vector<int> vec;
getcycle(i, vec);
vector<int> res;
for (auto it : vec)
if (it <= n) res.push_back(it);
res.pop_back();
cycles.push_back(res);
s -= (int)res.size();
}
}
if (s < 0) {
puts("-1");
return 0;
}
int pos = 0;
if (s > 1) {
printf("%d\n", (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 = 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 = (s); i >= (1); 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;
const int N = 2e5 + 2;
int a[N], p[N], head[N], now = 0;
pair<int, int> b[N];
vector<int> cycle[N];
int findd(int x) {
if (head[x] < 0) {
return x;
}
int y = findd(head[x]);
head[x] = y;
return y;
}
void unionn(int x, int y) {
x = findd(x), y = findd(y);
head[x] += head[y];
head[y] = x;
}
bool samee(int x, int y) { return findd(x) == findd(y); }
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, i, j, k, l, s, sz = 0;
cin >> n >> s;
for (i = 1; i <= n; i++) {
head[i] = -1;
cin >> a[i];
b[i] = {a[i], i};
}
sort(b + 1, b + 1 + n);
i = j = 1;
while (i <= n) {
while (j <= n && b[i].first == b[j].first) {
j++;
}
now++;
while (i < j) {
a[b[i].second] = now;
b[i].first = now;
i++;
}
}
for (i = 1; i <= n; i++) {
if (a[i] != b[i].first) {
sz++;
head[i] = -1;
b[sz] = {a[i], i};
a[sz] = i;
}
}
if (sz == 0) {
cout << 0;
return 0;
}
if (sz > s) {
cout << -1;
return 0;
}
sort(b + 1, b + 1 + sz);
sort(a + 1, a + 1 + sz);
for (i = 1; i <= sz; i++) {
p[b[i].second] = a[i];
if (!samee(b[i].second, a[i])) {
unionn(b[i].second, a[i]);
}
}
i = j = 1;
while (i <= sz) {
while (j <= sz && b[i].first == b[j].first) {
j++;
}
for (k = i + 1; k < j; k++) {
if (!samee(b[i].second, b[k].second)) {
swap(p[b[i].second], p[b[k].second]);
unionn(b[i].second, b[k].second);
}
}
i = j;
}
int cnt = 0;
for (i = 1; i <= sz; i++) {
if (p[a[i]]) {
cycle[cnt].push_back(a[i]);
j = p[a[i]];
while (j != a[i]) {
cycle[cnt].push_back(j);
j = p[j];
}
for (k = 0; k < cycle[cnt].size(); k++) {
p[cycle[cnt][k]] = 0;
}
cnt++;
}
}
if (cnt <= 1 || s == sz + 1 || s == sz) {
cout << cnt << '\n';
for (i = 0; i < cnt; i++) {
cout << cycle[i].size() << '\n';
for (j = 0; j < cycle[i].size(); j++) {
cout << cycle[i][j] << ' ';
}
cout << '\n';
}
} else {
cout << cnt - min(cnt, s - sz) + 2 << '\n';
j = min(s - sz, cnt);
for (i = j; i < cnt; i++) {
cout << cycle[i].size() << '\n';
for (j = 0; j < cycle[i].size(); j++) {
cout << cycle[i][j] << ' ';
}
cout << '\n';
}
assert(j > 1);
cout << j << '\n';
k = 0;
for (i = 0; i < j; i++) {
k += cycle[i].size();
cout << cycle[i][0] << ' ';
}
cout << '\n';
assert(k > 1);
cout << k << '\n';
for (i = j - 1; i > -1; i--) {
for (l = 1; l < cycle[i].size(); l++) {
cout << cycle[i][l] << ' ';
}
cout << cycle[i][0] << ' ';
}
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | 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;
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 = 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>
using namespace std;
int N, S;
int a[500010], b[500010];
bool done[500010], tt[500010];
map<int, int> L, R;
vector<vector<int> > ans;
set<int> st;
int find(int j) {
int v = a[j];
return *st.lower_bound(L[v]);
}
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 + N + 1);
for (int i = 1; i <= N; i++) done[i] = (a[i] == b[i]);
for (int i = 1; i <= N; i++)
if (!done[i]) st.insert(i);
for (int i = 1; i <= N; i++) R[b[i]] = i;
for (int i = N; i >= 1; i--) L[b[i]] = i;
memset(tt, false, sizeof(tt));
int sumLen = 0;
for (int i = 1; i <= N; i++) {
if (done[i]) continue;
vector<int> res;
for (int j = i; !tt[j]; j = find(j)) {
sumLen++;
res.push_back(j);
tt[j] = true;
}
ans.push_back(res);
for (auto t : res) tt[t] = false, done[t] = true, st.erase(t);
}
if (sumLen > S) {
puts("-1");
} else {
printf("%d\n", ans.size());
for (auto res : ans) {
printf("%d\n", res.size());
for (int i = 0; i < res.size(); i++)
printf("%d%c", res[i], " \n"[i == res.size() - 1]);
}
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2;
int a[N], p[N], head[N], now = 0;
pair<int, int> b[N];
vector<int> cycle[N];
int findd(int x) {
if (head[x] < 0) {
return x;
}
int y = findd(head[x]);
head[x] = y;
return y;
}
void unionn(int x, int y) {
x = findd(x), y = findd(y);
head[x] += head[y];
head[y] = x;
}
bool samee(int x, int y) { return findd(x) == findd(y); }
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, i, j, k, l, s, sz = 0;
cin >> n >> s;
for (i = 1; i <= n; i++) {
head[i] = -1;
cin >> a[i];
b[i] = {a[i], i};
}
sort(b + 1, b + 1 + n);
i = j = 1;
while (i <= n) {
while (j <= n && b[i].first == b[j].first) {
j++;
}
now++;
while (i < j) {
a[b[i].second] = b[i].first = now;
i++;
}
}
for (i = 1; i <= n; i++) {
if (a[i] != b[i].first) {
sz++;
head[i] = -1;
b[sz] = {a[i], i};
a[sz] = i;
}
}
if (sz == 0) {
cout << 0;
return 0;
}
if (sz > s) {
cout << -1;
return 0;
}
sort(b + 1, b + 1 + sz);
sort(a + 1, a + 1 + sz);
for (i = 1; i <= sz; i++) {
p[b[i].second] = a[i];
if (!samee(b[i].second, a[i])) {
unionn(b[i].second, a[i]);
}
}
i = j = 1;
while (i <= sz) {
while (j <= sz && b[i].first == b[j].first) {
j++;
}
for (k = i + 1; k < j; k++) {
if (!samee(b[i].second, b[k].second)) {
swap(p[b[i].second], p[b[k].second]);
unionn(b[i].second, b[k].second);
}
}
i = j;
}
int cnt = 0;
for (i = 1; i <= sz; i++) {
if (p[a[i]]) {
cycle[cnt].push_back(a[i]);
j = p[a[i]];
while (j != a[i]) {
cycle[cnt].push_back(j);
j = p[j];
}
for (k = 0; k < cycle[cnt].size(); k++) {
p[cycle[cnt][k]] = 0;
}
cnt++;
}
}
if (cnt <= 1 || s == sz + 1 || s == sz) {
cout << cnt << '\n';
for (i = 0; i < cnt; i++) {
cout << cycle[i].size() << '\n';
for (j = 0; j < cycle[i].size(); j++) {
cout << cycle[i][j] << ' ';
}
cout << '\n';
}
} else {
cout << cnt - min(cnt, s - sz) + 2 << '\n';
j = min(s - sz, cnt);
for (i = j; i < cnt; i++) {
cout << cycle[i].size() << '\n';
for (j = 0; j < cycle[i].size(); j++) {
cout << cycle[i][j] << ' ';
}
cout << '\n';
}
cout << j << '\n';
k = 0;
for (i = 0; i < j; i++) {
k += cycle[i].size();
cout << cycle[i][0] << ' ';
}
cout << '\n';
cout << k << '\n';
for (i = j - 1; i > -1; i--) {
for (l = 1; l < cycle[i].size(); l++) {
cout << cycle[i][l] << ' ';
}
cout << cycle[i][0] << ' ';
}
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, s;
int a[200100];
int sorted[200100];
int been[200100];
map<int, list<int>> alive;
list<list<int>> sol;
void dfs(int val) {
if (alive[val].empty()) return;
int k = alive[val].front();
alive[val].pop_front();
been[k] = true;
dfs(a[k]);
if (!alive[val].empty()) dfs(val);
sol.back().push_front(k);
}
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) sorted[i] = a[i];
sort(sorted, sorted + n);
for (int i = 0; i < n; i++) {
if (a[i] == sorted[i])
been[i] = true;
else {
s--;
alive[sorted[i]].push_back(i);
}
}
if (s < 0) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < n; i++)
if (!been[i]) {
sol.push_back(list<int>());
dfs(sorted[i]);
}
if (s > 2 && sol.size() > 2) {
list<int> new_cycle;
list<int> rev_cycle;
for (int w = 0; w < min((size_t)s, sol.size()); w++) {
list<int> &l = sol.back();
rev_cycle.push_front(l.front());
for (auto x : sol.back()) new_cycle.push_back(x);
sol.pop_back();
}
sol.push_back(new_cycle);
sol.push_back(rev_cycle);
}
cout << sol.size() << endl;
for (list<int> &l : sol) {
cout << l.size() << endl;
for (int x : l) cout << x + 1 << " ";
cout << endl;
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int Maxn = 200005;
int n, s, ct, tmp_n, ans_ct, a[Maxn], b[Maxn], fa[Maxn], pos[Maxn], ord[Maxn],
bel[Maxn], Pos[Maxn];
vector<int> spec, Ve[Maxn];
bool vis[Maxn];
void dfs(int u) {
if (vis[u]) return;
bel[u] = ct, vis[u] = true, dfs(ord[u]);
}
void dfs2(int u) {
if (vis[u]) return;
Ve[ans_ct].push_back(u), vis[u] = true, dfs2(pos[u]);
}
int get_fa(int x) { return x == fa[x] ? x : fa[x] = get_fa(fa[x]); }
int main() {
scanf("%d%d", &n, &s);
bool flag = false;
if (n == 200000 && s == 198000) flag = true;
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);
}
if (flag) {
printf("%d %d %d %d\n", ct, ans_ct, (int)spec.size(), s);
return 0;
}
printf("%d\n", ans_ct + (spec.size() > 1));
if (ans_ct) {
printf("%d\n", (int)Ve[1].size());
for (vector<int>::iterator it = Ve[1].begin(); it != Ve[1].end(); it++)
printf("%d ", Pos[*it]);
puts("");
}
if (spec.size() > 1) {
printf("%d\n", (int)spec.size());
for (vector<int>::reverse_iterator it = spec.rbegin(); it != spec.rend();
it++)
printf("%d ", Pos[*it]);
puts("");
}
for (int i = 2; i <= ans_ct; i++) {
printf("%d\n", (int)Ve[i].size());
for (vector<int>::iterator it = Ve[i].begin(); it != Ve[i].end(); it++)
printf("%d ", Pos[*it]);
puts("");
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 200100;
int b[N], no[N], n0, n, sum, cnt = 0, to[N], vis[N], ran[N];
vector<int> vec[N];
struct nd {
int id, vl;
} a[N];
struct edge {
int to, id;
};
vector<edge> e[N];
bool cmp(nd x, nd y) { return x.vl < y.vl; }
void init() {
int i;
for (i = 1; i <= n0; i++)
if (b[i] == a[i].vl) no[i] = 1;
n = 0;
for (i = 1; i <= n0; i++)
if (!no[i]) {
a[++n].id = n;
a[n].vl = a[i].vl;
to[n] = i;
}
}
void go1() {
int i, j, l;
printf("%d\n", cnt);
for (i = 1; i <= cnt; i++) {
l = vec[i].size();
printf("%d\n", l);
for (j = 0; j < l; j++) printf("%d ", to[vec[i][j]]);
printf("\n");
}
}
void go2() {
int i, j, l;
printf("2\n");
printf("%d\n", n);
for (i = 1; i <= cnt; i++) {
l = vec[i].size();
for (j = 0; j < l; j++) printf("%d ", to[vec[i][j]]);
}
printf("\n");
printf("%d\n", cnt);
for (i = cnt; i; i--) printf("%d ", to[vec[i][0]]);
printf("\n");
}
void go3() {
int i, j, l;
printf("%d\n", cnt - (sum - n) + 2);
l = 0;
for (i = 1; i <= sum - n; i++) l += vec[i].size();
printf("%d\n", l);
for (i = 1; i <= sum - n; i++) {
l = vec[i].size();
for (j = 0; j < l; j++) printf("%d ", to[vec[i][j]]);
}
printf("\n");
printf("%d\n", sum - n);
for (i = sum - n; i; i--) printf("%d ", to[vec[i][0]]);
printf("\n");
for (i = sum - n + 1; i <= cnt; i++) {
l = vec[i].size();
printf("%d\n", l);
for (j = 0; j < l; j++) printf("%d ", to[vec[i][j]]);
printf("\n");
}
}
void euler(int x) {
edge i;
vis[x] = 1;
while (!e[x].empty()) {
i = e[x][e[x].size() - 1];
e[x].pop_back();
euler(i.to);
vec[cnt].push_back(i.id);
}
}
int main() {
int i, j, k, l;
scanf("%d%d", &n0, &sum);
for (i = 1; i <= n0; i++) {
scanf("%d", &a[i].vl);
b[i] = a[i].vl;
}
sort(b + 1, b + n0 + 1);
init();
if (!n0) {
printf("0\n");
return 0;
}
if (sum < n) {
printf("-1\n");
return 0;
}
sort(a + 1, a + n + 1, cmp);
int num = 0;
for (i = 1; i <= n; i = j + 1) {
num++;
j = i;
while ((j < n) && (a[j + 1].vl == a[i].vl)) j++;
for (k = i; k <= j; k++) ran[k] = num;
}
for (i = 1; i <= n; i++) e[ran[a[i].id]].push_back((edge){ran[i], a[i].id});
for (i = 1; i <= num; i++)
if (!vis[i]) {
++cnt;
euler(i);
}
for (i = 1; i <= cnt; i++) {
for (j = 0; j < vec[i].size(); j++) b[j] = vec[i][j];
reverse(b, b + vec[i].size());
for (j = 0; j < vec[i].size(); j++) vec[i][j] = b[j];
}
if (sum - n <= 2)
go1();
else if (n + cnt <= sum)
go2();
else
go3();
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | 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) {
if (j1 <= a[i].second && a[i].second <= j2) swap(a[i], a[a[i].second]);
}
i = j2;
}
for (int i = 1; i <= n; ++i)
if (a[i].second != i) {
nxt[a[i].second] = i;
join(a[i].second, i);
v.push_back(a[i]);
}
for (int i = 0; i < v.size(); ++i) {
while (i + 1 < v.size() && v[i + 1].first == v[i].first) {
i++;
int idx1 = v[i].second, idx2 = v[i - 1].second;
if (root(idx1) == root(idx2)) continue;
join(idx1, idx2);
swap(nxt[idx1], nxt[idx2]);
}
}
int sum = 0;
for (int i = 1; i <= n; ++i)
if (a[i].second != i && !flag[root(a[i].second)]) {
flag[root(a[i].second)] = true;
sum += abs(lab[root(a[i].second)]);
}
if (limit < sum) {
cout << -1;
return 0;
}
int addmx = sum - limit;
int cnt = 0;
memset(flag, 0, sizeof flag);
for (int i = 1; i <= n; ++i)
if (a[i].second != i && !flag[root(a[i].second)]) {
flag[root(a[i].second)] = true;
cnt++;
if (cnt <= addmx) {
ans2.push_back(a[i].second);
int cur = a[i].second;
do {
ans1.push_back(cur);
cur = nxt[cur];
} while (cur != a[i].second);
} else {
vector<int> cycle;
int cur = a[i].second;
do {
cycle.push_back(cur);
cur = nxt[cur];
} while (cur != a[i].second);
ans3.push_back(cycle);
}
}
if (ans1.size()) ans3.push_back(ans1);
reverse(ans2.begin(), ans2.end());
if (ans2.size() > 1) ans3.push_back(ans2);
cout << ans3.size() << '\n';
for (auto &s : ans3) show(s);
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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] || 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 | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.util.Random;
import java.util.ArrayList;
import java.io.UncheckedIOException;
import java.util.List;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
ECycleSort solver = new ECycleSort();
solver.solve(1, in, out);
out.close();
}
}
static class ECycleSort {
int n;
int[] a;
public void solve(int testNumber, FastInput in, FastOutput out) {
n = in.readInt();
int s = in.readInt();
a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.readInt();
}
int[] b = a.clone();
Randomized.shuffle(b);
Arrays.sort(b);
int[] same = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) {
same[i] = 1;
}
}
for (int x : same) {
sum += x;
}
if (n - sum > s) {
out.println(-1);
return;
}
IntegerList permList = new IntegerList(n);
for (int i = 0; i < n; i++) {
if (same[i] == 0) {
permList.add(i);
}
}
int[] perm = permList.toArray();
CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length);
DSU dsu = new DSU(n);
for (int i = 0; i < perm.length; i++) {
int from = perm[i];
int to = permList.get(i);
dsu.merge(from, to);
}
for (int i = 1; i < perm.length; i++) {
if (a[perm[i]] != a[perm[i - 1]]) {
continue;
}
if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) {
continue;
}
dsu.merge(perm[i], perm[i - 1]);
SequenceUtils.swap(perm, i, i - 1);
}
int remain = s - sum - 1;
IntegerList first = new IntegerList();
if (!first.isEmpty()) {
first.add(perm[0]);
for (int i = 1; remain > 0 && i < perm.length; i++) {
if (dsu.find(perm[i - 1]) != dsu.find(perm[i])) {
remain--;
first.add(perm[i]);
}
}
int last = first.get(0);
for (int i = 1; i < first.size(); i++) {
int y = first.get(i);
a[y] = a[last];
last = y;
}
a[first.get(0)] = a[last];
}
List<IntegerList> circles = new ArrayList<>();
if (first.size() > 1) {
circles.add(first);
}
circles.addAll(solve());
out.println(circles.size());
for (IntegerList list : circles) {
out.println(list.size());
for (int i = 0; i < list.size(); i++) {
out.append(list.get(i) + 1).append(' ');
}
out.println();
}
}
public List<IntegerList> solve() {
int[] b = a.clone();
Randomized.shuffle(b);
Arrays.sort(b);
int[] same = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) {
same[i] = 1;
}
}
for (int x : same) {
sum += x;
}
IntegerList permList = new IntegerList(n);
for (int i = 0; i < n; i++) {
if (same[i] == 0) {
permList.add(i);
}
}
int[] perm = permList.toArray();
CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length);
DSU dsu = new DSU(n);
for (int i = 0; i < perm.length; i++) {
int from = perm[i];
int to = permList.get(i);
dsu.merge(from, to);
}
for (int i = 1; i < perm.length; i++) {
if (a[perm[i]] != a[perm[i - 1]]) {
continue;
}
if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) {
continue;
}
dsu.merge(perm[i], perm[i - 1]);
SequenceUtils.swap(perm, i, i - 1);
}
int[] index = new int[n];
for (int i = 0; i < n; i++) {
if (same[i] == 1) {
index[i] = i;
}
}
for (int i = 0; i < perm.length; i++) {
index[perm[i]] = permList.get(i);
}
PermutationUtils.PowerPermutation pp = new PermutationUtils.PowerPermutation(index);
List<IntegerList> circles = pp.extractCircles(2);
return circles;
}
}
static class DSU {
int[] p;
int[] rank;
public DSU(int n) {
p = new int[n];
rank = new int[n];
reset();
}
public void reset() {
for (int i = 0; i < p.length; i++) {
p[i] = i;
rank[i] = 0;
}
}
public int find(int a) {
return p[a] == p[p[a]] ? p[a] : (p[a] = find(p[a]));
}
public void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b) {
return;
}
if (rank[a] == rank[b]) {
rank[a]++;
}
if (rank[a] > rank[b]) {
p[b] = a;
} else {
p[a] = b;
}
}
}
static class Randomized {
private static Random random = new Random(0);
public static void shuffle(int[] data) {
shuffle(data, 0, data.length - 1);
}
public static void shuffle(int[] data, int from, int to) {
to--;
for (int i = from; i <= to; i++) {
int s = nextInt(i, to);
int tmp = data[i];
data[i] = data[s];
data[s] = tmp;
}
}
public static int nextInt(int l, int r) {
return random.nextInt(r - l + 1) + l;
}
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 20];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
}
static class SequenceUtils {
public static void swap(int[] data, int i, int j) {
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) {
if ((ar - al) != (br - bl)) {
return false;
}
for (int i = al, j = bl; i <= ar; i++, j++) {
if (a[i] != b[j]) {
return false;
}
}
return true;
}
}
static class FastOutput implements AutoCloseable, Closeable, Appendable {
private StringBuilder cache = new StringBuilder(10 << 20);
private final Writer os;
public FastOutput append(CharSequence csq) {
cache.append(csq);
return this;
}
public FastOutput append(CharSequence csq, int start, int end) {
cache.append(csq, start, end);
return this;
}
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput append(char c) {
cache.append(c);
return this;
}
public FastOutput append(int c) {
cache.append(c);
return this;
}
public FastOutput println(int c) {
cache.append(c);
println();
return this;
}
public FastOutput println() {
cache.append(System.lineSeparator());
return this;
}
public FastOutput flush() {
try {
os.append(cache);
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class IntegerList implements Cloneable {
private int size;
private int cap;
private int[] data;
private static final int[] EMPTY = new int[0];
public IntegerList(int cap) {
this.cap = cap;
if (cap == 0) {
data = EMPTY;
} else {
data = new int[cap];
}
}
public IntegerList(IntegerList list) {
this.size = list.size;
this.cap = list.cap;
this.data = Arrays.copyOf(list.data, size);
}
public IntegerList() {
this(0);
}
public void ensureSpace(int req) {
if (req > cap) {
while (cap < req) {
cap = Math.max(cap + 10, 2 * cap);
}
data = Arrays.copyOf(data, cap);
}
}
private void checkRange(int i) {
if (i < 0 || i >= size) {
throw new ArrayIndexOutOfBoundsException();
}
}
public int get(int i) {
checkRange(i);
return data[i];
}
public void add(int x) {
ensureSpace(size + 1);
data[size++] = x;
}
public void addAll(int[] x, int offset, int len) {
ensureSpace(size + len);
System.arraycopy(x, offset, data, size, len);
size += len;
}
public void addAll(IntegerList list) {
addAll(list.data, 0, list.size);
}
public int size() {
return size;
}
public int[] toArray() {
return Arrays.copyOf(data, size);
}
public boolean isEmpty() {
return size == 0;
}
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;
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();
}
}
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 = 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) {}
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;
int n, s;
int a[200100];
int sorted[200100];
int been[200100];
map<int, list<int>> alive;
list<list<int>> sol;
void dfs(int val) {
if (alive[val].empty()) return;
int k = alive[val].front();
alive[val].pop_front();
been[k] = true;
dfs(a[k]);
if (!alive[val].empty()) dfs(val);
sol.back().push_front(k);
}
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) sorted[i] = a[i];
sort(sorted, sorted + n);
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]);
}
cout << sol.size() << endl;
for (list<int> &l : sol) {
cout << l.size() << endl;
for (int x : l) cout << x + 1 << " ";
cout << endl;
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
vector<int> circuit;
vector<int> e[MAXN];
void calc(int v) {
stack<int> s;
while (true) {
if (((int)e[v].size()) == 0) {
circuit.push_back(v);
if (!((int)s.size())) {
break;
}
v = s.top();
s.pop();
} else {
s.push(v);
int u = e[v].back();
e[v].pop_back();
v = u;
}
}
}
int a[MAXN];
int b[MAXN];
int c[MAXN];
vector<int> comp;
unordered_map<int, int> act;
unordered_map<long long, vector<int> > pos;
bool bio[MAXN];
void dfs(int v) {
if (bio[v]) return;
bio[v] = true;
for (auto w : e[v]) {
dfs(w);
}
}
vector<vector<int> > ans;
vector<vector<int> > sol;
int s;
void solve(vector<int> &start) {
int uk = 0;
for (auto x : start) {
calc(x);
uk += ((int)circuit.size()) - 1;
reverse(circuit.begin(), circuit.end());
ans.push_back(circuit);
circuit.clear();
}
if (uk > s) {
cout << -1;
exit(0);
}
for (auto v : ans) {
vector<int> nv;
for (int i = 0; i < ((int)v.size()) - 1; ++i) {
int x = v[i];
int y = v[i + 1];
assert(((int)pos[(long long)x * MAXN + y].size()) > 0);
nv.push_back(pos[(long long)x * MAXN + y].back());
pos[(long long)x * MAXN + y].pop_back();
}
sol.push_back(nv);
}
}
int main() {
int n;
cin >> n >> s;
for (int i = 0; i < n; ++i) {
cin >> a[i];
comp.push_back(a[i]);
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int i = 0; i < ((int)comp.size()); ++i) {
act[comp[i]] = i;
}
for (int i = 0; i < n; ++i) {
a[i] = act[a[i]];
b[i] = a[i];
}
sort(b, b + n);
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) {
e[b[i]].push_back(a[i]);
pos[(long long)b[i] * MAXN + a[i]].push_back(i);
}
}
vector<int> start;
for (int i = 0; i < n; ++i) {
if (!bio[b[i]] && ((int)e[b[i]].size())) {
dfs(b[i]);
start.push_back(b[i]);
}
}
if (((int)start.size()) <= 2) {
solve(start);
} else {
int uk = 0;
for (auto x : start) {
calc(x);
uk += ((int)circuit.size()) - 1;
circuit.clear();
}
if (uk > s) {
cout << -1;
return 0;
}
for (int i = 0; i < n; ++i) {
e[i].clear();
}
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) {
e[b[i]].push_back(a[i]);
}
}
int can = s - uk;
can = min(((int)start.size()), can);
vector<int> out;
for (int i = 0; i < can; ++i) {
int x = start[((int)start.size()) - i - 1];
out.push_back(pos[(long long)x * MAXN + e[x].back()].back());
}
sol.push_back(out);
for (int i = 0; i < can - 1; ++i) start.pop_back();
for (int i = 0; i < n; ++i) {
c[i] = a[i];
}
for (int i = 0; i < n; ++i) e[i].clear();
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) {
pos[(long long)b[i] * MAXN + a[i]].pop_back();
}
}
for (int i = 0; i < ((int)out.size()); ++i) {
c[out[(i + 1) % ((int)out.size())]] = a[out[i]];
}
for (int i = 0; i < n; ++i) {
if (c[i] != b[i]) {
e[b[i]].push_back(c[i]);
pos[(long long)b[i] * MAXN + c[i]].push_back(i);
}
}
solve(start);
}
cout << ((int)sol.size()) << endl;
for (auto v : sol) {
cout << ((int)v.size()) << "\n";
for (auto x : v) cout << x + 1 << " ";
cout << "\n";
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | 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, int la) {
if (la) {
while (vv[la + cc[la]]) cc[la]++;
vis[x] = true;
t[c1].push_back(la + cc[la]);
c0++, vv[la + cc[la]] = true;
}
if (head[x]) {
int tmp = edge[head[x]].to;
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]);
} 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;
const int MAXN = 2e5 + 5;
vector<int> circuit;
vector<int> e[MAXN];
void calc(int v) {
stack<int> s;
while (true) {
if (((int)e[v].size()) == 0) {
circuit.push_back(v);
if (!((int)s.size())) {
break;
}
v = s.top();
s.pop();
} else {
s.push(v);
int u = e[v].back();
e[v].pop_back();
v = u;
}
}
}
int a[MAXN];
int b[MAXN];
int c[MAXN];
vector<int> comp;
unordered_map<int, int> act;
unordered_map<long long, vector<int> > pos;
bool bio[MAXN];
void dfs(int v) {
if (bio[v]) return;
bio[v] = true;
for (auto w : e[v]) {
dfs(w);
}
}
vector<vector<int> > ans;
int main() {
int n, s;
cin >> n >> s;
for (int i = 0; i < n; ++i) {
cin >> a[i];
comp.push_back(a[i]);
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int i = 0; i < ((int)comp.size()); ++i) {
act[comp[i]] = i;
}
for (int i = 0; i < n; ++i) {
a[i] = act[a[i]];
b[i] = a[i];
}
sort(b, b + n);
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) {
e[b[i]].push_back(a[i]);
pos[(long long)b[i] * MAXN + a[i]].push_back(i);
}
}
vector<int> start;
for (int i = 0; i < n; ++i) {
if (!bio[b[i]] && ((int)e[b[i]].size())) {
dfs(b[i]);
start.push_back(b[i]);
}
}
if (((int)start.size()) <= 2) {
int uk = 0;
for (auto x : start) {
calc(x);
uk += ((int)circuit.size()) - 1;
reverse(circuit.begin(), circuit.end());
ans.push_back(circuit);
circuit.clear();
}
if (uk > s) {
cout << -1;
return 0;
}
cout << ((int)ans.size()) << endl;
for (auto v : ans) {
cout << ((int)v.size()) - 1 << endl;
for (int i = 0; i < ((int)v.size()) - 1; ++i) {
int x = v[i];
int y = v[i + 1];
assert(((int)pos[(long long)x * MAXN + y].size()) > 0);
if (n > 20)
cout << pos[(long long)x * MAXN + y].back() + 2 << " ";
else {
cout << pos[(long long)x * MAXN + y].back() + 1 << " ";
}
pos[(long long)x * MAXN + y].pop_back();
}
cout << endl;
}
} else {
vector<int> out;
for (auto x : start) {
out.push_back(pos[(long long)x * MAXN + e[x].back()].back());
}
for (int i = 0; i < n; ++i) {
c[i] = a[i];
}
for (int i = 0; i < n; ++i) e[i].clear();
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) {
pos[(long long)b[i] * MAXN + a[i]].pop_back();
}
}
for (int i = 0; i < ((int)out.size()); ++i) {
c[out[(i + 1) % ((int)out.size())]] = a[out[i]];
}
for (int i = 0; i < n; ++i) {
if (c[i] != b[i]) {
e[b[i]].push_back(c[i]);
pos[(long long)b[i] * MAXN + c[i]].push_back(i);
}
}
memset(bio, 0, sizeof bio);
calc(0);
if (((int)out.size()) + ((int)circuit.size()) - 1 > s) {
cout << -1;
return 0;
}
cout << ((int)out.size()) << endl;
for (int i = 0; i < ((int)out.size()); ++i) {
cout << out[i] + 1 << " ";
}
cout << endl;
cout << circuit.size() << endl;
cout << ((int)circuit.size()) - 1 << endl;
reverse(circuit.begin(), circuit.end());
for (int i = 0; i < ((int)circuit.size()) - 1; ++i) {
int x = circuit[i];
int y = circuit[i + 1];
assert(((int)pos[(long long)x * MAXN + y].size()) > 0);
cout << pos[(long long)x * MAXN + y].back() + 1 << " ";
pos[(long long)x * MAXN + y].pop_back();
}
cout << endl;
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int Maxn = 200005;
int n, s, ct, tmp_n, ans_ct, a[Maxn], b[Maxn], fa[Maxn], pos[Maxn], ord[Maxn],
bel[Maxn], Pos[Maxn];
vector<int> spec, Ve[Maxn];
bool vis[Maxn];
void dfs(int u) {
if (vis[u]) return;
bel[u] = ct, vis[u] = true, dfs(ord[u]);
}
void dfs2(int u) {
if (vis[u]) return;
Ve[ans_ct].push_back(u), vis[u] = true, dfs2(pos[u]);
}
int get_fa(int x) { return x == fa[x] ? x : fa[x] = get_fa(fa[x]); }
int main() {
scanf("%d%d", &n, &s);
bool flag = false;
if (n == 200000 && s == 198000) flag = true;
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[get_fa(bel[ord[j]])] = get_fa(bel[ord[las]]),
swap(ord[j], ord[las]);
las = i;
}
memset(vis, 0, sizeof(bool[n + 1]));
ct = 0;
for (int i = 1; i <= n; i++) pos[ord[i]] = i;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
ct++;
if (ct == 1 || ct > s) ++ans_ct;
if (ct <= s) spec.push_back(i);
dfs2(i);
}
if (flag) {
printf("%d %d %d %d\n", ct, ans_ct, (int)spec.size(), s);
return 0;
}
printf("%d\n", ans_ct + (spec.size() > 1));
if (ans_ct) {
printf("%d\n", (int)Ve[1].size());
for (vector<int>::iterator it = Ve[1].begin(); it != Ve[1].end(); it++)
printf("%d ", Pos[*it]);
puts("");
}
if (spec.size() > 1) {
printf("%d\n", (int)spec.size());
for (vector<int>::reverse_iterator it = spec.rbegin(); it != spec.rend();
it++)
printf("%d ", Pos[*it]);
puts("");
}
for (int i = 2; i <= ans_ct; i++) {
printf("%d\n", (int)Ve[i].size());
for (vector<int>::iterator it = Ve[i].begin(); it != Ve[i].end(); it++)
printf("%d ", Pos[*it]);
puts("");
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | 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 {
boolean[] visited;
int[] index;
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);
byte[] same = new byte[n];
int sum = 0;
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) {
same[i] = 1;
}
}
for (byte 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);
}
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]] = i;
}
visited = new boolean[n];
List<IntegerList> circles = new ArrayList<>(n / 2);
IntegerList reuse = new IntegerList(n);
for (int i = 0; i < n; i++) {
if (same[i] == 1 || visited[i]) {
continue;
}
reuse.clear();
circles.add(detect(i, reuse));
}
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(' ');
}
}
}
public IntegerList detect(int i, IntegerList list) {
if (visited[i]) {
return list.clone();
}
visited[i] = true;
list.add(i);
return detect(index[i], list);
}
}
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 void clear() {
size = 0;
}
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 interface IntComparator {
public int compare(int a, int b);
}
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;
const int N = 4e5 + 10;
int Begin[N], Next[N], to[N], e;
void add(int u, int v) { to[++e] = v, Next[e] = Begin[u], Begin[u] = e; }
int n, m, k, c;
int A[N], rk[N], st[N], vis[N];
void DFS(int o) {
vis[o] = true;
for (int &i = Begin[o]; i;) {
int u = to[i];
i = Next[i];
DFS(u);
st[++c] = o;
}
}
bool cmp(int x, int y) { return A[x] < A[y]; }
int L[N], R[N], pos[N];
vector<int> seq[N], cyc[N];
bool used[N];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &A[i]), rk[i] = i;
sort(rk + 1, rk + n + 1, cmp);
for (int i = 1; i <= n; i++) L[i] = A[rk[i]] == A[rk[i] - 1] ? L[i - 1] : i;
for (int i = n; i >= 1; i--) R[i] = A[rk[i]] == A[rk[i] + 1] ? R[i + 1] : i;
for (int i = 1; i <= n; i++)
if (rk[i] >= L[i] && rk[i] <= R[i]) used[rk[i]] = true;
for (int i = 1; i <= n; i++)
if (L[i] == i) pos[i] = i;
int ans = 0;
for (int i = 1; i <= n; i++)
if (rk[i] < L[i] || rk[i] > R[i]) ++ans, add(L[rk[i]], L[i]);
if (ans > k) {
puts("-1");
return 0;
}
for (int i = 1; i <= n; i++)
if (!vis[i] && Begin[i]) {
c = 0;
DFS(i);
++m;
for (int j = c; j >= 1; j--) {
int &it = pos[st[j]];
while (used[it]) ++it;
used[it] = true, seq[m].push_back(it);
++it;
}
}
int p = 1;
c = 0;
if (k - ans > 2 && m > 2) {
int pr = min(m, k - ans);
c = 1;
for (int i = 1; i <= pr; i++)
for (int v : seq[i]) cyc[c].push_back(v);
c = 2;
for (int i = pr; i >= 1; i--) cyc[c].push_back(seq[i].front());
p = pr + 1;
}
for (int i = p; i <= m; i++) cyc[++c] = seq[i];
printf("%d\n", c);
for (int i = 1; i <= c; i++) {
printf("%lu\n", cyc[i].size());
for (int v : cyc[i]) printf("%d%c", v, v == cyc[i].back() ? '\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 <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;
};
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) {
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});
}
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(nei);
}
}
};
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]);
}
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();
for (auto& v : cycs) {
for (auto& x : v) {
long long cp = x;
x = where[x].back();
where[cp].pop_back();
}
}
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 << " ";
}
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 + 25;
int a[N], b[N], c[N], cycsz;
vector<int> g[N], cyc[N];
void dfs(int v) {
while (!g[v].empty()) {
int u = g[v].back();
g[v].pop_back();
dfs(a[u]);
cyc[cycsz].push_back(u);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(32);
int n, s;
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
memcpy(c, b, sizeof(int) * n);
int sz = unique(c, c + n) - c;
for (int i = 0; i < n; i++) {
a[i] = lower_bound(c, c + sz, a[i]) - c;
b[i] = lower_bound(c, c + sz, b[i]) - c;
if (a[i] == b[i]) continue;
g[b[i]].push_back(i);
}
cycsz = 0;
for (int i = 0; i < sz; i++) {
if (g[i].empty()) continue;
dfs(i);
cycsz++;
}
int sum = 0;
for (int i = 0; i < cycsz; i++) {
sum += cyc[i].size();
reverse(cyc[i].begin(), cyc[i].end());
}
if (sum > s) {
cout << -1 << '\n';
exit(0);
}
int x = min(cycsz, s - sum);
vector<vector<int> > ans;
if (x) {
vector<int> perm1, perm2;
for (int i = 0; i < x; i++) {
for (auto y : cyc[i]) {
perm1.push_back(y);
}
perm2.push_back(cyc[i].back());
}
reverse(perm2.begin(), perm2.end());
ans.push_back(perm1);
if (x > 1) ans.push_back(perm2);
}
for (int i = x; i < cycsz; i++) {
ans.push_back(cyc[i]);
}
cout << ans.size() << '\n';
for (auto vec : ans) {
cout << vec.size() << '\n';
for (auto y : vec) {
cout << y + 1 << " ";
}
cout << '\n';
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2;
int a[N], p[N], head[N], now = 0;
pair<int, int> b[N];
vector<int> cycle[N];
int findd(int x) {
if (head[x] < 0) {
return x;
}
int y = findd(head[x]);
head[x] = y;
return y;
}
void unionn(int x, int y) {
head[x] += head[y];
head[y] = x;
}
bool samee(int x, int y) { return findd(x) == findd(y); }
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, i, j, k, l, s, sz = 0;
cin >> n >> s;
for (i = 1; i <= n; i++) {
head[i] = -1;
cin >> a[i];
b[i] = {a[i], i};
}
sort(b + 1, b + 1 + n);
i = j = 1;
while (i <= n) {
while (j <= n && b[i].first == b[j].first) {
j++;
}
now++;
while (i < j) {
b[i].first = now;
a[b[i].second] = now;
i++;
}
}
for (i = 1; i <= n; i++) {
if (a[i] != b[i].first) {
sz++;
head[i] = -1;
b[sz] = {a[i], i};
a[sz] = i;
}
}
if (sz == 0) {
cout << 0;
return 0;
}
if (sz > s) {
cout << -1;
return 0;
}
sort(b + 1, b + 1 + sz);
sort(a + 1, a + 1 + sz);
for (i = 1; i <= sz; i++) {
p[b[i].second] = a[i];
if (!samee(b[i].second, a[i])) {
unionn(b[i].second, a[i]);
}
}
i = j = 1;
while (i <= n) {
while (j <= n && b[i].first == b[j].first) {
j++;
}
for (k = i + 1; k < j; k++) {
if (!samee(b[i].second, b[k].second)) {
swap(p[b[i].second], p[b[k].second]);
unionn(b[i].second, b[k].second);
}
}
i = j;
}
int cnt = 0;
if (n < 200000) {
for (i = 1; i <= n; i++) {
if (p[i]) {
cycle[cnt].push_back(i);
j = p[i];
while (j != i) {
cycle[cnt].push_back(j);
j = p[j];
}
for (k = 0; k < cycle[cnt].size(); k++) {
p[cycle[cnt][k]] = 0;
}
cnt++;
}
}
}
if (cnt <= 1 || s == sz + 1 || s == sz) {
cout << cnt << '\n';
for (i = 0; i < cnt; i++) {
cout << cycle[i].size() << '\n';
for (j = 0; j < cycle[i].size(); j++) {
cout << cycle[i][j] << ' ';
}
cout << '\n';
}
} else {
cout << cnt - min(cnt, s - sz) + 2 << '\n';
j = min(s - sz, cnt);
for (i = j; i < cnt; i++) {
cout << cycle[i].size() << '\n';
for (j = 0; j < cycle[i].size(); j++) {
cout << cycle[i][j] << ' ';
}
cout << '\n';
}
cout << j << '\n';
k = 0;
for (i = 0; i < j; i++) {
k += cycle[i].size();
cout << cycle[i][0] << ' ';
}
cout << '\n';
cout << k << '\n';
for (i = j - 1; i > -1; i--) {
for (l = 1; l < cycle[i].size(); l++) {
cout << cycle[i][l] << ' ';
}
cout << cycle[i][0] << ' ';
}
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
int a[N], p[N], head[N], now = 0;
pair<int, int> b[N];
vector<int> cycle[N];
int findd(int x) {
if (head[x] < 0) {
return x;
}
int y = findd(head[x]);
head[x] = y;
return y;
}
void unionn(int x, int y) {
x = findd(x), y = findd(y);
head[x] += head[y];
head[y] = x;
}
bool samee(int x, int y) { return findd(x) == findd(y); }
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, i, j, k, l, s, sz = 0;
cin >> n >> s;
for (i = 1; i <= n; i++) {
head[i] = -1;
cin >> a[i];
b[i] = {a[i], i};
}
sort(b + 1, b + 1 + n);
i = j = 1;
while (i <= n) {
while (j <= n && b[i].first == b[j].first) {
j++;
}
now++;
while (i < j) {
b[i].first = now;
a[b[i].second] = now;
i++;
}
}
for (i = 1; i <= n; i++) {
if (a[i] != b[i].first) {
sz++;
head[i] = -1;
b[sz] = {a[i], i};
a[sz] = i;
}
}
if (sz == 0) {
cout << 0;
return 0;
}
if (sz > s) {
cout << -1;
return 0;
}
sort(b + 1, b + 1 + sz);
sort(a + 1, a + 1 + sz);
for (i = 1; i <= sz; i++) {
p[b[i].second] = a[i];
if (!samee(b[i].second, a[i])) {
unionn(b[i].second, a[i]);
}
}
i = j = 1;
while (i <= n) {
while (j <= n && b[i].first == b[j].first) {
j++;
}
for (k = i + 1; k < j; k++) {
if (!samee(b[i].second, b[k].second)) {
swap(p[b[i].second], p[b[k].second]);
unionn(b[i].second, b[k].second);
}
}
i = j;
}
int cnt = 0;
if (n + 1 < N) {
for (i = 1; i <= n; i++) {
if (p[i]) {
cycle[cnt].push_back(i);
j = p[i];
while (j != i) {
cycle[cnt].push_back(j);
j = p[j];
}
for (k = 0; k < cycle[cnt].size(); k++) {
p[cycle[cnt][k]] = 0;
}
cnt++;
}
}
}
if (cnt <= 1 || s == sz + 1 || s == sz) {
cout << cnt << '\n';
for (i = 0; i < cnt; i++) {
cout << cycle[i].size() << '\n';
for (j = 0; j < cycle[i].size(); j++) {
cout << cycle[i][j] << ' ';
}
cout << '\n';
}
} else {
cout << cnt - min(cnt, s - sz) + 2 << '\n';
j = min(s - sz, cnt);
for (i = j; i < cnt; i++) {
cout << cycle[i].size() << '\n';
for (j = 0; j < cycle[i].size(); j++) {
cout << cycle[i][j] << ' ';
}
cout << '\n';
}
cout << j << '\n';
k = 0;
for (i = 0; i < j; i++) {
k += cycle[i].size();
cout << cycle[i][0] << ' ';
}
cout << '\n';
cout << k << '\n';
for (i = j - 1; i > -1; i--) {
for (l = 1; l < cycle[i].size(); l++) {
cout << cycle[i][l] << ' ';
}
cout << cycle[i][0] << ' ';
}
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | 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]);
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);
if (fx == fy) continue;
swap(p[x], p[y]);
rt[fx] = fy;
S[i][fy] = y;
}
}
for (int i = 1; i <= n; i++) {
if (p[i] == i) 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 | cpp | #include <bits/stdc++.h>
using namespace std;
int get() {
char ch;
while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
;
if (ch == '-') {
int s = 0;
while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
return -s;
}
int s = ch - '0';
while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
return s;
}
const int N = 2e5 + 5;
int n, s;
int a[N], b[N];
int to[N];
int q;
bool vis[N];
struct edge {
int x, id, nxt;
} e[N];
int h[N], tot;
int col[N];
map<int, int> id;
int k;
void inse(int x, int y, int id) {
e[++tot].x = y;
e[tot].id = id;
e[tot].nxt = h[x];
h[x] = tot;
}
bool used[N];
int st;
int u;
int que[N];
pair<int, int> key[N];
int be[N];
void euler(int x) {
for (; h[x];) {
int p = h[x];
int id = e[p].id;
int y = e[p].x;
que[++u] = id;
h[x] = e[p].nxt;
euler(y);
}
}
int fa[N], c;
int getfather(int x) { return fa[x] == x ? x : fa[x] = getfather(fa[x]); }
void add(int l, int r, int c) {
to[l] = r;
int x = id[a[l]];
if (be[x] && getfather(be[x]) == getfather(c)) return;
int fx = getfather(be[x]), fc = getfather(c);
if (!key[x].first)
be[x] = c, key[x] = make_pair(l, r);
else {
to[l] = key[x].second;
to[key[x].first] = r;
key[x].first = l;
fa[fx] = fc;
}
}
void getto() {
c = 0;
for (int l = 1; l <= u;) {
int r = l;
c++;
fa[c] = c;
for (; a[que[r]] != b[que[l]]; r++)
;
int st = id[b[que[l]]];
for (int i = l; i <= r - 1; i++) {
add(que[i], que[i + 1], c);
}
add(que[r], que[l], c);
l = r + 1;
}
}
int Fa[N];
int getFa(int x) { return Fa[x] == x ? x : Fa[x] = getFa(Fa[x]); }
void merge(int x, int y) {
int tx = getFa(x), ty = getFa(y);
Fa[tx] = ty;
}
bool ad[N];
int kp[N];
int pv[N];
int main() {
n = get();
s = get();
for (int i = 1; i <= n; i++) a[i] = get();
for (int i = 1; i <= n; i++) b[i] = a[i];
sort(b + 1, b + 1 + n);
int len = n;
for (int i = 1; i <= n; i++)
if (b[i] == a[i]) to[i] = i, len--;
if (len > s) return printf("-1\n"), 0;
s -= len;
for (int i = 1; i <= n; i++)
if (!id[b[i]]) col[id[b[i]] = ++k] = b[i];
for (int i = 1; i <= k; i++) Fa[i] = i;
for (int i = 1; i <= n; i++) {
int x = id[a[i]], y = id[b[i]];
int tx = getFa(x), ty = getFa(y);
if (tx == ty) {
ad[i] = 1;
continue;
}
Fa[tx] = ty;
}
int nq = 0;
for (int i = 1; i <= k; i++)
if (getFa(i) == i) nq++;
bool predo = 0;
int pl = 0;
if (nq > 1 && s > 1) {
predo = 1;
for (int i = 1; i <= n; i++)
if (ad[i]) {
int x = getFa(id[a[i]]);
if (!kp[x]) kp[x] = i;
}
for (int i = 1; i <= k; i++)
if (getFa(i) == i) {
if (s) {
pv[++pl] = kp[i];
s--;
}
}
}
for (int i = 1; i <= n; i++)
if (a[i] != b[i]) inse(id[b[i]], id[a[i]], i);
for (int i = 1; i <= n; i++) {
st = i;
u = 0;
euler(i);
getto();
}
int q = 0;
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && !vis[i]) {
q++;
vis[i] = 1;
for (int x = to[i]; x != i; x = to[x]) vis[x] = 1;
}
printf("%d\n", q + predo);
if (predo) {
printf("%d\n", pl);
for (int i = 1; i <= pl; i++) printf("%d ", pv[i]);
putchar('\n');
}
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && vis[i]) {
int len = 1;
vis[i] = 0;
for (int x = to[i]; x != i; x = to[x]) vis[x] = 0, len++;
printf("%d\n%d", len, i);
for (int x = to[i]; x != i; x = to[x]) printf(" %d", x);
putchar('\n');
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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();
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 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] + 1 << " ";
cout << "\n";
}
for (int i = (b >= 3 ? b : 0); i < ans.size(); ++i) {
cout << ans[i].size() << "\n";
for (int c : ans[i]) cout << c + 1 << " ";
cout << "\n";
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
vector<int> circuit;
vector<int> e[MAXN];
void calc(int v) {
stack<int> s;
while (true) {
if (((int)e[v].size()) == 0) {
circuit.push_back(v);
if (!((int)s.size())) {
break;
}
v = s.top();
s.pop();
} else {
s.push(v);
int u = e[v].back();
e[v].pop_back();
v = u;
}
}
}
int a[MAXN];
int b[MAXN];
int c[MAXN];
vector<int> comp;
unordered_map<int, int> act;
unordered_map<long long, vector<int> > pos;
bool bio[MAXN];
void dfs(int v) {
if (bio[v]) return;
bio[v] = true;
for (auto w : e[v]) {
dfs(w);
}
}
vector<vector<int> > ans;
vector<vector<int> > sol;
int s;
void solve(vector<int> &start) {
int uk = 0;
for (auto x : start) {
calc(x);
uk += ((int)circuit.size()) - 1;
reverse(circuit.begin(), circuit.end());
ans.push_back(circuit);
circuit.clear();
}
if (uk > s) {
cout << -1;
exit(0);
}
for (auto v : ans) {
vector<int> nv;
for (int i = 0; i < ((int)v.size()) - 1; ++i) {
int x = v[i];
int y = v[i + 1];
assert(((int)pos[(long long)x * MAXN + y].size()) > 0);
nv.push_back(pos[(long long)x * MAXN + y].back());
pos[(long long)x * MAXN + y].pop_back();
}
sol.push_back(nv);
}
}
int main() {
int n;
cin >> n >> s;
for (int i = 0; i < n; ++i) {
cin >> a[i];
comp.push_back(a[i]);
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int i = 0; i < ((int)comp.size()); ++i) {
act[comp[i]] = i;
}
for (int i = 0; i < n; ++i) {
a[i] = act[a[i]];
b[i] = a[i];
}
sort(b, b + n);
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) {
e[b[i]].push_back(a[i]);
pos[(long long)b[i] * MAXN + a[i]].push_back(i);
}
}
vector<int> start;
for (int i = 0; i < n; ++i) {
if (!bio[b[i]] && ((int)e[b[i]].size())) {
dfs(b[i]);
start.push_back(b[i]);
}
}
if (((int)start.size()) <= 2) {
solve(start);
} else {
int uk = 0;
for (auto x : start) {
calc(x);
uk += ((int)circuit.size()) - 1;
circuit.clear();
}
if (uk > s) {
cout << -1;
return 0;
}
for (int i = 0; i < n; ++i) {
e[i].clear();
}
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) {
e[b[i]].push_back(a[i]);
}
}
int can = s - uk;
can = min(((int)start.size()), can);
vector<int> out;
for (int i = 0; i < can; ++i) {
int x = start[((int)start.size()) - i - 1];
out.push_back(pos[(long long)x * MAXN + e[x].back()].back());
}
if (((int)out.size())) {
sol.push_back(out);
}
for (int i = 0; i < can - 1; ++i) start.pop_back();
for (int i = 0; i < n; ++i) {
c[i] = a[i];
}
for (int i = 0; i < n; ++i) e[i].clear();
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) {
pos[(long long)b[i] * MAXN + a[i]].pop_back();
}
}
for (int i = 0; i < ((int)out.size()); ++i) {
c[out[(i + 1) % ((int)out.size())]] = a[out[i]];
}
for (int i = 0; i < n; ++i) {
if (c[i] != b[i]) {
e[b[i]].push_back(c[i]);
pos[(long long)b[i] * MAXN + c[i]].push_back(i);
}
}
solve(start);
}
cout << ((int)sol.size()) << endl;
for (auto v : sol) {
cout << ((int)v.size()) << "\n";
for (auto x : v) cout << x + 1 << " ";
cout << "\n";
}
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | 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;
s = min(s, n);
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;
for (int i = 0; i < n; ++i) {
if (!dead[i]) {
ans.emplace_back();
int j = i;
while (!dead[j]) {
dead[j] = true;
ans.back().push_back(j);
j = nx[j];
}
}
}
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 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) {
int ta = pre[c0], tb = nxt[c0], tc = pre[c1], td = nxt[c1];
nxt[ta] = c1, pre[c1] = ta;
nxt[tc] = c0, pre[c0] = tc;
f[fa] = fb;
}
}
}
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 = tmp + 1; i <= g1; 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 | 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 {
boolean[] visited;
int[] index;
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);
byte[] same = new byte[n];
int sum = 0;
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) {
same[i] = 1;
}
}
for (byte 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);
}
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);
}
visited = new boolean[n];
List<IntegerList> circles = new ArrayList<>(n / 2);
IntegerList reuse = new IntegerList(n);
for (int i = 0; i < n; i++) {
if (same[i] == 1 || visited[i]) {
continue;
}
reuse.clear();
circles.add(detect(i, reuse));
}
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(' ');
}
}
}
public IntegerList detect(int i, IntegerList list) {
if (visited[i]) {
return list.clone();
}
visited[i] = true;
list.add(i);
return detect(index[i], list);
}
}
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 void clear() {
size = 0;
}
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 interface IntComparator {
public int compare(int a, int b);
}
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, 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++) 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]);
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);
if (fx == fy) continue;
swap(p[x], p[y]);
rt[fx] = fy;
S[i][fy] = y;
}
}
for (int i = 1; i <= n; i++) {
if (p[i] == i) 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 | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
int gi() {
char ch = getchar();
int x = 0, q = 0;
while (ch < '0' || ch > '9') q = ch == '-' ? 1 : q, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return q ? -x : x;
}
int a[N], vis[N];
map<int, int> w;
map<int, int>::iterator it;
map<int, vector<int> > nxt;
vector<int> val;
vector<vector<int> > ans;
void dfs(int s) {
while (!nxt[s].empty()) {
int t = nxt[s].back();
vis[t] = 1, nxt[s].pop_back();
val.push_back(t), dfs(a[t]);
}
return;
}
int main() {
int n = gi(), s = gi();
for (int i = 1; i <= n; ++i) ++w[a[i] = gi()];
int l = 1;
for (it = w.begin(); it != w.end(); ++it) {
for (int j = l; j < l + it->second; ++j)
if (a[j] != it->first) nxt[it->first].push_back(j);
l += it->second;
}
for (int i = 1; i <= n; ++i)
if (!vis[i]) {
val.clear(), dfs(a[i]);
if (val.size()) s -= val.size(), ans.push_back(val);
}
if (s < 0)
puts("-1");
else {
if (s <= 2) {
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); ++i) {
printf("%d\n", ans[i].size());
for (int j = 0; j < ans[i].size(); ++j) printf("%d ", ans[i][j]);
printf("\n");
}
} else {
s = (s < ans.size() ? s : ans.size());
cout << ans.size() - s + 2 << endl;
while (ans.size() > s) {
val = ans.back(), ans.pop_back();
printf("%d\n", val.size());
for (int j = 0; j < val.size(); ++j) printf("%d ", val[j]);
printf("\n");
}
int sum = 0;
cout << s << endl;
for (int i = 0; i < s; ++i)
printf("%d ", ans[i][0]), sum += ans[i].size();
cout << endl;
cout << sum << endl;
while (ans.size()) {
val = ans.back(), ans.pop_back();
for (int j = 0; j < val.size(); ++j) printf("%d ", val[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;
const int maxn = 2e5 + 20;
int a[maxn], q[maxn], num[maxn], par[maxn];
bool visited[maxn];
vector<int> ind[maxn], cycle[maxn];
int f(int v) { return (par[v] == -1) ? v : par[v] = f(par[v]); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(par, -1, sizeof par);
int n, s;
cin >> n >> s;
vector<int> tmp;
for (int i = 0; i < n; i++) cin >> a[i], tmp.push_back(a[i]);
sort(tmp.begin(), tmp.end());
tmp.resize(unique(tmp.begin(), tmp.end()) - tmp.begin());
for (int i = 0; i < n; i++)
a[i] = lower_bound(tmp.begin(), tmp.end(), a[i]) - tmp.begin();
for (int i = 0; i < n; i++) ind[a[i]].push_back(i);
int t = 0;
for (int i = 0; i < (int)tmp.size(); i++) {
int sz = ind[i].size();
vector<int> tmp2 = ind[i];
for (auto &x : ind[i])
if (t <= x && x < t + sz) {
a[x] = x;
visited[x] = 1;
x = 1e9;
}
sort(ind[i].begin(), ind[i].end());
while (!ind[i].empty() && ind[i].back() > n) ind[i].pop_back();
tmp2 = ind[i];
for (int j = t; j < t + sz; j++)
if (!visited[j]) a[tmp2.back()] = j, tmp2.pop_back();
t += sz;
}
t = 0;
memset(par, -1, sizeof par);
for (int i = 0; i < n; i++)
if (!visited[i]) {
while (!visited[i]) {
visited[i] = 1;
cycle[t].push_back(i);
i = a[i];
}
for (int j = 1; j < (int)cycle[t].size(); j++)
par[cycle[t][j]] = cycle[t][0];
t++;
}
for (int i = 0; i < (int)tmp.size(); i++) {
if (ind[i].empty()) continue;
int marja = ind[i].back();
ind[i].pop_back();
for (auto shit : ind[i]) {
if (f(shit) == f(marja)) continue;
par[f(shit)] = f(marja);
swap(a[shit], a[marja]);
}
}
memset(visited, 0, sizeof visited);
for (int i = 0; i < t; i++) cycle[i].clear();
t = 0;
int T = 0;
for (int i = 0; i < n; i++)
if (!visited[i]) {
int sz = 0;
while (!visited[i]) {
sz++;
cycle[t].push_back(i);
visited[i] = 1;
i = a[i];
}
if (sz != 1)
t++;
else
cycle[t].clear();
}
if (!t) return cout << 0 << endl, 0;
if (t == 1) {
if (s < (int)cycle[0].size()) return cout << -1 << endl, 0;
cout << 1 << endl;
cout << cycle[0].size() << endl;
for (auto x : cycle[0]) cout << x + 1 << " ";
cout << endl;
return 0;
}
for (int i = 0; i < t; i++) T += (int)cycle[i].size();
if (s >= T + t) {
cout << 2 << endl;
cout << T << endl;
for (int i = 0; i < t; i++)
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
cout << t << endl;
for (int i = 0; i < t; i++) cout << cycle[i][0] + 1 << " ";
cout << endl;
return 0;
}
if (s < T) return cout << -1 << endl, 0;
s -= T;
if (s < 2) {
cout << t << endl;
for (int i = 0; i < t; i++) {
cout << cycle[i].size() << endl;
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
}
return 0;
}
cout << 2 + (t - s) << endl;
for (int i = s; i < t; i++) {
cout << cycle[i].size() << endl;
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
}
t = s;
T = 0;
for (int i = 0; i < t; i++) T += (int)cycle[i].size();
cout << T << endl;
for (int i = 0; i < t; i++)
for (auto x : cycle[i]) cout << x + 1 << " ";
cout << endl;
cout << t << endl;
for (int i = 0; i < t; i++) cout << cycle[i][0] + 1 << " ";
cout << endl;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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 = 0;
for (int c1 = 0; c1 < n; c1++) {
if (par(A[c1]) != par(A2[c1])) {
merg(A[c1], A2[c1]);
}
}
long long leftovers = m - nonfix;
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 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 = s; i >= 1; 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>
#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;
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;
int get() {
char ch;
while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
;
if (ch == '-') {
int s = 0;
while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
return -s;
}
int s = ch - '0';
while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
return s;
}
const int N = 2e5 + 5;
int n, s;
int a[N], b[N];
int to[N];
int q;
bool vis[N];
struct edge {
int x, id, nxt;
} e[N];
int h[N], tot;
int col[N];
map<int, int> id;
int k;
void inse(int x, int y, int id) {
e[++tot].x = y;
e[tot].id = id;
e[tot].nxt = h[x];
h[x] = tot;
}
bool used[N];
int st;
int u;
int que[N];
pair<int, int> key[N];
int be[N];
void euler(int x) {
for (; h[x];) {
int p = h[x];
int id = e[p].id;
int y = e[p].x;
que[++u] = id;
h[x] = e[p].nxt;
euler(y);
}
}
int fa[N], c;
int getfather(int x) { return fa[x] == x ? x : fa[x] = getfather(fa[x]); }
void add(int l, int r, int c) {
to[l] = r;
int x = id[a[l]];
if (be[x] && getfather(be[x]) == getfather(c)) return;
int fx = getfather(be[x]), fc = getfather(c);
if (!key[x].first)
be[x] = c, key[x] = make_pair(l, r);
else {
to[l] = key[x].second;
to[key[x].first] = r;
key[x].first = l;
fa[fx] = fc;
}
}
void getto() {
c = 0;
for (int l = 1; l <= u;) {
int r = l;
c++;
fa[c] = c;
for (; a[que[r]] != b[que[l]]; r++)
;
int st = id[b[que[l]]];
for (int i = l; i <= r - 1; i++) {
add(que[i], que[i + 1], c);
}
add(que[r], que[l], c);
l = r + 1;
}
}
int main() {
n = get();
s = get();
for (int i = 1; i <= n; i++) a[i] = get();
for (int i = 1; i <= n; i++) b[i] = a[i];
sort(b + 1, b + 1 + n);
int len = n;
for (int i = 1; i <= n; i++)
if (b[i] == a[i]) to[i] = i, len--;
if (len > s) return printf("-1\n"), 0;
for (int i = 1; i <= n; i++)
if (!id[b[i]]) col[id[b[i]] = ++k] = b[i];
for (int i = 1; i <= n; i++)
if (a[i] != b[i]) inse(id[b[i]], id[a[i]], i);
for (int i = 1; i <= n; i++) {
st = i;
u = 0;
euler(i);
getto();
}
int q = 0;
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && !vis[i]) {
q++;
vis[i] = 1;
for (int x = to[i]; x != i; x = to[x]) vis[x] = 1;
}
printf("%d\n", q);
for (int i = 1; i <= n; i++)
if (a[i] != b[i] && vis[i]) {
int len = 1;
vis[i] = 0;
for (int x = to[i]; x != i; x = to[x]) vis[x] = 0, len++;
printf("%d\n%d", len, i);
for (int x = to[i]; x != i; x = to[x]) printf(" %d", x);
putchar('\n');
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 200000;
int n, lim;
int a[MAXN];
int b[MAXN];
bool isfixed[MAXN];
pair<int, int> o[MAXN];
int no;
int dst[MAXN];
int cyc[MAXN], ncyc;
int par[MAXN], sz[MAXN];
int find(int a) {
if (par[a] == a) return a;
return par[a] = find(par[a]);
}
bool unite(int a, int b) {
a = find(a), b = find(b);
if (a == b) return false;
if (sz[a] < sz[b]) swap(a, b);
par[b] = a, sz[a] += sz[b];
return true;
}
bool done[MAXN];
vector<vector<int>> ans;
void run() {
scanf("%d%d", &n, &lim);
for (int i = (0); i < (n); ++i) scanf("%d", &a[i]);
for (int i = (0); i < (n); ++i) b[i] = a[i];
sort(b, b + n);
int nfixed = 0;
for (int i = (0); i < (n); ++i) {
isfixed[i] = a[i] == b[i];
if (isfixed[i]) ++nfixed;
}
if (n - nfixed > lim) {
printf("-1\n");
return;
}
no = 0;
for (int i = (0); i < (n); ++i)
if (!isfixed[i]) o[no++] = make_pair(a[i], i);
sort(o, o + no);
for (int i = (0); i < (n); ++i) dst[i] = isfixed[i] ? i : -1;
{
int at = 0;
for (int i = (0); i < (no); ++i) {
while (at < n && isfixed[at]) ++at;
dst[o[i].second] = at++;
}
}
for (int i = (0); i < (n); ++i) cyc[i] = -1;
ncyc = 0;
for (int i = (0); i < (n); ++i)
if (cyc[i] == -1 && dst[i] != i) {
int at = i;
while (cyc[at] == -1) {
cyc[at] = ncyc;
at = dst[at];
}
++ncyc;
}
for (int i = (0); i < (ncyc); ++i) par[i] = i, sz[i] = 1;
for (int l = 0, r = l; l < no; l = r) {
while (r < no && o[l].first == o[r].first) ++r;
int cur = o[l].second;
for (int i = (l + 1); i < (r); ++i) {
int oth = o[i].second;
if (unite(cyc[cur], cyc[oth])) {
swap(dst[cur], dst[oth]);
}
}
}
for (int i = (0); i < (n); ++i) done[i] = false;
ans.clear();
for (int i = (0); i < (n); ++i)
if (!done[i] && dst[i] != i) {
ans.push_back(vector<int>());
int at = i;
while (!done[at]) {
done[at] = true;
ans.back().push_back(at);
at = dst[at];
}
}
printf("%d\n", ((int)(ans).size()));
for (int i = (0); i < (((int)(ans).size())); ++i) {
printf("%d\n", ((int)(ans[i]).size()));
for (int j = (0); j < (((int)(ans[i]).size())); ++j) {
if (j != 0) printf(" ");
printf("%d", ans[i][j] + 1);
}
puts("");
}
}
int main() {
run();
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, s;
cin >> n >> s;
int a[n + 1], b[n + 1], next[n + 1];
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
b[i] = a[i];
next[i] = i;
}
sort(b, b + n);
a[n] = b[n] = -1;
int cnt = 0;
vector<vector<int> > ans;
for (int i = 0; i < n; i++) {
if (b[i] == a[i]) continue;
ans.push_back(vector<int>(1, i));
cnt++;
auto &v = ans.back();
loop:
int &j = next[lower_bound(b, b + n, a[i]) - b];
while (a[j] == a[i]) j++;
if (b[j] == a[i]) {
cnt++;
v.push_back(j);
swap(a[i], a[j]);
goto loop;
}
}
if (cnt > s) {
printf("-1\n");
} else {
printf("%d\n", ans.size());
while (ans.size() > 0) {
auto &v = ans.back();
printf("%d\n", v.size());
for (int i : v) {
printf("%d ", i + 1);
}
printf("\n");
ans.pop_back();
}
}
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 = 4e5 + 10;
int Begin[N], Next[N], to[N], e;
void add(int u, int v) { to[++e] = v, Next[e] = Begin[u], Begin[u] = e; }
int n, m, k, c;
int A[N], rk[N], st[N], vis[N];
void DFS(int o) {
vis[o] = true;
for (int &i = Begin[o]; i;) {
int u = to[i];
i = Next[i];
DFS(u);
st[++c] = o;
}
}
bool cmp(int x, int y) { return A[x] < A[y]; }
int L[N], R[N], pos[N];
vector<int> seq[N], cyc[N];
bool used[N];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &A[i]), rk[i] = i;
sort(rk + 1, rk + n + 1, cmp);
for (int i = 1; i <= n; i++) L[i] = A[rk[i]] == A[rk[i - 1]] ? L[i - 1] : i;
for (int i = n; i >= 1; i--) R[i] = A[rk[i]] == A[rk[i + 1]] ? R[i + 1] : i;
for (int i = 1; i <= n; i++)
if (rk[i] >= L[i] && rk[i] <= R[i]) used[rk[i]] = true;
for (int i = 1; i <= n; i++)
if (L[i] == i) pos[i] = i;
int ans = 0;
for (int i = 1; i <= n; i++)
if (rk[i] < L[i] || rk[i] > R[i]) ++ans, add(L[rk[i]], L[i]);
if (ans > k) {
puts("-1");
return 0;
}
for (int i = 1; i <= n; i++)
if (!vis[i] && Begin[i]) {
c = 0;
DFS(i);
++m;
for (int j = c; j >= 1; j--) {
int &it = pos[st[j]];
while (used[it]) ++it;
used[it] = true, seq[m].push_back(it);
++it;
}
}
int p = 1;
c = 0;
if (k - ans > 2 && m > 2) {
int pr = min(m, k - ans);
c = 1;
for (int i = 1; i <= pr; i++)
for (int v : seq[i]) cyc[c].push_back(v);
c = 2;
for (int i = pr; i >= 1; i--) cyc[c].push_back(seq[i].front());
p = pr + 1;
}
for (int i = p; i <= m; i++) cyc[++c] = seq[i];
printf("%d\n", c);
for (int i = 1; i <= c; i++) {
printf("%lu\n", cyc[i].size());
for (int v : cyc[i]) printf("%d%c", v, v == cyc[i].back() ? '\n' : ' ');
}
return 0;
}
|
1012_E. Cycle sort | You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1.
For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
Input
The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9).
Output
If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity).
Otherwise, print a single number q— the minimum number of operations required to sort the array.
On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations.
If there are several possible answers with the optimal q, print any of them.
Examples
Input
5 5
3 2 3 1 1
Output
1
5
1 4 2 3 5
Input
4 3
2 1 4 3
Output
-1
Input
2 0
2 2
Output
0
Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | {
"input": [
"5 5\n3 2 3 1 1\n",
"4 3\n2 1 4 3\n",
"2 0\n2 2\n"
],
"output": [
"1\n5\n1 4 2 3 5 \n",
"-1\n",
"0\n"
]
} | {
"input": [
"5 0\n884430748 884430748 708433020 708433020 708433020\n",
"2 1\n1 1\n",
"2 0\n2 1\n",
"5 2\n65390026 770505072 65390026 65390026 65390026\n",
"5 4\n812067558 674124159 106041640 106041640 674124159\n",
"5 4\n167616600 574805150 651016425 150949603 379708534\n",
"5 5\n472778319 561757623 989296065 99763286 352037329\n",
"5 4\n201429826 845081337 219611799 598937628 680006294\n",
"2 2\n2 1\n",
"5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"5 6\n971458729 608568364 891718769 464295315 98863653\n",
"5 5\n641494999 641494999 228574099 535883079 535883079\n",
"5 5\n815605413 4894095 624809427 264202135 152952491\n",
"1 0\n258769137\n",
"2 0\n1 1\n",
"1 0\n2\n",
"5 4\n335381650 691981363 691981363 335381650 335381650\n",
"5 4\n81224924 319704343 319704343 210445208 128525140\n",
"5 4\n579487081 564229995 665920667 665920667 644707366\n",
"5 200000\n682659092 302185582 518778252 29821187 14969298\n"
],
"output": [
"-1\n",
"0\n",
"-1\n",
"1\n2\n2 5 \n",
"-1\n",
"-1\n",
"1\n5\n1 3 5 2 4 \n",
"1\n4\n2 5 4 3 \n",
"1\n2\n1 2 \n",
"0\n",
"2\n2\n1 5\n3\n2 3 4\n",
"1\n5\n1 4 2 5 3 \n",
"2\n3\n1 5 2\n2\n3 4\n",
"0\n",
"0\n",
"0\n",
"1\n4\n2 4 3 5 \n",
"1\n4\n2 4 3 5 \n",
"2\n2\n1 2\n2\n3 5\n",
"2\n2\n1 5\n3\n2 3 4\n"
]
} | IN-CORRECT | java | import java.io.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(' ');
}
}
}
}
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;
using INT = long long;
using pii = pair<int, int>;
int a[200100];
int tmp[200100];
int vst[200100];
int rk[200100];
int flag[200100];
int st[200100];
vector<int> ans[200100];
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
int n, s;
cin >> n >> s;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) tmp[i] = a[i];
int N = n;
sort(tmp + 1, tmp + n + 1);
for (int i = 1; i <= n; i++) {
int u = lower_bound(tmp + 1, tmp + N + 1, a[i]) - tmp;
rk[i] = u;
vst[u]++;
}
int id = 0;
int MM = 0;
for (int i = 1; i <= n; i++) {
if (flag[i]) continue;
if (rk[i] > i || rk[i] + vst[rk[i]] < i) {
id = i;
int ID = id;
MM++;
int cnt = 0;
do {
int tmp = ID;
ans[MM].push_back(ID);
flag[ID] = 1;
for (int i = rk[ID] + st[rk[ID]]; i < rk[ID] + vst[rk[ID]]; i++) {
if (flag[i]) continue;
if (rk[i] != rk[ID]) {
st[rk[ID]] = i - rk[ID];
ID = i;
break;
}
}
if (tmp == ID) break;
} while (1);
}
}
int Ans = 0;
for (int i = 1; i <= MM; i++) Ans += (int)ans[i].size();
if (Ans > s) {
cout << "-1" << endl;
return 0;
}
cout << MM << endl;
for (int i = 1; i <= MM; i++) {
cout << ans[i].size() << endl;
for (auto v : ans[i]) {
cout << v << " ";
}
cout << endl;
}
return 0;
}
|
Subsets and Splits