T727759 T1-dfs01
题意
从到个数字中选出个数,每个数只能用一次
思路
用组合来写就可以了
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 25;
int a[N], v[N];
int n, m;
void dfs(int s, int id){
if(s > m){
for(int i = 1;i <= m;i++){
cout << a[i]<< ' ';
}
cout << '\n';
return ;
}
for(int i = id + 1;i <= n;i++){
a[s] = i;
dfs(s + 1, i);
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> m;
dfs(1, 0);
return 0;
}赛时分析
用时
分钟到分钟
得分
分
T727327 特殊全排列
题意
输出到个数字的所有排列,不能输出的倍数
思路
用一个数组提前标记不能输出的数,然后用全排列来写就行了,注意一共有个数(为不能输出的数字数量)
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 25;
int a[N], v[N];
int n, k, m;
void dfs(int s){
if(s > n - m){
for(int i = 1;i <= n - m;i++){
cout << a[i]<< ' ';
}
cout << '\n';
return ;
}
for(int i = 1;i <= n;i++){
if(v[i] == 0){
a[s] = i;
v[i] = 1;
dfs(s + 1);
v[i] = 0;
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> k;
for(int i = 1;i <= n;i++){
if(i % k == 0){
v[i] = 1;
m++;
}
}
dfs(1);
return 0;
}赛时分析
用时
分钟到分钟
得分
分
T667194 超人
题意
要将个防御塔放到多个连续的能量舱里,每个防御塔左边要留出个格子,右边要留出个格子,请问要多少个连续的能量舱
思路
用全排列枚举出每一个超人的安排顺序,再计算每一个顺序的结果,取最大值
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 15;
int v[15];
int n;
int t[15];
int minn = INT_MAX;
struct P{
int a, b;
}c[15];
void dfs(int s){
if(s > n){
int sum = n;
for(int i = 2;i <= n;i++){
int x = max(c[t[i]].a, c[t[i - 1]].b);
sum += x;
}
minn = min(minn, sum);
return ;
}
for(int i = 1;i <= n;i++){
if(v[i] == 0){
t[s] = i;
v[i] = 1;
dfs(s + 1);
v[i] = 0;
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++){
cin >> c[i].a;
}
for(int i = 1;i <= n;i++){
cin >> c[i].b;
}
dfs(1);
cout << minn;
return 0;
}赛时分析
用时
分钟到分钟
得分
分
T727153 T4-dfs04
题意
给定两个数字和,通过反求出到数字的顺序
例:
3 1 2 4
4 3 6
7 9
16
思路
枚举出每一种数字的排列方法,然后计算每种排列的结果,用一个二维数组来记录每一轮的结果
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
int a[N], v[N];
int n, sum;
int t[N][N];
void dfs(int s){
if(s > n){
for(int i = 1;i <= n;i++){
t[1][i] = a[i];
}
for(int i = 2;i <= n;i++){
for(int j = 1;j <= n - i + 1;j++){
t[i][j] = t[i - 1][j] + t[i - 1][j + 1];
}
}
if(t[n][1] == sum){
for(int i = 1;i <= n;i++){
cout << a[i]<< ' ';
}
exit(0);
}
return ;
}
for(int i = 1;i <= n;i++){
if(!v[i]){
a[s] = i;
v[i] = 1;
dfs(s + 1);
v[i] = 0;
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> sum;
dfs(1);
return 0;
}赛时分析
用时
分钟到分钟
得分
分
错因
因为发现有啥都不输出的情况,就决定啥也不输出骗分
T727664 T5-dfs05
题意
将数字分成份,每一份不能是,如果两组分组数字都是一样的,视为一组
思路
用去重全排列枚举所有分组即可,还得加一个参数来记录结果
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int a[N], v[N];
int n, k;
int res;
void dfs(int s, int id, int sum){
if(s > k){
if(sum == n){
res++;
}
return ;
}
for(int i = id;i <= n;i++){
if(sum + i <= n){
a[s] = i;
dfs(s + 1, i, sum + i);
}else{
return ;
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> k;
dfs(1, 1, 0);
cout << res;
return 0;
}赛时分析
用时
分钟到分钟
得分
分
错因
超时了,没有提前结束代码