/**
* Problem: JOI16_skyscraper - Skyscraper
* Link: https://oj.uz/problem/view/JOI16_skyscraper
* Tags: DP CC
* Solution: xét chiều cao các tòa nhà chưa biết là h[i + 1]
**/
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 100 + 3, L = 1000 + 3, mod = 1e9 + 7;
int n, l;
int h[N];
#define add(a, b) (a += b) %= mod
ll dp[N][N][L][3];
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> l;
if (n == 1) {
cout << "1\n";
return 0;
}
for (int i = 1; i <= n; ++i) cin >> h[i];
sort(h + 1, h + n + 1);
dp[1][1][0][0] = 1;
dp[1][1][0][1] = 2;
for (int i = 1; i < n - 1; ++i)
for (int j = 1; j <= i; ++j)
for (int k = 0, nxtk; k <= l; ++k)
for (int x = 0; x < 3; ++x) if (dp[i][j][k][x]) {
nxtk = k + (j * 2 - x) * (h[i + 1] - h[i]);
if (nxtk > l) continue;
// tạo cc mới giữa 2 cc thường
add(dp[i + 1][j + 1][nxtk][x], dp[i][j][k][x] * (j - x + 1));
if (x < 2) // tạo cc mới dính với 1 đầu permu
add(dp[i + 1][j + 1][nxtk][x + 1], dp[i][j][k][x] * (2 - x));
// dán vào 1 cc
add(dp[i + 1][j][nxtk][x], dp[i][j][k][x] * (2 * j - x));
if (j != x) { // j != x để không nối 2 đầu permu
// nối 2 cc thường
add(dp[i + 1][j - 1][nxtk][x], dp[i][j][k][x] * (j - 1));
if (x < 2) // tạo cc mới nối 1 đầu permu với 1 cc
add(dp[i + 1][j][nxtk][x + 1], dp[i][j][k][x] * (2 - x));
}
}
ll sum(0);
for (int k = 0, nxtk; k <= l; ++k) {
nxtk = k + h[n] - h[n - 1];
if (nxtk <= l)
sum += dp[n - 1][1][k][1];
nxtk += h[n] - h[n - 1];
if (nxtk <= l)
sum += dp[n - 1][2][k][2];
}
cout << sum % mod << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
436 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
436 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
452 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
716 KB |
Output is correct |
19 |
Correct |
1 ms |
716 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
3 ms |
1336 KB |
Output is correct |
22 |
Correct |
63 ms |
26180 KB |
Output is correct |
23 |
Correct |
35 ms |
9804 KB |
Output is correct |
24 |
Correct |
36 ms |
14064 KB |
Output is correct |
25 |
Correct |
48 ms |
11240 KB |
Output is correct |
26 |
Correct |
44 ms |
10264 KB |
Output is correct |
27 |
Correct |
24 ms |
10840 KB |
Output is correct |
28 |
Correct |
24 ms |
13252 KB |
Output is correct |
29 |
Correct |
40 ms |
18692 KB |
Output is correct |
30 |
Correct |
37 ms |
11320 KB |
Output is correct |