#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7, MAXN = 107, MAXL = 1007;
int a[MAXN], f[MAXN][MAXL][2][MAXN][2], n, l;
void add(int &a, int b)
{
a += b;
if (a >= MOD) a -= MOD;
}
int calc(int i, int curl, int kl, int k, int kr)
{
/// kl = 1 if there is a segment connected to the left border, 0 otherwise
/// kr = 1 if there is a segment connected to the right border, 0 otherwise
/// k is the number of segments in the middle
int nxtl = curl;
if (i) nxtl += (kl + kr + 2 * k) * (a[i] - a[i - 1]); ///Add penalty
if (nxtl > l) return 0;
if (k < 0) return 0;
if (i == n - 1) return !k && (kl || kr);
if (f[i][curl][kl][k][kr] != - 1) return f[i][curl][kl][k][kr];
int ans = 0;
add(ans, calc(i + 1, nxtl, 1, k, kr)); ///Connect to left segmenet
add(ans, 1LL * calc(i + 1, nxtl, 1, k - 1, kr) * k % MOD); ///Connect to left segment and join to a middle segment
add(ans, calc(i + 1, nxtl, kl, k, 1)); ///Connect to right segment
add(ans, 1LL * calc(i + 1, nxtl, kl, k - 1, 1) * k % MOD); /// Connect to right segment and join to a middle segment
add(ans, calc(i + 1, nxtl, kl, k + 1, kr)); ///Create new segment
add(ans, 1LL * calc(i + 1, nxtl, kl, k, kr) * k * 2 % MOD); ///Connect to a middle segment
add(ans, 1LL * k * (k - 1) * calc(i + 1, nxtl, kl, k - 1, kr) % MOD); ///Use to join two middle segments
return f[i][curl][kl][k][kr] = ans;
}
int main()
{
if (fopen("tst.inp", "r"))
{
freopen("tst.inp", "r", stdin);
freopen("tst.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> l;
if (n == 1) return cout << 1, 0;
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a, a + n);
memset(f, - 1, sizeof(f));
cout << calc(0, 0, 0, 0, 0);
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:44:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
44 | freopen("tst.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:45:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
45 | freopen("tst.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
91 ms |
180836 KB |
Output is correct |
3 |
Correct |
91 ms |
180856 KB |
Output is correct |
4 |
Correct |
92 ms |
180976 KB |
Output is correct |
5 |
Correct |
89 ms |
180856 KB |
Output is correct |
6 |
Correct |
90 ms |
180964 KB |
Output is correct |
7 |
Correct |
89 ms |
180836 KB |
Output is correct |
8 |
Correct |
91 ms |
180836 KB |
Output is correct |
9 |
Correct |
92 ms |
180836 KB |
Output is correct |
10 |
Correct |
98 ms |
180996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
180836 KB |
Output is correct |
2 |
Correct |
94 ms |
180836 KB |
Output is correct |
3 |
Correct |
94 ms |
180840 KB |
Output is correct |
4 |
Correct |
94 ms |
180772 KB |
Output is correct |
5 |
Correct |
95 ms |
180836 KB |
Output is correct |
6 |
Correct |
93 ms |
180836 KB |
Output is correct |
7 |
Correct |
97 ms |
180836 KB |
Output is correct |
8 |
Correct |
94 ms |
180964 KB |
Output is correct |
9 |
Correct |
93 ms |
180856 KB |
Output is correct |
10 |
Correct |
95 ms |
180836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
91 ms |
180836 KB |
Output is correct |
3 |
Correct |
91 ms |
180856 KB |
Output is correct |
4 |
Correct |
92 ms |
180976 KB |
Output is correct |
5 |
Correct |
89 ms |
180856 KB |
Output is correct |
6 |
Correct |
90 ms |
180964 KB |
Output is correct |
7 |
Correct |
89 ms |
180836 KB |
Output is correct |
8 |
Correct |
91 ms |
180836 KB |
Output is correct |
9 |
Correct |
92 ms |
180836 KB |
Output is correct |
10 |
Correct |
98 ms |
180996 KB |
Output is correct |
11 |
Correct |
94 ms |
180836 KB |
Output is correct |
12 |
Correct |
94 ms |
180836 KB |
Output is correct |
13 |
Correct |
94 ms |
180840 KB |
Output is correct |
14 |
Correct |
94 ms |
180772 KB |
Output is correct |
15 |
Correct |
95 ms |
180836 KB |
Output is correct |
16 |
Correct |
93 ms |
180836 KB |
Output is correct |
17 |
Correct |
97 ms |
180836 KB |
Output is correct |
18 |
Correct |
94 ms |
180964 KB |
Output is correct |
19 |
Correct |
93 ms |
180856 KB |
Output is correct |
20 |
Correct |
95 ms |
180836 KB |
Output is correct |
21 |
Correct |
97 ms |
180768 KB |
Output is correct |
22 |
Correct |
366 ms |
180880 KB |
Output is correct |
23 |
Correct |
360 ms |
180964 KB |
Output is correct |
24 |
Correct |
332 ms |
180840 KB |
Output is correct |
25 |
Correct |
374 ms |
180916 KB |
Output is correct |
26 |
Correct |
318 ms |
180944 KB |
Output is correct |
27 |
Correct |
167 ms |
180836 KB |
Output is correct |
28 |
Correct |
192 ms |
180836 KB |
Output is correct |
29 |
Correct |
313 ms |
180836 KB |
Output is correct |
30 |
Correct |
381 ms |
180836 KB |
Output is correct |