/*input
8 35
3 7 1 5 10 2 11 6
4 10
3 6 2 9
3 10
2 3 6 9
*/
#include <bits/stdc++.h>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define int long long
#define N
#define bit(x,y) ((x>>y)&1LL)
#define na(x) (#x) << ":" << x
ostream& operator << (ostream &os, vector<int>&x) {
for (int i = 0; i < x.size(); i++) os << x[i] << sp;
return os;
}
ostream& operator << (ostream &os, pair<int, int> x) {
cout << x.fi << sp << x.se << sp;
return os;
}
ostream& operator << (ostream &os, vector<pair<int, int> >&x) {
for (int i = 0; i < x.size(); i++) os << x[i] << endl;
return os;
}
int n, lim;
vector<int> a;
vector<int> b;
int dp[105][55][2][2][1005];
const int mod = 1e9 + 7;
int cal(int pos, int numcc, bool loc, bool roc, int diff) {
if (pos) {
diff += a[pos - 1] * (2 * numcc - loc - roc);
}
if (diff > lim) return 0;
if (pos == n) {
if (numcc != 1 || !loc || !roc) return 0;
if (diff <= lim && numcc == 1 && loc && roc) return 1;
return 0;
}
if (dp[pos][numcc][loc][roc][diff] != -1) {
return dp[pos][numcc][loc][roc][diff];
}
int &ret = dp[pos][numcc][loc][roc][diff];
ret = 0;
// make new
if (numcc <= n - pos) {
ret += cal(pos + 1, numcc + 1, loc, roc, diff) * (numcc + 1 - loc - roc);
// make new loc
if (!loc)
ret += cal(pos + 1, numcc + 1, 1, roc, diff);
// make new roc
if (!roc)
ret += cal(pos + 1, numcc + 1, loc, 1, diff);
}
if (numcc) {
// push front
ret += cal(pos + 1, numcc, loc, roc, diff) * (numcc - loc);
// push back
ret += cal(pos + 1, numcc, loc, roc, diff) * (numcc - roc);
// connect
if (numcc >= 2) ret += cal(pos + 1, numcc - 1, loc, roc, diff) * (numcc - 1);
// push front loc
if (!loc) ret += cal(pos + 1, numcc, 1, roc, diff);
// push back roc
if (!roc) ret += cal(pos + 1, numcc, loc, 1, diff);
}
ret %= mod;
return ret;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef in1code
freopen("1test.inp", "r", stdin);
#endif
cin >> n >> lim;
if (n == 1) {
cout << 1 << endl;
exit(0);
}
for (int i = 1; i <= n; i++) {
int t; cin >> t;
a.push_back(t);
}
sort(a.begin(), a.end());
for (int i = 0; i < a.size() - 1; i++) {
a[i] = a[i + 1] - a[i];
}
a.back() = 0;
memset(dp, -1, sizeof(dp));
cout << cal(0, 0, 0, 0, 0) << endl;
}
Compilation message
skyscraper.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<long long int>&)':
skyscraper.cpp:21:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < x.size(); i++) os << x[i] << sp;
^
skyscraper.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<std::pair<long long int, long long int> >&)':
skyscraper.cpp:29:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < x.size(); i++) os << x[i] << endl;
^
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:96:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a.size() - 1; i++) {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
183548 KB |
Output is correct |
2 |
Correct |
16 ms |
183548 KB |
Output is correct |
3 |
Correct |
23 ms |
183548 KB |
Output is correct |
4 |
Correct |
6 ms |
183548 KB |
Output is correct |
5 |
Correct |
16 ms |
183548 KB |
Output is correct |
6 |
Correct |
9 ms |
183548 KB |
Output is correct |
7 |
Correct |
3 ms |
183548 KB |
Output is correct |
8 |
Correct |
23 ms |
183548 KB |
Output is correct |
9 |
Correct |
16 ms |
183548 KB |
Output is correct |
10 |
Correct |
16 ms |
183548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
183548 KB |
Output is correct |
2 |
Correct |
13 ms |
183548 KB |
Output is correct |
3 |
Correct |
26 ms |
183548 KB |
Output is correct |
4 |
Correct |
29 ms |
183548 KB |
Output is correct |
5 |
Correct |
13 ms |
183548 KB |
Output is correct |
6 |
Correct |
19 ms |
183548 KB |
Output is correct |
7 |
Correct |
13 ms |
183548 KB |
Output is correct |
8 |
Correct |
16 ms |
183548 KB |
Output is correct |
9 |
Correct |
16 ms |
183548 KB |
Output is correct |
10 |
Correct |
16 ms |
183548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
183548 KB |
Output is correct |
2 |
Correct |
16 ms |
183548 KB |
Output is correct |
3 |
Correct |
23 ms |
183548 KB |
Output is correct |
4 |
Correct |
6 ms |
183548 KB |
Output is correct |
5 |
Correct |
16 ms |
183548 KB |
Output is correct |
6 |
Correct |
9 ms |
183548 KB |
Output is correct |
7 |
Correct |
3 ms |
183548 KB |
Output is correct |
8 |
Correct |
23 ms |
183548 KB |
Output is correct |
9 |
Correct |
16 ms |
183548 KB |
Output is correct |
10 |
Correct |
16 ms |
183548 KB |
Output is correct |
11 |
Correct |
13 ms |
183548 KB |
Output is correct |
12 |
Correct |
13 ms |
183548 KB |
Output is correct |
13 |
Correct |
26 ms |
183548 KB |
Output is correct |
14 |
Correct |
29 ms |
183548 KB |
Output is correct |
15 |
Correct |
13 ms |
183548 KB |
Output is correct |
16 |
Correct |
19 ms |
183548 KB |
Output is correct |
17 |
Correct |
13 ms |
183548 KB |
Output is correct |
18 |
Correct |
16 ms |
183548 KB |
Output is correct |
19 |
Correct |
16 ms |
183548 KB |
Output is correct |
20 |
Correct |
16 ms |
183548 KB |
Output is correct |
21 |
Correct |
13 ms |
183548 KB |
Output is correct |
22 |
Correct |
376 ms |
183548 KB |
Output is correct |
23 |
Correct |
106 ms |
183548 KB |
Output is correct |
24 |
Correct |
223 ms |
183548 KB |
Output is correct |
25 |
Correct |
126 ms |
183548 KB |
Output is correct |
26 |
Correct |
129 ms |
183548 KB |
Output is correct |
27 |
Correct |
99 ms |
183548 KB |
Output is correct |
28 |
Correct |
136 ms |
183548 KB |
Output is correct |
29 |
Correct |
259 ms |
183548 KB |
Output is correct |
30 |
Correct |
156 ms |
183548 KB |
Output is correct |