• [洛谷] P1515 旅行


    dfs 递推 都是15ms

    递推:

    //#pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <cctype>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <ctime>
    #include <vector>
    #include <fstream>
    #include <list>
    #include <iomanip>
    #include <numeric>
    using namespace std;
    typedef long long ll;
    
    const int MAXN = 1e6 + 10;
    
    int arr[MAXN] = {0,990,1010,1970,2030,2940,3060,3930,4060,4970,5030,5990,6010,7000};
    
    int dp[MAXN] = {0};
    
    int flag = 14, a, b, n, ans = 0;
    
    int main()
    {
        //ios::sync_with_stdio(false);
    
        //cin.tie(0);     cout.tie(0);
    
        cin>>a>>b>>n;
    
        for(int i = flag; i < flag + n; i++)
            cin>>arr[i];
    
        flag += n;
    
        sort(arr, arr + flag);
    
        int i;
        for(i = 0; arr[i] <= b; i++)
        {
            if(arr[i] >= a && arr[i] <= b)
                dp[i]++;
        }
    
        for(i = 0; i < flag; i++)
        {
            for(int j = i; j >= 0; j--)
            {
                if(arr[i] - arr[j] >= a && arr[i] - arr[j] <= b)
                    dp[i] += dp[j];
            }
        }
    
        cout<<dp[flag - 1]<<endl;
    
        return 0;
    }

    2.dfs

    //#pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <cctype>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <ctime>
    #include <vector>
    #include <fstream>
    #include <list>
    #include <iomanip>
    #include <numeric>
    using namespace std;
    typedef long long ll;
    
    const int MAXN = 1e6 + 10;
    
    int arr[MAXN] = {0,990,1010,1970,2030,2940,3060,3930,4060,4970,5030,5990,6010,7000};
    
    int flag = 14, a, b, n, ans = 0;
    
    void dfs(int now)
    {
        if(now >= 7000)
        {
            ans++;
            return ;
        }
    
        for(int i = 0; i < flag; i++)
        {
            if(arr[i] - now >= a && arr[i] - now <= b)
            {
                dfs(arr[i]);
            }
        }
            
    }
    
    int main()
    {
        //ios::sync_with_stdio(false);
    
        //cin.tie(0);     cout.tie(0);
    
        cin>>a>>b>>n;
    
        for(int i = flag; i < flag + n; i++)
            cin>>arr[i];
    
        flag += n;
    
        sort(arr, arr + flag);
    
        dfs(0);
    
        cout<<ans<<endl;
    
        return 0;
    }
  • 相关阅读:
    背包问题
    阶乘尾数0的个数
    欧拉筛找素数
    威佐夫博弈
    三角形面积
    deleted
    deleted
    deleted
    deleted
    deleted
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270439.html
Copyright © 2020-2023  润新知