ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

每日构造/DP(4.21)

2022-04-22 13:02:24  阅读:166  来源: 互联网

标签:std begin cout int pos 构造 path 4.21 DP


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有