[BOJ] 1038 감소하는 수

Time Lapse :20min 50sec

1038.cpp

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
#include <iostream>
#include <list>
#include <map>
#include <unordered_map>
using namespace std;

unordered_map<int, list<long>> m;
int N;

long solution() {
if(N <= 9) return N;
N -= 9;
int numberOfDigits = 2;

while(numberOfDigits <= 10) {
list<long> prevComb = m[numberOfDigits - 1];
list<long> curComb;
for(auto num : prevComb) {
int endOfDigit = num % 10;
for(int i = 0; i < endOfDigit; i++) {
curComb.push_back(num * 10 + i);
N--;
if(!N) return num * 10 + i;
}
}
m[numberOfDigits++] = curComb;
}
return -1;
}

int main() {
m[1] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cin>>N;

cout<<solution();

}
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/07/03/PS/BOJ/1038/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.