Submission #1279563

#TimeUsernameProblemLanguageResultExecution timeMemory
1279563quangminh412Skyscraper (JOI16_skyscraper)C++17
100 / 100
108 ms130532 KiB
/*
  Ben Watson

  Quang Minh MasterDDDDD
*/

#include <bits/stdc++.h>
using namespace std;

#define ll long long

const string name = "test";

void solve();
signed main()
{
    if (fopen((name + ".inp").c_str(), "r"))
    {
        freopen((name + ".inp").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    solve();

    return 0;
}

// main program
const int mod = 1e9 + 7;
const int maxl = 1005;
const int maxn = 105;

int a[maxn];
int n, l;

int dp[maxn][maxn][maxl][3];
int magic(int i, int c, int cost, int ends)
{
    if (cost > l || ends > 2)
        return 0;
    if (i > 1 && c <= 0)
        return 0;
    if (i == n + 1)
        return (c == 1 && ends == 2);
    if (dp[i][c][cost][ends] != -1)
        return dp[i][c][cost][ends];

    ll ans = 0;
    int nxt_cost = cost + (2 * c - ends) * (a[i] - a[i - 1]);
    // General case 1 : do not create a new end
        // case 1 : create a new component
        ans += 1ll * (c + 1 - ends) * magic(i + 1, c + 1, nxt_cost, ends);
        // case 2 : append to the endpoints of a component
        ans += 1ll * (2 * c - ends) * magic(i + 1, c, nxt_cost, ends);
        // case 3 : merge 2 components
        ans += 1ll * (c - 1) * magic(i + 1, c - 1, nxt_cost, ends);

    // General case 2 : create a new end
        // case 1 : create a new component
        ans += 1ll * (2 - ends) * magic(i + 1, c + 1, nxt_cost, ends + 1);
        // case 2 : extend a current component
        ans += 1ll * (2 - ends) * magic(i + 1, c, nxt_cost, ends + 1);

    return dp[i][c][cost][ends] = (ans % mod);
}

void solve()
{
    cin >> n >> l;
    for (int i = 1; i <= n; i++)
        cin >> a[i];

    if (n == 1)
    {
        cout << 1 << '\n';
        return;
    }

    sort(a + 1, a + n + 1);
    memset(dp, -1, sizeof(dp));

    cout << magic(1, 0, 0, 0) << '\n';
}

Compilation message (stderr)

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