其他分享
首页 > 其他分享> > 每日构造/DP(4.20)

每日构造/DP(4.20)

作者:互联网

D. Optimal Partition

#include <bits/stdc++.h>
#define IOS                           \
    std::ios::sync_with_stdio(false); \
    std::cin.tie(0);                  \
    std::cout.tie(0);
using ll = long long;
const ll inf = 1e18;

#define lson o << 1
#define rson o << 1 | 1
struct SegTree
{
    const int n;
    std::vector<ll> max;
    SegTree(int n) : n(n), max(n << 2 | 1, -inf){};

    void pushup(int o) { max[o] = std::max(max[lson], max[rson]); }

    void update(int o, int l, int r, int x, ll y)
    {
        if (r < x || x < l)
            return;
        if (l == x && r == x)
        {
            max[o] = std::max(max[o], y);
            return;
        }
        int mid = (l + r) >> 1;
        if (x <= mid)
            update(lson, l, mid, x, y);
        if (x > mid)
            update(rson, mid + 1, r, x, y);
        pushup(o);
    }

    ll query(int o, int l, int r, int x, int y)
    {
        if (r < x || y < l)
            return -inf;
        if (x <= l && r <= y)
            return max[o];
        int mid = l + r >> 1;
        return std::max(query(lson, l, mid, x, y), query(rson, mid + 1, r, x, y));
    }
};

ll solve()
{
    int n;
    std::cin >> n;
    std::vector<ll> a(n + 1), s(n + 1), ss(n + 1), f(n + 1, -inf);
    for (int i = 1; i <= n; ++i)
        std::cin >> a[i];
    for (int i = 1; i <= n; ++i)
        ss[i] = s[i] = s[i - 1] + a[i];
    std::sort(ss.begin(), ss.end());
    ss.erase(std::unique(ss.begin(), ss.end()), ss.end());
    for (int i = 0; i <= n; ++i)
        s[i] = std::lower_bound(ss.begin(), ss.end(), s[i]) - ss.begin() + 1;
    int nn = ss.size();
    SegTree t[3] = {nn, nn, nn}; // f[i] - i, f[i], f[i] + i
    for (int i = 0; i < 3; ++i)
        t[i].update(1, 1, nn, s[0], 0);
    for (int i = 1; i <= n; ++i)
    {
        f[i] = std::max({t[0].query(1, 1, nn, 1, s[i] - 1) + i, t[1].query(1, 1, nn, s[i], s[i]), t[2].query(1, 1, nn, s[i] + 1, nn) - i});
        t[0].update(1, 1, nn, s[i], f[i] - i);
        t[1].update(1, 1, nn, s[i], f[i]);
        t[2].update(1, 1, nn, s[i], f[i] + i);
    }
    return f[n];
}

int main()
{
    IOS;
    int t;
    std::cin >> t;
    while (t--)
        std::cout << solve() << std::endl;
    return 0;
}

E. Half Queen Cover

#include <bits/stdc++.h>
#define PII std::pair<int, int>

int main()
{
    int n, m, nn;
    std::cin >> n;
    std::vector<PII> ans;
    if (n == 1)
    {
        std::cout << "1\n1 1" << std::endl;
        return 0;
    }
    if (n % 3 == 0)
        m = n / 3;
    else if ((n - 1) % 3 == 0)
        m = (n - 1) / 3;
    else
        m = (n + 1) / 3;
    for (int i = 1; i <= m; ++i)
        ans.push_back({i, m - i + 1});
    for (int i = 1; i <= m - 1; ++i)
        ans.push_back({m + i, 2 * m - i});
    if (n % 3 == 0)
    {
        ans.push_back({n, n});
    }
    else if ((n - 1) % 3 == 0)
    {
        ans.push_back({n - 1, n - 1});
        ans.push_back({n, n});
    }
    std::cout << ans.size() << std::endl;
    for (auto [x, y] : ans)
        std::cout << x << " " << y << std::endl;
    return 0;
}

标签:std,cout,int,ll,mid,构造,4.20,query,DP
来源: https://www.cnblogs.com/Foreign/p/16178388.html