dpM - Candies

Click to show code.


using namespace std;
const int NMAX = 1e2 + 11;
const int KMAX = 1e5 + 11;
const int MOD = 1e9 + 7;
int n, k, a[NMAX], acc[KMAX], dp[NMAX][KMAX];
int solve(void)
{
    dp[0][0] = 1;
    for (int s = 1; s <= k; ++s)
        dp[0][s] = 0;
    for (int i = 1; i <= n; ++i)
    {
        acc[0] = dp[i - 1][0];
        for (int s = 1; s <= k; ++s)
            acc[s] = (acc[s - 1] + dp[i - 1][s]) % MOD;
        for (int s = 0; s <= k; ++s)
            dp[i][s] =
                (acc[s] - (s - a[i] - 1 >= 0 ? acc[s - a[i] - 1] : 0) + MOD) %
                MOD;
    }
    return dp[n][k];
}
int main(void)
{
    cin >> n >> k;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    cout << solve() << endl;
    return 0;
}


dpL - Deque

Click to show code.


using namespace std;
using ll = long long;
const int NMAX = 3e3 + 11;
int n, a[NMAX], li, ri;
ll mem[NMAX][NMAX];
bool vis[NMAX][NMAX];
ll solve(bool maximizer)
{
    int l, r;
    ll left_ans, right_ans;
    if (li > ri)
        return 0;
    ll &ans = mem[li][ri];
    if (vis[li][ri])
        return ans;
    vis[li][ri] = true;
    l = a[li++];
    left_ans = solve(!maximizer);
    --li;
    r = a[ri--];
    right_ans = solve(!maximizer);
    ++ri;
    if (maximizer)
        ans = max(l + left_ans, r + right_ans);
    else
        ans = min(left_ans - l, right_ans - r);
    return ans;
}
int main(void)
{
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    li = 0, ri = n - 1;
    cout << solve(true) << endl;
    return 0;
}


dpK - Stones

Click to show code.


using namespace std;
const int NMAX = 1e2 + 11;
const int KMAX = 1e5 + 11;
int n, k, a[NMAX];
bool vis[KMAX], mem[KMAX];
bool wins(int stones)
{
    bool &ans = mem[stones];
    if (vis[stones])
        return ans;
    vis[stones] = true;
    for (int i = 0; i < n; ++i)
        if (stones - a[i] >= 0 and not wins(stones - a[i]))
            return (ans = true);
    return (ans = false);
}
int main(void)
{
    cin >> n >> k;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    cout << (wins(k) ? "First" : "Second") << endl;
    return 0;
}


dpI - Coins

Click to show code.


using namespace std;
const int NMAX = 3e3 + 11;
int n;
double p[NMAX], dp[NMAX][NMAX];
double solve(void)
{
    dp[0][0] = 1;
    for (int i = 1; i <= n; ++i)
        dp[0][i] = 0;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 0; j <= n; ++j)
        {
            dp[i][j] = (1 - p[i]) * dp[i - 1][j];
            if (j != 0)
                dp[i][j] += p[i] * dp[i - 1][j - 1];
        }
    }
    return accumulate(dp[n] + n / 2 + 1, dp[n] + n + 1, 0.0);
}
int main(void)
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> p[i];
    cout << setprecision(10) << solve() << endl;
    return 0;
}


dpH - Grid 1

Click to show code.


using namespace std;
const int NMAX = 1e3 + 11;
const int MOD = 1e9 + 7;
int h, w, mem[NMAX][NMAX];
bool wall[NMAX][NMAX];
int paths(void)
{
    for (int row = h - 1; row >= 0; --row)
    {
        for (int col = w - 1; col >= 0; --col)
        {
            if (wall[row][col])
                continue;
            if (row == h - 1 and col == w - 1)
                mem[row][col] = 1;
            else
                mem[row][col] = (mem[row + 1][col] + mem[row][col + 1]) % MOD;
        }
    }
    return mem[0][0];
}
int main(void)
{
    char c;
    cin >> h >> w;
    for (int row = 0; row < h; ++row)
    {
        for (int col = 0; col < w; ++col)
        {
            cin >> c;
            wall[row][col] = (c == '#');
        }
    }
    cout << paths() << endl;
    return 0;
}


dpG - Longest Path

Click to show code.


