-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1864B.cpp
More file actions
48 lines (45 loc) · 1000 Bytes
/
1864B.cpp
File metadata and controls
48 lines (45 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int> even;
vector<int> odd;
if (k % 2 != 0)
{
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
even.push_back(s[i]);
else
odd.push_back(s[i]);
}
sort(even.begin(), even.end());
sort(odd.begin(), odd.end());
int even_idx = 0, odd_idx = 0;
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
s[i] = even[even_idx++];
}
else
{
s[i] = odd[odd_idx++];
}
}
}
else
{
sort(s.begin(), s.end());
}
cout << s << endl;
}
}