-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLv3_줄서는방법.cpp
More file actions
39 lines (34 loc) · 798 Bytes
/
Lv3_줄서는방법.cpp
File metadata and controls
39 lines (34 loc) · 798 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
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
vector<int> solution(int n, long long k) {
vector<int> answer;
list<int> l;
vector<long long> factorial;
factorial.push_back(1);
for (int i = 0; i < n; i++) {
l.push_back(i + 1);
factorial.push_back(factorial[i] * (i + 1));
}
// ex) 4,10
for (int i = n-1; i >=0; i--) {
long long q = k/factorial[i]; // q = 10/6 = 1
k = k % factorial[i]; // r = 10%6 = 4
if (k == 0)
q--;
list<int>::iterator iter = l.begin();
for (long long c = 0; c < q; c++)
iter++;
answer.push_back(*iter);
l.erase(iter);
if (k == 0) {
list<int>::reverse_iterator iter(l.rbegin());
for (; iter!= l.rend(); iter++)
answer.push_back(*iter);
break;
}
}
return answer;
}