Saturday, February 20, 2021

BOJ 1038: 감소하는 수

* 하나의 정수는 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} 집합 내의 숫자로 이루어진다.

* 단조감소는 고려하지 않기 때문에 최대 크기의 수는 9876543210이다.

* 위 집합의 부분집합에서 조건을 만족하는 수는 단 하나: 총 210 - 1가지 경우의 수가 존재

* 가능한 수들을 모두 조합하여 cand에 저장하고, 오름차순 정렬


#include <bits/stdc++.h>


using namespace std;


vector<long long> cand;


void dfs(long long num) {

  long long last = num % 10, new_cand = num * 10;

  for(int i = 0; i < last; ++i) {

    cand.push_back(new_cand + i);

    dfs(new_cand + i);

  }

}


int main(void) {

    ios::sync_with_stdio(false);

    cin.tie(0);

    int n;

    cin >> n;

    for(int i = 0; i < 10; ++i) {

      cand.push_back(i);

      dfs(i);

    }

    sort(cand.begin(), cand.end());

    if(n < cand.size()) cout << cand[n] << '\n';

    else cout << "-1\n"; 

    return 0;

}