0129-407-第五次课-dfs2

· · 个人记录

T723530 传球游戏

题意

有n个人围成一个圈,玩传球游戏,从1好开始传,有多少种情况传了m次后传回给1号

思路

用dfs选与不选,列出所有可能的传球途径,如果最后传到的人是1号,就是一种方案

AC代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, m;
int a[N];
int sum;
void dfs(int s, int cnt){
    if(s == m + 1){
        if(cnt == 1){
            sum++;
        }
        return ;
    }
    if(cnt + 1 == n + 1){
        a[s] = 1;
        dfs(s + 1, 1);
    }else{
        a[s] = cnt + 1;
        dfs(s + 1, cnt + 1);
    }
    if(cnt - 1 == 0){
        a[s] = n;
        dfs(s + 1, n);
    }else{
        a[s] = cnt - 1;
        dfs(s + 1, cnt - 1);
    }
}
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n>> m;
    dfs(1, 1);
    cout << sum;
    return 0;
} 

赛时分析

用时

#### 得分 100分 ## T726606 dfs-选和不选-3 ### 题意 求出$1$到$n$有多少个$3-5-7$数字,$3-5-7$数字代表这个数字至少包含$1$个$3$,$1$个$5$,$1$个$7

思路

答案必须包含1个3,1个5,1个7,并且小于n

void dfs(int s){
  if(s > n){
    return ;
  }
  dfs(选3);
  dfs(选5);
  dfs(选7);
}

AC代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 15;
int n;
int res;
void dfs(int s, int c1, int c2, int c3){
    if(s > n){
        return ;
    }
    if(c1 > 0 && c2 > 0 && c3 > 0){
        res++;
    }
    dfs(s * 10 + 3, c1 + 1, c2, c3);
    dfs(s * 10 + 5, c1, c2 + 1, c3);
    dfs(s * 10 + 7, c1, c2, c3 + 1);
} 
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    dfs(0, 0, 0, 0);
    cout << res;
    return 0;
}

赛时分析

用时

#### 思路 实在没有想到正解,就决定直接写前$30$分的暴力代码 #### 得分 70分 #### 错因 暴力代码,超时了 ## T717565 多边形(Easy Version) ### 题意 有一些木棍,能组成多少个多边形,要拼成多边形要至少用$3$跟木棍,并且选的木棍的总和是否大于最长的木棍的两倍 ### 思路 用$dfs$选与不选,依次判断每根木棍是选还是不选,选的木棍的总和是否大于最长的木棍的两倍,并至少用了$3$根木棍,如果是就是一种方法 ### AC代码 ```cpp #include<bits/stdc++.h> #define int long long using namespace std; const int N = 25; int n; int a[N]; int t[N]; int res; void dfs(int s){ if(s == n + 1){ int maxn = INT_MIN; int sum = 0; int f = 0; for(int i = 1;i <= n;i++){ if(t[i] == 1){ sum += a[i]; maxn = max(maxn, a[i]); f++; } } if(sum > maxn * 2 && f >= 3){ res++; } return ; } t[s] = 1; dfs(s + 1); t[s] = 0; dfs(s + 1); } signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for(int i = 1;i <= n;i++){ cin >> a[i]; } dfs(1); cout << res; return 0; } ``` ### 赛时分析 #### 用时 $5$分钟到$10$分钟 #### 得分 100分 ## T671143 小田的素数分解 ### 题意 从$1$到$n$中挑选一些素数,这些素数的和得刚好等于$n

思路

提前处理出每个素数,用dfs选与不选来记录选的素数的和,一旦发现素数和等于n,就判断素数个数是否大于原来的个数

AC代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 205;
int n;
int a[N], v[N], h;
void f(int n){
    v[0] = 1;
    v[1] = 1;
    for(int i = 2;i <= n;i++){
        if(v[i] == 0){
            h++;
            a[h] = i;
            for(int j = 2 * i;j <= n;j += i){
                v[j] = 1;
            }
        }
    }
    return ;
}
int sum;
int maxn = 0;
void dfs(int s, int cnt, int f){
    if(cnt > n){
        return ;
    }
    if(cnt == n){
        maxn = max(maxn, f);
        return ;
    }
    if(s > h){
        return ;
    }
    dfs(s + 1, cnt + a[s], f + 1);
    dfs(s + 1, cnt, f);
}
signed main(){
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    f(200);
    cin >> n;
    dfs(1, 0, 0);
    cout << maxn;
    return 0;
}

赛时分析

用时

#### 思路 用埃式筛计算出$1$到$200$的所有质数,然后用$dfs$来枚举每一种可能 #### 得分 0分 #### 错因 埃式筛没写对,把$v[i] == 0$写成了$v[i] = 0

T726607 Air Cownditioning II B

题意

有N头奶牛,第i头奶牛占据了S_i到T_i的牛栏,第i头牛所占据的所有牛栏要降温c度,有M台空调,每台空调,使用要用m_i元,第i台空调可以覆盖a_i到b_i的牛栏,所有牛栏要降低p_i度

思路

首先用一个数组wd来标记牛栏的位置,然后使用dfs来枚举用哪台空调,判断所用的空调是否可以让所有牛栏降到合适的温度,可以就min一下

AC代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 25;
struct P{
    int a, b, m, p;
};
P q[N];
int wd[105];
int n, m;
int mk[105];
int minn = INT_MAX;
void dfs(int s, int sum){
    if(s > m){
        for(int i = 1;i <= 100;i++){
            if(mk[i] < wd[i]){
                return ;
            } 
        }
        minn = min(minn, sum);
        return ;
    }
    for(int i = q[s].a;i <= q[s].b;i++){
        mk[i] += q[s].p;
    }
    dfs(s + 1, sum + q[s].m);
    for(int i = q[s].a;i <= q[s].b;i++){
        mk[i] -= q[s].p;
    }
    dfs(s + 1, sum);
}
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n>> m;
    for(int i = 1;i <= n;i++){
        int s, t, c;
        cin >> s>> t>> c;
        for(int j = s;j <= t;j++){
            wd[j] = c;
        }
    }
    for(int i = 1;i <= m;i++){
        cin >> q[i].a>> q[i].b>> q[i].p>> q[i].m;
    }
    dfs(1, 0);
    cout << minn;
    return 0;
}

赛时分析

得分

0分

错因

没写