#include <bits/stdc++.h>
using namespace std;
template <class T> int size(const T &x) { return x.size(); }
#define rep(i,a,b) for (__typeof(a) i=(a); i<(b); ++i)
#define iter(it,c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
const int INF = 2147483647;
int arr[1010];
int n, l;
int mem[110][1010][2][110][2];
int mod = 1000000007;
ll dp(int at, 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 (at > 0) {
// add the penalty from the last element:
nxtl += (kl+kr+2*k)*abs(arr[at]-arr[at-1]);
}
if (nxtl > l) return 0;
if (k < 0) return 0;
if (at == n-1) {
return k == 0 ? 1 : 0;
}
if (mem[at][curl][kl][k][kr] != -1)
return mem[at][curl][kl][k][kr];
ll res = 0;
res += dp(at+1, nxtl, 1, k, kr); // connect to left segment
res += dp(at+1, nxtl, 1, k-1, kr)*k; // connect to left segment, and join to some middle segment
res += dp(at+1, nxtl, kl, k, 1); // connect to right segment
res += dp(at+1, nxtl, kl, k-1, 1)*k; // connect to right segment, and join to some middle segment
res += dp(at+1, nxtl, kl, k+1, kr); // new segment
res += dp(at+1, nxtl, kl, k, kr)*k*2; // connect to some middle segment
res += dp(at+1, nxtl, kl, k-1, kr)*k*(k-1); // join two middle segments
return mem[at][curl][kl][k][kr] = res % mod;
}
int main() {
memset(mem,-1,sizeof(mem));
cin >> n >> l;
rep(i,0,n) cin >> arr[i];
sort(arr,arr+n);
cout << dp(0, 0, 0, 0, 0) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
191608 KB |
Output is correct |
2 |
Correct |
168 ms |
191608 KB |
Output is correct |
3 |
Correct |
169 ms |
191708 KB |
Output is correct |
4 |
Correct |
166 ms |
191608 KB |
Output is correct |
5 |
Correct |
166 ms |
191712 KB |
Output is correct |
6 |
Correct |
170 ms |
191576 KB |
Output is correct |
7 |
Correct |
168 ms |
191672 KB |
Output is correct |
8 |
Correct |
172 ms |
191672 KB |
Output is correct |
9 |
Correct |
168 ms |
191672 KB |
Output is correct |
10 |
Correct |
168 ms |
191608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
191812 KB |
Output is correct |
2 |
Correct |
169 ms |
191624 KB |
Output is correct |
3 |
Correct |
167 ms |
191608 KB |
Output is correct |
4 |
Correct |
166 ms |
191612 KB |
Output is correct |
5 |
Correct |
228 ms |
191648 KB |
Output is correct |
6 |
Correct |
168 ms |
191608 KB |
Output is correct |
7 |
Correct |
168 ms |
191608 KB |
Output is correct |
8 |
Correct |
168 ms |
191684 KB |
Output is correct |
9 |
Correct |
169 ms |
191720 KB |
Output is correct |
10 |
Correct |
179 ms |
191608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
191608 KB |
Output is correct |
2 |
Correct |
168 ms |
191608 KB |
Output is correct |
3 |
Correct |
169 ms |
191708 KB |
Output is correct |
4 |
Correct |
166 ms |
191608 KB |
Output is correct |
5 |
Correct |
166 ms |
191712 KB |
Output is correct |
6 |
Correct |
170 ms |
191576 KB |
Output is correct |
7 |
Correct |
168 ms |
191672 KB |
Output is correct |
8 |
Correct |
172 ms |
191672 KB |
Output is correct |
9 |
Correct |
168 ms |
191672 KB |
Output is correct |
10 |
Correct |
168 ms |
191608 KB |
Output is correct |
11 |
Correct |
172 ms |
191812 KB |
Output is correct |
12 |
Correct |
169 ms |
191624 KB |
Output is correct |
13 |
Correct |
167 ms |
191608 KB |
Output is correct |
14 |
Correct |
166 ms |
191612 KB |
Output is correct |
15 |
Correct |
228 ms |
191648 KB |
Output is correct |
16 |
Correct |
168 ms |
191608 KB |
Output is correct |
17 |
Correct |
168 ms |
191608 KB |
Output is correct |
18 |
Correct |
168 ms |
191684 KB |
Output is correct |
19 |
Correct |
169 ms |
191720 KB |
Output is correct |
20 |
Correct |
179 ms |
191608 KB |
Output is correct |
21 |
Correct |
179 ms |
191588 KB |
Output is correct |
22 |
Correct |
448 ms |
191736 KB |
Output is correct |
23 |
Correct |
444 ms |
191736 KB |
Output is correct |
24 |
Correct |
419 ms |
191736 KB |
Output is correct |
25 |
Correct |
432 ms |
191864 KB |
Output is correct |
26 |
Correct |
393 ms |
191736 KB |
Output is correct |
27 |
Correct |
251 ms |
191728 KB |
Output is correct |
28 |
Correct |
263 ms |
191736 KB |
Output is correct |
29 |
Correct |
392 ms |
191680 KB |
Output is correct |
30 |
Correct |
443 ms |
191692 KB |
Output is correct |