Submission #650039

#TimeUsernameProblemLanguageResultExecution timeMemory
650039Gromp15Skyscraper (JOI16_skyscraper)C++17
100 / 100
467 ms2340 KiB
#include <bits/stdc++.h>
#define ll long long
#define ar array
using namespace std;
const int mod = 1e9+7;
void fadd(int &a, int b) {
    a += b;
    if (a >= mod) a -= mod;
}
int mul(int a, int b) {
    return (ll)a*b%mod;
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, L; cin >> n >> L;
    vector<int> t(n);
    for (int &x : t) cin >> x;
    if (n == 1) {
        cout << 1 << '\n';
        return 0;
    }
    sort(t.begin(), t.end());
    vector<map<int, ar<int, 4>>> dp(n+1);
    // [# of components][total sum rn][(leftmost element filled) + (rightmost element filled)*2]
    dp[0][0][0] = 1;
    vector<int> pref(n);
    for (int i = 0; i < n; i++) pref[i] = t[i] + (i?pref[i-1]:0);
    for (int i = 0; i < n; i++) {
        vector<map<int, ar<int, 4>>> dp2(n+1);
        for (int j = 0; j <= n; j++) {
            for (const auto &[l, val] : dp[j]) {
                int need_merge = j-1;
                if (need_merge > 0 && (i+need_merge-1 >= n || (pref[i+need_merge-1] - (i?pref[i-1]:0))*2 + l > L)) {
                    continue;
                }
                for (int tp = 0; tp < 4; tp++) {
                    int num = val[tp];
                    if (!num) continue;
                    if (j > 1) { // merge
                        fadd(dp2[j-1][l+2*t[i]][tp], mul(num, j-1));
                    }
                    if (j > 0) { // add to comp
                        if (2*j-(tp&1)-(tp >> 1 & 1) > 0) fadd(dp2[j][l][tp], mul(num, 2*j-(tp&1)-(tp >> 1 & 1)));
                        if (!(tp & 1)) fadd(dp2[j][l+t[i]][tp|1], num);
                        if (!(tp >> 1 & 1)) fadd(dp2[j][l+t[i]][tp|2], num);
                    }
                    if (j+1 <= n) { // new comp
                        if (j+1-(tp&1)-(tp >> 1 & 1) > 0) fadd(dp2[j+1][l-2*t[i]][tp], mul(j+1-(tp&1)-(tp >> 1 & 1), num));
                        if (!(tp & 1)) fadd(dp2[j+1][l-t[i]][tp|1], num);
                        if (!(tp >> 1 & 1)) fadd(dp2[j+1][l-t[i]][tp|2], num);
                    }
                }
            }
        }
        swap(dp, dp2);
    }
    int ans = 0;
    for (int i = 0; i <= L; i++) { 
        fadd(ans, dp[1][i][3]);
    }
    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...