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

每日构造/DP(4.21)

作者:互联网

C - ThREE

#include <bits/stdc++.h>
#define IOS                           \
    std::ios::sync_with_stdio(false); \
    std::cin.tie(0);                  \
    std::cout.tie(0);

int main()
{
    IOS;
    int n;
    std::cin >> n;
    std::vector<int> p(n + 1);
    std::queue<int> q[2]; // 0 红 1 蓝
    std::vector<std::vector<int>> g(n + 1);
    for (int i = 1, u, v; i < n; ++i)
    {
        std::cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    std::function<void(int, int, int)> dfs = [&](int u, int fa, int col)
    {
        q[col].push(u);
        for (auto v : g[u])
            if (v != fa)
                dfs(v, u, col ^ 1);
    };
    dfs(1, 0, 0);
    int Rnum = q[0].size(), Bnum = q[1].size();
    int n1 = (n - 1) / 3 + 1, n2 = (n - 2) / 3 + 1, n3 = n / 3;
    if (Rnum < n3)
    {
        int x = 1;
        while (q[0].size() && 3 * x <= n)
        {
            p[q[0].front()] = 3 * x;
            q[0].pop();
            ++x;
        }
        while (q[1].size() && 3 * x <= n)
        {
            p[q[1].front()] = 3 * x;
            q[1].pop();
            ++x;
        }
        x = 0;
        while (q[1].size() && 3 * x + 1 <= n)
        {
            p[q[1].front()] = 3 * x + 1;
            q[1].pop();
            ++x;
        }
        x = 0;
        while (q[1].size() && 3 * x + 2 <= n)
        {
            p[q[1].front()] = 3 * x + 2;
            q[1].pop();
            ++x;
        }
    }
    else if (Rnum > n1 + n3)
    {
        int x = 1;
        while (q[1].size() && 3 * x <= n)
        {
            p[q[1].front()] = 3 * x;
            q[1].pop();
            ++x;
        }
        while (q[0].size() && 3 * x <= n)
        {
            p[q[0].front()] = 3 * x;
            q[0].pop();
            ++x;
        }
        x = 0;
        while (q[0].size() && 3 * x + 1 <= n)
        {
            p[q[0].front()] = 3 * x + 1;
            q[0].pop();
            ++x;
        }
        x = 0;
        while (q[0].size() && 3 * x + 2 <= n)
        {
            p[q[0].front()] = 3 * x + 2;
            q[0].pop();
            ++x;
        }
    }
    else
    {
        int x = 0;
        while (q[0].size() && 3 * x + 1 <= n)
        {
            p[q[0].front()] = 3 * x + 1;
            q[0].pop();
            ++x;
        }
        x = 0;
        while (q[1].size() && 3 * x + 2 <= n)
        {
            p[q[1].front()] = 3 * x + 2;
            q[1].pop();
            ++x;
        }
        x = 1;
        while (q[0].size() && 3 * x <= n)
        {
            p[q[0].front()] = 3 * x;
            q[0].pop();
            ++x;
        }
        while (q[1].size() && 3 * x <= n)
        {
            p[q[1].front()] = 3 * x;
            q[1].pop();
            ++x;
        }
    }
    for (int i = 1; i <= n; ++i)
        std::cout << p[i] << " ";
    return 0;
}

D. Flowers

#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 int N = 1e5;
const int P = 1e9 + 7;

int main()
{
    IOS;
    int t, k;
    std::cin >> t >> k;
    std::vector<ll> f(N + 1), s(N + 1);
    f[0] = 1;
    for (int i = 1; i <= N; ++i)
    {
        f[i] = f[i - 1] % P;
        if (i >= k)
            f[i] = (f[i] + f[i - k]) % P;
        s[i] = (s[i] + f[i]) % P;
    }
    for (int i = 1; i <= N; ++i)
        s[i] = (s[i - 1] + s[i]) % P;
    for (int i = 1, a, b; i <= t; ++i)
    {
        std::cin >> a >> b;
        std::cout << (s[b] - s[a - 1] + P) % P << std::endl;
    }
    return 0;
}

E. Pillars

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

#define lson o << 1
#define rson o << 1 | 1
#define val first
#define id second
struct SegTree
{
    const int n;
    std::vector<PLL> max;
    SegTree(int n) : n(n), max(n << 2 | 1){};

    PLL merge(PLL a, PLL b) { return a.val > b.val ? a : b; }

    void update(int o, int l, int r, int x, PLL y)
    {
        if (l == r)
        {
            max[o] = merge(max[o], y);
            return;
        }
        int mid = (l + r) >> 1;
        if (x <= mid)
            update(lson, l, mid, x, y);
        else
            update(rson, mid + 1, r, x, y);
        max[o] = merge(max[lson], max[rson]);
    }

    PLL query(int o, int l, int r, int x, int y)
    {
        if (x <= l && r <= y)
            return max[o];
        PLL res = {0, 0};
        int mid = (l + r) >> 1;
        if (x <= mid)
            res = merge(res, query(lson, l, mid, x, y));
        if (mid < y)
            res = merge(res, query(rson, mid + 1, r, x, y));
        return res;
    }
};

int main()
{
    IOS;
    int n, d;
    std::cin >> n >> d;
    std::vector<ll> a(n + 1), b(n + 1), s(n + 1);
    for (int i = 1; i <= n; b[i] = a[i], ++i)
        std::cin >> a[i];
    std::sort(b.begin() + 1, b.begin() + n + 1);
    int nn = std::unique(b.begin() + 1, b.begin() + n + 1) - b.begin() - 1;
    // for (int i = 1; i <= n; ++i)
    //     a[i] = std::lower_bound(b.begin() + 1, b.begin() + nn + 1, a[i]) - b.begin() + 1;
    SegTree t(nn);
    std::vector<ll> f(n + 1);
    for (int i = 1, l, r, pos; i <= n; ++i)
    {
        l = std::upper_bound(b.begin() + 1, b.begin() + nn + 1, a[i] - d) - b.begin() + 1 - 1;
        r = std::lower_bound(b.begin() + 1, b.begin() + nn + 1, a[i] + d) - b.begin() + 1;
        PLL res1 = t.query(1, 1, nn, 1, l), res2 = t.query(1, 1, nn, r, nn);
        PLL res = res1.val > res2.val ? res1 : res2;
        f[i] = res.val + 1, s[i] = res.id;
        pos = std::lower_bound(b.begin() + 1, b.begin() + nn + 1, a[i]) - b.begin() + 1;
        t.update(1, 1, nn, pos, {f[i], i});
    }
    int ans = 0, pos = 0;
    for (int i = 1; i <= n; ++i)
        if (f[i] > ans)
            ans = f[i], pos = i;
    std::cout << ans << std::endl;
    std::vector<int> path;
    path.push_back(pos);
    while (s[pos])
    {
        path.push_back(s[pos]);
        pos = s[pos];
    }
    std::reverse(path.begin(), path.end());
    for (auto x : path)
        std::cout << x << " ";
    return 0;
}

标签:std,begin,cout,int,pos,构造,path,4.21,DP
来源: https://www.cnblogs.com/Foreign/p/16178397.html