using namespace std;
using vi = vector<int>;
const int NMAX = 1e5 + 11;
int n, m, mem[NMAX];
bool vis[NMAX];
vi g[NMAX];
int dp(int u)
{
    int ans = 0;
    if (vis[u])
        return mem[u];
    vis[u] = true;
    for (auto v : g[u])
        ans = max(dp(v) + 1, ans);
    return (mem[u] = ans);
}
int main(void)
{
    int u, v, ans;
    cin >> n >> m;
    for (int i = 0; i < m; ++i)
    {
        cin >> u >> v;
        g[u].push_back(v);
    }
    ans = 0;
    for (int i = 0; i <= n; ++i)
        ans = max(ans, dp(i));
    cout << ans << endl;
    return 0;
}


dpF - Lcs

Click to show code.


using namespace std;
const int NMAX = 3e3 + 11;
string s, t;
int mem[NMAX][NMAX];
string reconstruct(int i, int j)
{
    if (i == 0 or j == 0)
        return "";
    if (s[i - 1] == t[j - 1])
        return reconstruct(i - 1, j - 1) + s[i - 1];
    else
    {
        if (mem[i - 1][j] > mem[i][j - 1])
            return reconstruct(i - 1, j);
        else
            return reconstruct(i, j - 1);
    }
}
string solve(void)
{
    int n = s.size();
    int m = t.size();
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            if (s[i - 1] == t[j - 1])
                mem[i][j] = mem[i - 1][j - 1] + 1;
            else
                mem[i][j] = max(mem[i - 1][j], mem[i][j - 1]);
        }
    }
    return reconstruct(n, m);
}
int main(void)
{
    cin >> s >> t;
    cout << solve() << endl;
    return 0;
}


dpE - Knapsack 2

Click to show code.


using namespace std;
using ll = long long;
const int NMAX = 1e2 + 11;
const int VMAX = 1e3 + 11;
int n, c, v_max, w[NMAX], v[NMAX];
ll mem[NMAX][VMAX];
ll solve(void)
{
    v_max = accumulate(v + 1, v + n + 1, 0);
    vector<vector<ll>> mem(n + 1, vector<ll>(v_max + 1, (ll)1e12));
    for (int i = 0; i <= n; ++i)
        mem[i][0] = 0;
    for (int i = 1; i <= n; ++i)
    {
        for (int vacc = 1; vacc <= v_max; ++vacc)
        {
            ll &ans = mem[i][vacc];
            if (vacc - v[i] >= 0)
                ans = min(w[i] + mem[i - 1][vacc - v[i]], mem[i - 1][vacc]);
            else
                ans = mem[i - 1][vacc];
        }
    }
    int ans = 0;
    for (int vacc = 1; vacc <= v_max; ++vacc)
        if (mem[n][vacc] <= c)
            ans = vacc;
    return ans;
}
int main(void)
{
    cin >> n >> c;
    for (int i = 1; i <= n; ++i)
        cin >> w[i] >> v[i];
    cout << solve() << endl;
    return 0;
}


dpD - Knapsack 1

Click to show code.


using namespace std;
using ll = long long;
const int NMAX = 1e2 + 11;
const int WMAX = 1e5 + 11;
int n, c, w[NMAX], v[NMAX];
ll mem[NMAX][WMAX];
bool vis[NMAX][WMAX];
ll dp(int i, int cl)
{
    ll &ans = mem[i][cl];
    if (i == n)
        return 0;
    if (vis[i][cl])
        return mem[i][cl];
    vis[i][cl] = true;
    ans = 0;
    if (cl - w[i] >= 0)
        ans = dp(i + 1, cl - w[i]) + v[i];
    return (ans = max(ans, dp(i + 1, cl)));
}
int main(void)
{
    cin >> n >> c;
    for (int i = 0; i < n; ++i)
        cin >> w[i] >> v[i];
    cout << dp(0, c) << endl;
    return 0;
}


dpC - Vacation

Click to show code.


using namespace std;
const int NMAX = 1e5 + 11;
int n, abc[NMAX][3], mem[NMAX][3];
bool vis[NMAX][3];
int dp(int i, int p)
{
    int &ans = mem[i][p];
    if (i == n)
        return 0;
    if (vis[i][p])
        return mem[i][p];
    vis[i][p] = true;
    ans = INT_MIN;
    for (int j = 0; j < 3; ++j)
    {
        if (j == p)
            continue;
        ans = max(ans, dp(i + 1, j) + abc[i][j]);
    }
    return ans;
}
int main(void)
{
    cin >> n;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < 3; ++j)
            cin >> abc[i][j];
    cout << dp(0, -1) << endl;
    return 0;
}