Problem Reversort
Note: The main parts of the statements of the problems “Reversort” and “Reversort Engineering” are identical, except for the last paragraph. The problems can otherwise be solved independently.
Reversort is an algorithm to sort a list of distinct integers in increasing order. The algorithm is based on the “Reverse” operation. Each application of this operation reverses the order of some contiguous part of the list.
The pseudocode of the algorithm is the following:
Reversort(L): for i := 1 to length(L) - 1 j := position with the minimum value in L between i and length(L), inclusive Reverse(L[i..j])
After i−1i−1 iterations, the positions 1,2,…,i−11,2,…,i−1 of the list contain the i−1i−1 smallest elements of L, in increasing order. During the ii-th iteration, the process reverses the sublist going from the ii-th position to the current position of the ii-th minimum element. That makes the ii-th minimum element end up in the ii-th position.
For example, for a list with 44 elements, the algorithm would perform 33 iterations. Here is how it would process L=[4,2,1,3]L=[4,2,1,3]:
- i=1, j=3⟶L=[1,2,4,3]i=1, j=3⟶L=[1,2,4,3]
- i=2, j=2⟶L=[1,2,4,3]i=2, j=2⟶L=[1,2,4,3]
- i=3, j=4⟶L=[1,2,3,4]i=3, j=4⟶L=[1,2,3,4]
The most expensive part of executing the algorithm on our architecture is the Reverse operation. Therefore, our measure for the cost of each iteration is simply the length of the sublist passed to Reverse, that is, the value j−i+1j−i+1. The cost of the whole algorithm is the sum of the costs of each iteration.
In the example above, the iterations cost 33, 11, and 22, in that order, for a total of 66.
Given the initial list, compute the cost of executing Reversort on it.
Input
The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case consists of 2 lines. The first line contains a single integer NN, representing the number of elements in the input list. The second line contains NN distinct integers L1L1, L2L2, …, LNLN, representing the elements of the input list LL, in order.
Output
For each test case, output one line containing Case #xx: yy
, where xx is the test case number (starting from 1) and yy is the total cost of executing Reversort on the list given as input.
Limits
Time limit: 10 seconds.
Memory limit: 1 GB.
Test Set 1 (Visible Verdict)
1≤T≤1001≤T≤100.
2≤N≤1002≤N≤100.
1≤Li≤N1≤Li≤N, for all ii.
Li≠LjLi≠Lj, for all i≠ji≠j.
Sample
Sample Inputsave_altcontent_copy
3 4 4 2 1 3 2 1 2 7 7 6 5 4 3 2 1
Sample Outputsave_altcontent_copy
Case #1: 6 Case #2: 1 Case #3: 12
Sample Case #1 is described in the statement above.
In Sample Case #2, there is a single iteration, in which Reverse is applied to a sublist of size 1. Therefore, the total cost is 1.
In Sample Case #3, the first iteration reverses the full list, for a cost of 7. After that, the list is already sorted, but there are 5 more iterations, each of which contributes a cost of 1.
Solution
#include<bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int t; cin >> t; for(int tt = 1; tt <= t; ++tt){ int n; cin >> n; vector<int> a(n); for(int i = 0; i < n; ++i) cin >> a[i]; long long ans = 0; for(int i = 0; i < n-1; ++i){ int pos = i; int m = a[i]; for(int j = i; j < n; ++j){ if(m > a[j]){ pos = j; m = a[j]; } } for(int j = 0; j <= (pos-i)/2; ++j){ swap(a[i+j],a[pos-j]); } ans += pos - i + 1; } cout << "Case #" << tt << ": " << ans << endl; } return 0; }
Problem
Cody-Jamal is working on his latest piece of abstract art: a mural consisting of a row of waning moons and closed umbrellas. Unfortunately, greedy copyright trolls are claiming that waning moons look like an uppercase C and closed umbrellas look like a J, and they have a copyright on CJ and JC. Therefore, for each time CJ appears in the mural, Cody-Jamal must pay XX, and for each time JC appears in the mural, he must pay YY.

