Submission #1278539

#TimeUsernameProblemLanguageResultExecution timeMemory
1278539phuocrucppSkyscraper (JOI16_skyscraper)C++20
100 / 100
115 ms25604 KiB
/*ㅤ∧_∧
 ( ・∀・)
 ( つ┳⊃
ε (_)へ⌒ヽフ
 (  ( ・ω・)
 ◎―◎   ⊃  ⊃
BePhuongSuperSuwi
From TK4 - CHT
ㅤㅤ/ ⌒\____
  /・   )  \
 /ノへ ノ    /|
ノ    \\ |/_/_/*/


#include<bits/stdc++.h>
#define task "main"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define int long long
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii, ii>
#define base 341
#define MASK(i) (1ll << i)
#define oo 1e18
#define isOn(x,i) ((x) & MASK(i))
#define bitOn(x,i) ((x) | MASK(i))
#define bitOff(x,i) ((x) & ~MASK(i))
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
using namespace std;
//using namespace __gnu_pbds;
const int maxn = 1e6 + 5;

int n, l, a[maxn];
int mod = 1e9 + 7;

namespace giai1{

    int dp[(1ll << 14) + 5][102][15];

    void solve() {

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i == j) continue;
                int mask = (1ll << (i - 1)) + (1ll << (j - 1));
                dp[mask][abs(a[i] - a[j])][i - 1] = 1;
            }
        }

        for (int mask = 0; mask < (1 << n); mask++) {
            for (int s = 0; s <= l; s++) {
                vector <int> g, g2;
                for (int i = 0; i < n; i++) {
                    if (isOn(mask, i)) g.pb(i);
                    else g2.pb(i);
                }

                for (int x : g) {
                    if (dp[mask][s][x]) {
                        for (int y : g2) {
                            if (s + abs(a[x + 1] - a[y + 1]) <= l) {
                                int val = s + abs(a[x + 1] - a[y + 1]);
                                dp[bitOn(mask, y)][val][y] = (dp[bitOn(mask, y)][val][y] + dp[mask][s][x]) % mod;
                            }
                        }
                    }
                }
            }
        }

        int res = 0;
        for (int s = 0; s <= l; s++) {
            for (int i = 0; i < n; i++) {
                res = (res + dp[(1ll << n) - 1][s][i]) % mod;
            }
        }
        cout << res;
    }
}

int add(int a, int b) {
    return ((a % mod) + (b % mod)) % mod;
}

namespace giai2{
    int dp[102][102][1002][3];
    void solve() {
        dp[0][0][0][0] = 1;
        sort(a + 1, a + n + 1);

        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= n; j++) {
                for (int k = 0; k <= l; k++) {
                    for (int m = 0; m <= 2; m++) {
                        if (!dp[i][j][k][m]) continue;
                        if (2 * j - m >= 0) {
                            int cost = (2 * j - m) * (a[i + 1] - a[i]),
                                val = dp[i][j][k][m];

                           // cout << cost << " " << val << endl;
                            if (k + cost <= l) {
                                // tao 1 tplt moi
                                dp[i + 1][j + 1][k + cost][m] = add(dp[i + 1][j + 1][k + cost][m], val * (j + 1 - m));
                                // tao 1 tplt dat o bien
                                dp[i + 1][j + 1][k + cost][m + 1] = add(dp[i + 1][j + 1][k + cost][m + 1], val * (2 - m));
                                // noi 2 tplt
                                if (j >= 2)
                                    dp[i + 1][j - 1][k + cost][m] = add(dp[i + 1][j - 1][k + cost][m] , val * (j - 1));
                                // noi vao dau duoi cua 1 tplt ko phai bien
                                if (j >= 1)
                                    dp[i + 1][j][k + cost][m] = add(dp[i + 1][j][k + cost][m], val * (2 * j - m));
                                // chen 1 pt moi vao dau mut cua tplt
                                if (j >= 1)
                                    dp[i + 1][j][k + cost][m + 1] = add(dp[i + 1][j][k + cost][m + 1], val * (2 - m));
                            }
                        }
                    }
                }
            }
        }

        int res = 0;
        for (int k = 0; k <= l; k++) {
            res = (res + dp[n][1][k][2]) % mod;
        }
        if (n == 1) cout << 1;
        else cout << res;
    }
}

main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    if(fopen(task".inp","r")) {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin >> n >> l;
    for (int i = 1; i <= n; i++) cin >> a[i];

    giai2::solve();
    //if (n <= 14 && l <= 100) giai1::solve();
    //else giai2::solve();
}


Compilation message (stderr)

skyscraper.cpp:137:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  137 | main() {
      | ^~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...