-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path1512E.cpp
More file actions
executable file
·69 lines (58 loc) · 1.15 KB
/
Copy path1512E.cpp
File metadata and controls
executable file
·69 lines (58 loc) · 1.15 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Problem Code: 1512E
Time: O(n²)
Space: O(n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
#include <iostream>
#include <vector>
#include <set>
#include <utility>
using namespace std;
void permutation_by_sum(int n, int l, int r, int s) {
int len, mn, mx;
len = r - l + 1;
mn = len * (len + 1) / 2;
mx = len * (2 * n - len + 1) / 2;
if (s < mn || s > mx) {
cout << -1 << endl;
return;
}
set<int> used;
vector<int> perm(n + 1);
for (int i = 1; i <= n; i++)
used.insert(i);
for (int i = l; i <= r; i++) {
int next_s = (r - i) * (r - i + 1) / 2;
for (auto it = used.rbegin(); it != used.rend(); it++) {
int val = *it;
if (s >= next_s + val) {
perm[i] = val;
s -= val;
used.erase(next(it).base());
break;
}
}
}
for (int i = 1; i < l; i++) {
perm[i] = *used.begin();
used.erase(used.begin());
}
for (int i = r + 1; i <= n; i++) {
perm[i] = *used.begin();
used.erase(used.begin());
}
for (int i = 1; i <= n; i++)
cout << perm[i] << " ";
cout << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
int n, l, r, s;
cin >> n >> l >> r >> s;
permutation_by_sum(n, l, r, s);
}
return 0;
}