Cody-Jamal is unwilling to let them compromise his art, so he will not change anything already painted. He decided, however, that the empty spaces he still has could be filled strategically, to minimize the copyright expenses.
For example, if CJ?CC?
represents the current state of the mural, with C
representing a waning moon, J
representing a closed umbrella, and ?
representing a space that still needs to be painted with either a waning moon or a closed umbrella, he could finish the mural as CJCCCC
, CJCCCJ
, CJJCCC
, or CJJCCJ
. The first and third options would require paying X+YX+Y in copyrights, while the second and fourth would require paying 2⋅X+Y2⋅X+Y.
Given the costs XX and YY and a string representing the current state of the mural, how much does Cody-Jamal need to pay in copyrights if he finishes his mural in a way that minimizes that cost?
Input
The first line of the input gives the number of test cases, TT. TT lines follow. Each line contains two integers XX and YY and a string SS representing the two costs and the current state of the mural, respectively.
Output
For each test case, output one line containing Case #xx: yy
, where xx is the test case number (starting from 1) and yy is the minimum cost that Cody-Jamal needs to pay in copyrights for a finished mural.
Limits
Time limit: 10 seconds.
Memory limit: 1 GB.
1≤T≤1001≤T≤100.
Each character of SS is either C
, J
, or ?
.
Test Set 1 (Visible Verdict)
1≤1≤ the length of SS ≤10≤10.
1≤X≤1001≤X≤100.
1≤Y≤1001≤Y≤100.
Test Set 2 (Visible Verdict)
1≤1≤ the length of SS ≤1000≤1000.
1≤X≤1001≤X≤100.
1≤Y≤1001≤Y≤100.
Extra credit!
What if some copyright holders could pay Cody-Jamal for the advertisement instead of being paid? Cody-Jamal getting paid is represented by a negative cost.
Test Set 3 (Hidden Verdict)
1≤1≤ the length of SS ≤1000≤1000.
−100≤X≤100−100≤X≤100.
−100≤Y≤100−100≤Y≤100.
Sample
Note: there are additional samples that are not run on submissions down below.Sample Inputsave_altcontent_copy
4 2 3 CJ?CC? 4 2 CJCJ 1 3 C?J 2 5 ??J???
Sample Outputsave_altcontent_copy
Case #1: 5 Case #2: 10 Case #3: 1 Case #4: 0
Sample Case #1 is the one explained in the problem statement. The minimum cost is X+Y=2+3=5X+Y=2+3=5.
In Sample Case #2, Cody-Jamal is already finished, so he does not have a choice. There are two CJ
s and one JC
in his mural.
In Sample Case #3, substituting either C
or J
results in one CJ
either from the second and third characters or the first and second characters, respectively.
In Sample Case #4, Cody-Jamal can finish his mural with all J
s. Since that contains no instance of CJ
nor JC
, it yields no copyright cost.
Additional Sample – Test Set 3
The following additional sample fits the limits of Test Set 3. It will not be run against your submitted solutions.Sample Inputsave_altcontent_copy
1 2 -5 ??JJ??
Sample Outputsave_altcontent_copy
Case #1: -8
In Sample Case #1 for Test Set 3, Cody-Jamal can finish his mural optimally as JCJJCC
or JCJJJC
. Either way, there is one CJ
and two JC
s in his mural.
Solution
#include<bits/stdc++.h using namespace std; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int t; cin >> t; for(int tt = 1; tt <= t; ++tt){ string s; int x, y; cin >> x >> y >> s; int n = s.size(); long long ans = 0; vector<int> p, q; for(int i = 0; i < n; ++i){ if(s[i] == '?') q.push_back(i); else p.push_back(i); } if(p.size() == 0){ cout << "Case #" << tt << ": " << ans << endl; continue; } for(int i = 0; i < n; ++i){ if(s[i] == '?'){ if(i > 0){ if(s[i-1] == 'C'){ if(x < 0) s[i] = 'J'; else s[i] = 'C'; }else{ if(y < 0) s[i] = 'C'; else s[i] = 'J'; } }else{ for(int j = 0; j < p[0]; ++j){ s[j] = s[p[0]]; } i = p[0]-1; } } } for(int i = 1; i < n; ++i){ if(s[i-1] == 'C' && s[i] == 'J') ans += x; else if(s[i-1] == 'J' && s[i] == 'C') ans += y; } cout << "Case #" << tt << ": " << ans << endl; } return 0; }
Problem
Note: The main parts of the statements of the problems “Reversort” and “Reversort Engineering” are identical, except for the last paragraph. The problems can otherwise be solved independently.
Reversort is an algorithm to sort a list of distinct integers in increasing order. The algorithm is based on the “Reverse” operation. Each application of this operation reverses the order of some contiguous part of the list.
The pseudocode of the algorithm is the following:
Reversort(L): for i := 1 to length(L) - 1 j := position with the minimum value in L between i and length(L), inclusive Reverse(L[i..j])
After i−1i−1 iterations, the positions 1,2,…,i−11,2,…,i−1 of the list contain the i−1i−1 smallest elements of L, in increasing order. During the ii-th iteration, the process reverses the sublist going from the ii-th position to the current position of the ii-th minimum element. That makes the ii-th minimum element end up in the ii-th position.
For example, for a list with 44 elements, the algorithm would perform 33 iterations. Here is how it would process L=[4,2,1,3]L=[4,2,1,3]:
- i=1, j=3⟶L=[1,2,4,3]i=1, j=3⟶L=[1,2,4,3]
- i=2, j=2⟶L=[1,2,4,3]i=2, j=2⟶L=[1,2,4,3]
- i=3, j=4⟶L=[1,2,3,4]i=3, j=4⟶L=[1,2,3,4]
The most expensive part of executing the algorithm on our architecture is the Reverse operation. Therefore, our measure for the cost of each iteration is simply the length of the sublist passed to Reverse, that is, the value j−i+1j−i+1. The cost of the whole algorithm is the sum of the costs of each iteration.
In the example above, the iterations cost 33, 11, and 22, in that order, for a total of 66.
You are given a size NN and a cost CC. Find a list of NN distinct integers between 1 and NN such that the cost of applying Reversort to it is exactly CC, or say that there is no such list.
Input
The first line of the input gives the number of test cases, TT. TT lines follow. Each line describes a test case with two integers NN and CC, the size of the wanted list and the desired cost, respectively.
Output
For each test case, if there is no list of size NN such that applying Reversort to it costs exactly CC, output one line containing Case #xx: IMPOSSIBLE
, where xx is the test case number (starting from 1). Otherwise, output one line containing Case #xx: y1y1 y2y2 ... yNyN
, where xx is the test case number (starting from 1) and each yiyi is a distinct integer between 11 and NN, representing the ii-th element of one such possible list.
If there are multiple solutions, you may output any one of them. (See “What if a test case has multiple correct solutions?” in the Competing section of the FAQ.) This information about multiple solutions will not be explicitly stated in the remainder of the 2021 contest.
Limits
Time limit: 10 seconds.
Memory limit: 1 GB.
1≤T≤1001≤T≤100.
1≤C≤10001≤C≤1000.
Test Set 1 (Visible Verdict)
2≤N≤72≤N≤7.
Test Set 2 (Visible Verdict)
2≤N≤1002≤N≤100.
Sample
Sample Inputsave_altcontent_copy
5 4 6 2 1 7 12 7 2 2 1000
Sample Outputsave_altcontent_copy
Case #1: 4 2 1 3 Case #2: 1 2 Case #3: 7 6 5 4 3 2 1 Case #4: IMPOSSIBLE Case #5: IMPOSSIBLE
Sample Case #1 is described in the statement above.
In Sample Case #2, the algorithm runs for only one iteration on the proposed output. In that iteration, reverse is applied to a sublist of size 1, therefore, its cost is 1.
In Sample Case #3, the first iteration reverses the full list, for a cost of 7. After that, the list is already sorted, but there are 5 more iterations, each of which contributes a cost of 1. Another valid output would be 7 5 4 3 2 1 6
. For that output, the first iteration has a cost of 6, the last one has a cost of 2, and all others have a cost of 1.
In Sample Case #4, Reversort will necessarily perform 6 iterations, each of which will have a cost of at least 1, so there is no way the total cost can be as low as required.
solution
#include<bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int t; cin >> t; for(int tt = 1; tt <= t; ++tt){ int n,c; cin >> n >> c; int mx = 0; mx = (n+1)*n/2 - 2; if(c > mx+1 || c < n-1){ cout << "Case #" << tt << ": IMPOSSIBLE" << endl; continue; } vector<int> s(n-1,1); int k = c-n+1; for(int i = 0; i < n-1; ++i){ if(k > 0){ s[i] += min(k,(n-1) - (i)); k -= min(k,(n-1) - (i)); } } vector<int> a(n,0); for(int i = 0; i < n; ++i) a[i] = i+1; for(int i = n-2; i >= 0; --i){ //cout << "test: " << i << endl; //rep(i,n) cout << a[i] << " "; //cout << endl; int l = i, r = l + s[i] - 1; //cout << "l: " << l << " r: " << r << endl; while(r-l >= 1){ swap(a[r],a[l]); l++,r--; } } cout << "Case #" << tt << ":"; for(int i = 0; i < n; ++i) cout << " " << a[i]; cout << endl; } return 0; }