1 条题解
-
0
dfs暴力枚举
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; int h = 0; // 选中的数的个数 vector<int> ans(n); // 存储选中的数 // 等价于声明函数 void dfs(int p) // 这么写的好处是, 可以在 dfs() 里直接使用局部变量, 不需要开全局变量 function<void(int)> dfs = [&](int p) { // 第 1 步: 写终止条件 if (h == m) { for (int i = 0; i < m; i++) cout << ans[i] + 1 << " \n"[i == m - 1]; return ; } // 第 2 步: 解决当前状态, 然后往下递归 // 选择 a[p], 题目要求按字典序输出, 所以优先选择 a[p] if (h < m) { ans[h++] = p; // 选中 dfs(p + 1); // 递归下去 h--; // 还原 } // 不要 p, 需要判断如果不要 p 的话后面的数够不够 r 个 if (n - p - 1 + h >= m) { dfs(p + 1); // 如果后面的数够, 就尝试不要 p } }; dfs(0); return 0; }
- 1
信息
- ID
- 146
- 时间
- 100ms
- 内存
- 32MiB
- 难度
- 2
- 标签
- 递交数
- 32
- 已通过
- 24
- 上传者