# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1165816 | nhphuc | Skyscraper (JOI16_skyscraper) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
const int N = 107;
const int L = 1007;
const int mod = 1e9 + 7;
inline void add (int &a, int b){
a += b;
if (a >= mod){
a -= mod;
}
return;
}
int mul (int a, int b){
return 1ll * a * b % mod;
}
int n, l, a[N], dp[N][N][L][3], ans = 0;
int32_t main (){
ios::sync_with_stdio(false); cin.tie(nullptr);
if (fopen ("test.inp", "r")){
freopen ("test.inp", "r", stdin);
freopen ("test.out", "w", stdout);
}
cin >> n >> l;
for (int i = 1; i <= n; ++i){
cin >> a[i];
}
sort(a + 1, a + n + 1);
dp[0][0][0][0] = 1;
a[n + 1] = 10 * l;
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= n; ++j){
for (int k = 0; k <= l; ++k){
for (int e = 0; e <= 2; ++e){
int cost = (2 * j - e) * (a[i + 1] - a[i]);
if (cost > k || i + j - m - 1 > n){
continue;
}
// case 1: add new component not contains endpoint
add(dp[i][j][k][e], dp[i - 1][j - 1][k - cost][e]);
// case 2: merge into a component but not be an endpoint
add(dp[i][j][k][e], mul(dp[i - 1][j][k - cost][e], 2 * j - e));
// case 3: add new component contains endpoint
if (e >= 1){
add(dp[i][j][k][e], mul(dp[i - 1][j - 1][k - cost][e - 1], 3 - e));
}
// case 4: merge into a component and be an endpoint
if (e == 1){
add(dp[i][j][k][e], mul(2 * j, dp[i - 1][j][k - cost][e - 1]));
}
if (e == 2){
if (i == n){
add(dp[i][j][k][e], dp[i - 1][j][k - cost][e - 1]);
} else
if (j > 1){
add(dp[i][j][k][e], mul(dp[i - 1][j][k - cost][e - 1], j - 1));
}
}
// case 5: insert to join 2 component
if (e == 2){
if (i == n){
add(dp[i][j][k][e], dp[i - 1][j + 1][k - cost][e]);
} else {
add(dp[i][j][k][e], mul(dp[i - 1][j + 1][k - cost][e], j * (j - 1)));
}
} else {
if (e == 1){
add(dp[i][j][k][e], mul(dp[i - 1][j + 1][k - cost][e], j * j));
} else {
add(dp[i][j][k][e], mul(dp[i - 1][j + 1][k - cost][e], j * (j + 1)));
}
}
}
}
}
}
for (int i = 0; i <= l; ++i){
add(ans, dp[n][1][i][2]);
}
cout << ans << "\n";
}