Submission #1115234

#TimeUsernameProblemLanguageResultExecution timeMemory
1115234manhlinh1501Skyscraper (JOI16_skyscraper)C++17
20 / 100
209 ms101532 KiB
#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)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...