-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathC.cpp
More file actions
executable file
·41 lines (35 loc) · 703 Bytes
/
Copy pathC.cpp
File metadata and controls
executable file
·41 lines (35 loc) · 703 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
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
void reversort_engineering(int N, int C) {
// base case
if (C < N - 1 || C >= N * (N + 1) / 2) {
cout << "IMPOSSIBLE" << endl;
return;
}
vector<int> L(N);
C -= N - 1;
iota(L.begin(), L.end(), 1);
// construct the solution
for (int i = N - 2; i >= 0; i--) {
int cost = min(N - i - 1, C);
C -= cost;
reverse(L.begin() + i, L.begin() + i + 1 + cost);
}
for (int& x: L)
cout << x << " ";
cout << endl;
}
int main() {
int T;
cin >> T;
for (int x = 1; x <= T; x++) {
int N, C;
cin >> N >> C;
cout << "Case #" << x << ": ";
reversort_engineering(N, C);
}
return 0;
}