N과 M (10) 15664

PublicMinsu·2023년 5월 13일
0

문제

접근 방법

N과 M (9)에서 비내림차순이라는 조건을 추가해 주었다.
인덱스를 활용해서 해결하면 된다.

코드

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> arr, ret;
int N, M;
void dfs(int idx, int depth)
{
    if (depth == M)
    {
        for (int i : ret)
            cout << i << " ";
        cout << "\n";
        return;
    }
    for (int i = idx, prev = 0; i < N; ++i)
    {
        if (prev == arr[i])
            continue;
        prev = arr[i];
        ret[depth] = arr[i];
        dfs(i + 1, depth + 1);
    }
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> N >> M;
    arr = vector<int>(N);
    ret = vector<int>(M);
    for (int i = 0; i < N; ++i)
        cin >> arr[i];
    sort(arr.begin(), arr.end());
    dfs(0, 0);
    return 0;
}

풀이

N과 M 문제는 약간의 변형을 주고 반복되는 느낌이 크다.
이전 문제에서 활용한 방식으로 해결해주면 된다.
즉 비내림차순 문제를 풀었던 경험으로 해결하면 된다.

profile
연락 : publicminsu@naver.com

0개의 댓글