Submission #941402

#TimeUsernameProblemLanguageResultExecution timeMemory
941402Hacv16Skyscraper (JOI16_skyscraper)C++17
20 / 100
438 ms91200 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 14;
const int MAXL = 101;
const int MOD = 1e9 + 7;

int n, l, a[MAXN];
int dp[1 << MAXN][MAXN][MAXL];

int32_t main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> l;

    for(int i = 0; i < n; i++)
        cin >> a[i];

    if(n <= 10)
    {
        vector<int> p(n);
        iota(p.begin(), p.end(), 0);

        int ans = 0;

        do{
            vector<int> curPerm;

            for(int i = 0; i < n; i++)
                curPerm.push_back(a[ p[i] ]);

            int curVal = 0;
            for(int i = 1; i < n; i++)
                curVal += abs(a[ p[i] ] - a[ p[i - 1] ]);

            if(curVal <= l) ans++;

        } while(next_permutation(p.begin(), p.end()));

        cout << ans << '\n';
        return 0;
    }

    for(int i = 0; i < n; i++)
        dp[(1 << i)][i][0] = 1;

    for(int mask = 1; mask < (1 << n); mask++)
    {
        for(int last = 0; last < n; last++)
        {
            if((mask & (1 << last)) == 0) continue;

            for(int pen = 0; pen <= l; pen++)
            {
                for(int prev = 0; prev < n; prev++)
                {
                    if((mask & (1 << prev)) == 0 || prev == last) continue;

                    int curPen = abs(a[last] - a[prev]);

                    if(curPen <= pen)
                        dp[mask][last][pen] = (dp[mask][last][pen] + dp[mask ^ (1 << last)][prev][pen - curPen]) % MOD;
                }
            }
        }
    }

    int ans = 0;

    for(int i = 0; i < n; i++)
        for(int j = 0; j <= l; j++)
            ans = (ans + dp[(1 << n) - 1][i][j]) % MOD;

    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...