제출 #393944

#제출 시각아이디문제언어결과실행 시간메모리
393944KhaledFarhatSkyscraper (JOI16_skyscraper)C++14
100 / 100
108 ms20268 KiB
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;
int dp[105][105][1005][4];

int Add(int x, int y) {
    return (x + y) % MOD;
}

void Increase(int& x, int y) {
    x = Add(x, y);
}

int Multiply(int x, int y) {
    return 1LL * x * y % MOD;
}

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

    int n, L;
    cin >> n >> L;

    vector<int> a(n + 1);

    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }

    sort(a.begin() + 1, a.end());

    dp[2][1][0][0] = (n > 2); // middle
    dp[2][1][0][1] = 1; // left
    dp[2][1][0][2] = 1; // right
    dp[2][1][0][3] = (n == 1); // both

    for (int i = 2; i <= n; ++i) {
        int delta = a[i] - a[i - 1];

        for (int components = 1; components < i; ++components) {
            for (int balance = 0; balance <= L; ++balance) {
                for (int flag = 0; flag <= 3; ++flag) {
                    if (dp[i][components][balance][flag] == 0) {
                        continue;
                    }

                    int totalBorders = components * 2 - (flag & 1) - ((flag & 2) > 0);

                    if ((flag & 1) == 0) {
                        // place at the left side of the first component
                        int newBalance = balance + totalBorders * delta;

                        if (newBalance <= L) {
                            Increase(dp[i + 1][components][newBalance][flag | 1], dp[i][components][balance][flag]);
                        }

                        // create a new component at the left side
                        newBalance = balance + totalBorders * delta;

                        if (newBalance <= L) {
                            Increase(dp[i + 1][components + 1][newBalance][flag | 1], dp[i][components][balance][flag]);
                        }
                    }

                    if ((flag & 2) == 0) {
                        // place at the right side of the last component
                        int newBalance = balance + totalBorders * delta;

                        if (newBalance <= L) {
                            Increase(dp[i + 1][components][newBalance][flag | 2], dp[i][components][balance][flag]);
                        }

                        // create a new component at the right side
                        newBalance = balance + totalBorders * delta;

                        if (newBalance <= L) {
                            Increase(dp[i + 1][components + 1][newBalance][flag | 2], dp[i][components][balance][flag]);
                        }
                    }

                    // create a new component
                    int newBalance = balance + totalBorders * delta;
                    int validPositions = (components + 1) - (flag & 1) - ((flag & 2) > 0);

                    if (newBalance <= L) {
                        Increase(dp[i + 1][components + 1][newBalance][flag], Multiply(validPositions, dp[i][components][balance][flag]));
                    }

                    // append to the left side of a component
                    newBalance = balance + totalBorders * delta;
                    validPositions = components - (flag & 1);

                    if (newBalance <= L) {
                        Increase(dp[i + 1][components][newBalance][flag], Multiply(validPositions, dp[i][components][balance][flag]));
                    }

                    // append to the right side of a component
                    newBalance = balance + totalBorders * delta;
                    validPositions = components - ((flag & 2) > 0);

                    if (newBalance <= L) {
                        Increase(dp[i + 1][components][newBalance][flag], Multiply(validPositions, dp[i][components][balance][flag]));
                    }

                    // merge two components
                    newBalance = balance + totalBorders * delta;
                    validPositions = components - 1;

                    if (newBalance <= L) {
                        Increase(dp[i + 1][components - 1][newBalance][flag], Multiply(validPositions, dp[i][components][balance][flag]));
                    }
                }
            }
        }
    }

    int answer = 0;

    for (int balance = 0; balance <= L; ++balance) {
        Increase(answer, dp[n + 1][1][balance][3]);
    }

    cout << answer;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...