Submission #1261391

#TimeUsernameProblemLanguageResultExecution timeMemory
1261391icebearSkyscraper (JOI16_skyscraper)C++20
100 / 100
55 ms2888 KiB
// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

template<class T>
    bool minimize(T &a, const T &b) {
        if (a > b) return a = b, true;
        return false;
    }

template<class T>
    bool maximize(T &a, const T &b) {
        if (a < b) return a = b, true;
        return false;
    }

#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebear"

const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 100 + 5;
int n, a[N], l;
int f[2][N][1005][3];

void add(int &x, int y) {
    x += y;
    if (x >= MOD) x -= MOD;
}

void init(void) {
    cin >> n >> l;
    FOR(i, 1, n) cin >> a[i];
    if (n == 1) {
        cout << 1;
        exit(0);
    }
}

void process(void) {
    sort(a + 1, a + n + 1);
    f[0][0][0][0] = 1;
    FOR(i, 0, n - 1) {
        memset(f[1], 0, sizeof f[1]);
        FOR(j, 0, n) FOR(s, 0, l) FOR(e, 0, 2) if (f[0][j][s][e] > 0) {
            int g = (2 * j - e) * (a[i + 1] - a[i]); // transition value
            // Case 1 : create new component without containing begin/end
            if (s + g > l) continue;
            add(f[1][j + 1][s + g][e], f[0][j][s][e]);

            // Case 2: create new component with containing begin/end
            add(f[1][j + 1][s + g][e + 1], 1LL * f[0][j][s][e] * (2 - e) % MOD);

            // Case 3: insert in an existing component without containing begin/end
            add(f[1][j][s + g][e], 1LL * f[0][j][s][e] * (2 * j - e) % MOD);

            // Case 4: insert in an existing component with containing begin/end
            if (e == 0) add(f[1][j][s + g][e + 1], 1LL * f[0][j][s][e] * 2 * j % MOD);
            else if (e == 1) {
                if (j == 1 && i + 1 == n) add(f[1][j][s + g][e + 1], f[0][j][s][e]);
                else add(f[1][j][s + g][e + 1], 1LL * f[0][j][s][e] * (j - 1) % MOD);
            }

            // Case 5: connect two existing components
            if (e == 0) add(f[1][j - 1][s + g][e], 1LL * f[0][j][s][e] * j % MOD * (j - 1) % MOD);
            else if (e == 1) add(f[1][j - 1][s + g][e], 1LL * f[0][j][s][e] * (j - 1) % MOD * (j - 1) % MOD);
            else if (j > 1) {
                if (j == 2 && i + 1 == n) add(f[1][j - 1][s + g][e], f[0][j][s][e]);
                else add(f[1][j - 1][s + g][e], 1LL * f[0][j][s][e] * (j - 1) % MOD * (j - 2) % MOD);
            }
        }
        swap(f[0], f[1]);
    }

    int ans = 0;
    FOR(s, 0, l) add(ans, f[0][1][s][2]);
    cout << ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int tc = 1;
//    cin >> tc;
    while(tc--) {
        init();
        process();
    }
    return 0;
}

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...