# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1115234 | manhlinh1501 | Skyscraper (JOI16_skyscraper) | C++17 | 209 ms | 101532 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
const int MAXN = 105;
const int MAXL = 1e3 + 5;
const int MOD = 1e9 + 7;
int N, K;
int a[MAXN];
void add(int &a, int b) {
a += b;
if(a >= MOD) a -= MOD;
}
namespace Subtask1 {
bool is_subtask() {
return N <= 8;
}
void Solution() {
if(is_subtask() == false) return;
int ans = 0;
vector<int> p(N, 0);
iota(p.begin(), p.end(), 1);
do {
int x = 0;
for(int i = 1; i < N; i++) x += abs(a[p[i - 1]] - a[p[i]]);
if(x <= K) ans++;
} while(next_permutation(p.begin(), p.end()));
cout << ans;
exit(0);
}
}
namespace Subtask2 {
bool is_subtask() {
return N <= 14 and K <= 100;
}
int dp[(1 << 14) + 5][15][105];
void Solution() {
if(is_subtask() == false) return;
for(int i = 1; i <= N; i++) dp[1 << (i - 1)][i][0] = 1;
for(int mask = 1; mask < (1 << N); mask++) {
for(int i = 1; i <= N; i++) {
if((mask >> (i - 1) & 1)) {
for(int j = 1; j <= N; j++) {
if(i == j) continue;
int x = abs(a[i] - a[j]);
// cout << i << " " << j << " " << x << "\n";
for(int z = K; z >= x; z--)
add(dp[mask][i][z], dp[mask ^ (1 << (i - 1))][j][z - x]);
}
}
}
}
int ans = 0;
for(int i = 1; i <= N; i++) {
for(int j = 0; j <= K; j++)
add(ans, dp[(1 << N) - 1][i][j]);
}
// for(int mask = 0; mask < (1 << N); mask++) {
// for(int i = 1; i <= N; i++) {
// for(int j = 0; j <= K; j++)
// if(dp[mask][i][j])
// printf("mask = %d i = %d j = %d dp = %d\n", mask, i, j, dp[mask][i][j]);
// }
// }
cout << ans << "\n";
exit(0);
}
}
signed main() {
#define task ""
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> K;
for(int i = 1; i <= N; i++) cin >> a[i];
Subtask1::Solution();
Subtask2::Solution();
return (0 ^ 0);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |