2022牛客寒假算法基础集训营第二场记录

原题链接
题解
这一场在A题上花了太多时间,应该在纠结的时候反思一下是不是思路有问题,正常的做法应该是不会太麻烦的。

A-小沙的炉石

一种朴素的做法是二分,假设我们有a张攻击牌(以A代替),b张回复牌(以B代替),那么最小的攻击方式是ABABAB…,最大的攻击方式是BBBB…AAAA…,ABABAB…可以通过将A与后面相邻的B互换位置来时攻击值加一,从而最小到最大的这个区间是连续的,只要目标位于这个区间内就是可行的。那么我们就二分攻击牌使用的数目,判断是否可行即可。

//二分做法
//为了防止溢出 全部变量采用long long类型
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    long long a, b, k, q;
    cin >> a >> b >> k;
    if (a > b + 1) a = b + 1;
    while (k--)
    {
        cin >> q;
        long long l = 1, r = a, mid, flag = 0;
        while (l <= r)
        {
            mid = (l + r) / 2;
            long long minx = mid * mid;
            long long maxx = (2 * b + mid + 1) * mid / 2;
            if (q < minx)
                r = mid - 1;
            else if (q > maxx)
                l = mid + 1;
            else
            {
                flag = 1;
                break;
            }
        }
        if (flag)
            cout << "YES\n";
        else
            cout << "NO\n";
    }
    return 0;
}

如果加以观察,可以发现一些区间是有交集的。实际上,当a>=4时区间都可以合并(因为攻击牌使用两张时最小值为4),因此只要在[1,max]之间的询问都是可行的,再对a<4的情况进行特判即可。

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

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    long long a, b, k, q, MAX;
    cin >> a >> b >> k;
    if (a > b + 1) a = b + 1;
    MAX = (2 * b + a + 1) * a / 2;
    while (k--)
    {
        cin >> q;
        if (a == 2 && b == 1)
        {
            if (q == 3 || q > MAX)
                cout << "NO\n";
            else
                cout << "YES\n";
            continue;
        }
        if (a == 3 && b == 2)
        {
            if (q == 8 || q > MAX)
                cout << "NO\n";
            else
                cout << "YES\n";
            continue;
        }
        if (q <= MAX)
            cout << "YES\n";
        else
            cout << "NO\n";
    }
    return 0;
}

B-小沙的魔法

这题用到了并查集和生成树的思想。比较经典的题,其实不算难
可以先将所有城市要上升的高度进行排序,然后将相同高度的城市放在一起形成一个集合。接着从最大的高度开始,遍历这个集合,搜索与其有连边的城市,如果两个城市有连边、高度相同、不属于同一个连通集,就将其连接,这样在计数时就可以少算一个城市了。

AC代码:3364ms 如果用快读可以把时间压缩到1300ms左右

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

unordered_map<int, int> idx;
vector<int> edge[N], sv[N], mp; // edge保存连边 sv保存相同高度的城市 mp为高度集合
int s[N], f[N];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n, m, cnt = 0;
    long long ans = 0;
    mp.push_back(0); //加入一个0高度,方便后续计算
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> s[i];
        mp.push_back(s[i]);
        f[i] = i;
    }
    for (int i = 1; i <= m; i++)
    {
        int x, y;
        cin >> x >> y;
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    mp.erase(unique(mp.begin(), mp.end()), mp.end()); //去除重复的高度
    sort(mp.begin(), mp.end());                       //升序排序
    for (int i = 0; i < mp.size(); i++)               //记录高度对应的集合编号
        idx[mp[i]] = i;
    for (int i = 1; i <= n; i++) //将相同高度的城市加入集合
        sv[idx[s[i]]].push_back(i);
    for (int i = mp.size() - 1; i > 0; i--)
    {
        cnt += sv[i].size(); //加入当前集合的城市数
        for (int x : sv[i])
        {
            //遍历这个城市连出的边
            for (int y : edge[x])
            {
                if (s[y] >= s[x]) //只能与高度相同或更高的城市连接
                {
                    int fx = find(x), fy = find(y);
                    if (fx != fy)
                    {
                        cnt--; //加入连通集,计数的城市减一
                        f[fx] = fy;
                    }
                }
            }
        }
        ans += (mp[i] - mp[i - 1]) * cnt; //需要上升的高度乘上需要计数的城市数
    }
    cout << ans;
    return 0;
}

G-小沙的身法

这题用LCA,一个正向的前缀和以及一个反向的前缀和。因为只有从低处到高处的时候才要花费体力,因此要两个方向的前缀和,用于记录从i到根的花费和根到i的花费。在最终计算的时候有一个巧妙的点就是减去LCA(u,v)的正反前缀和即可抵消不同方向带来的差。

#include <bits/stdc++.h>
#define N 1000007
#define M 21
#define fre(f) freopen(f ".in", "r", stdin)
using namespace std;

vector<int> v[N];
long long cost[N], icost[N];
int h[N], fa[N][M], deep[N];

void dfs(int node, int parent) //完成建图 并且初始化cost和icost
{
    fa[node][0] = parent;
    deep[node] = deep[parent] + 1;
    cost[node] = cost[parent];
    icost[node] = icost[parent];

    if (h[node] < h[parent])
        cost[node] += h[parent] - h[node];
    else
        icost[node] += h[node] - h[parent];

    for (int i = 1; i < M; i++)
        fa[node][i] = fa[fa[node][i - 1]][i - 1];

    for (int i = 0; i < v[node].size(); i++)
        if (v[node][i] != parent) dfs(v[node][i], node);
}

int lca(int x, int y)
{
    if (deep[x] < deep[y]) swap(x, y);
    int delta = deep[x] - deep[y];
    for (int i = 0; delta; i++, delta >>= 1)
        if (delta & 1) x = fa[x][i];
    if (x == y) return x;
    for (int i = M - 1; i >= 0; i--)
    {
        if (fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    memset(fa[0], 0, sizeof(fa[0]));
    deep[0] = cost[0] = icost[0] = h[0] = 0;
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> h[i];
    for (int i = 1; i < n; i++)
    {
        int x, y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1, 0);
    while (m--)
    {
        int x, y, f;
        cin >> x >> y;
        f = lca(x, y);
        cout << h[x] + cost[x] + icost[y] - cost[f] - icost[f] << "\n";
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值