牛客白小月赛45

牛客白小月赛45

题目链接

A 悬崖

思路

审题!每时每刻都应在跳跃。
若第一跳没到对岸,直接结束。若能到对岸则共有 n 次跳跃
数据本身不溢出 int,但相乘会爆 int,应设 ll

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    // ios::sync_with_stdio(0);
    ll x, n;
    cin >> x >> n;
    if (n > x)
        cout << x;
    else
        cout << n * x;
    return 0;
}

B 数数

思路

观察递归函数,可知 a n s = ∑ i = 1 n 2 i − 1 = n 2 ans=\sum_{i=1}^n 2i-1=n^2 ans=i=1n2i1=n2
数据本身不溢出 int,但相乘会爆 int,应设 ll

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    // ios::sync_with_stdio(0);
    ll n;
    cin >> n;
    cout << n * n;
    return 0;
}

C 山楂

思路

表面上 ans=(3a+4b)i 需要考虑 a 和 b 的分配,但需要分配的山楂总数相同,对于 a b 的不通分配并不会影响总分的高低,只用考虑是否能将山楂全部利用,故全部三个一组,再将余数尽可能加入各组,即为最佳方案
当 a[i]>5 时余数可全部加入各组,a[i]<=5 时分类讨论
求解中涉及多次乘法,所有参与运算的变量都应设为 ll 防止溢出,因 nex mod 设为 int 而错了好几次!

代码

优化了分类讨论的部分

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll a[10];
ll ans;
int main()
{
    // ios::sync_with_stdio(0);
    for (int i = 1; i <= 8; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= 8; i++)
    {
        ll nex = a[i] / 3;
        ll mod = a[i] % 3;
        if (a[i] == 5)
            ans += 4 * i;
        else if (a[i] >= 3)
            ans += (3 * nex + mod) * i;
        a[i + 1] += nex;
    }
    cout << ans;
    return 0;
}

D 切糕

思路

括号匹配问题,合法切法即切成的每一段均合法
易知合法段的拼接仍然合法,故应寻找最小合法段的个数 cnt,cnt-1 个断点的排列组合,有 2 c n t − 1 2^{cnt-1} 2cnt1 种切法

代码

num 记录括号是否匹配,每次归零表示前缀为合法段,可以切割,小于零则右括号多余,前缀无法合法切割,该字符串也无法合法切割
cnt 很大,用快速幂计算
能合法切割的条件为 num 始终不会小于零,且无多余左括号

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned ull;
const ll M = 1e9 + 7;
ll fp(ll b, ll p)
{
    ll ret = 1;
    while (p)
    {
        if (p & 1)
            ret = ret * b % M;
        p >>= 1;
        b = b * b % M;
    }
    return ret;
}
string str;
int cnt, flag, num;
int main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);
    cin >> str;
    for (int i = 0; str[i] && !flag; i++)
    {
        if (str[i] == '(')
            num++;
        else
            num--;
        if (num < 0)
            flag = 1;
        if (num == 0) // 前缀合法
            cnt++;
    }
    if (flag == 0 && num == 0) // 一一匹配,且无多余括号
    {
        cout << fp(2, cnt - 1);
    }
    else
    {
        cout << -1;
    }
    return 0;
}

E 筑巢

思路

树形 DP
设一棵根节点为 1 的树,连通块要么包含根节点,要么在其子树上
初始状态
d p [ 0 ] [ i ] = 节点舒适度 dp[0][i]=节点舒适度 dp[0][i]=节点舒适度 表示连通块包含该节点的最大舒适度
d p [ 1 ] [ i ] = − i n f dp[1][i]=-inf dp[1][i]=inf 表示连通块在其子树上的最大舒适度
状态转移
d p [ 0 ] [ i ] = ∑ y = s o n i m a x ( d p [ y ] [ 0 ] , d p [ y ] [ 1 ] ) dp[0][i]=\sum_{y=son_i}max(dp[y][0],dp[y][1]) dp[0][i]=y=sonimax(dp[y][0],dp[y][1])
d p [ 1 ] [ i ] = w i y + m a x ( d p [ 1 ] [ y ] , 0 L L ) dp[1][i]=w_{iy}+max(dp[1][y],0LL) dp[1][i]=wiy+max(dp[1][y],0LL)用 ll 修饰常数

代码

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 100010;

int n, w[N];
struct Edge
{
    int to, val;
};
vector<Edge> tree[N];
LL dp[N][2];
void dfs(int x, int fa)
{
    dp[x][0] = -1e18, dp[x][1] = w[x];
    for (Edge e : tree[x])
    {
        int y = e.to, v = e.val;
        if (y != fa)
        {
            dfs(y, x);
            dp[x][0] = max(dp[x][0], max(dp[y][0], dp[y][1]));
            dp[x][1] = dp[x][1] + max(dp[y][1] + v, 0LL);
        }
    }
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> w[i];
    for (int i = 1; i < n; ++i)
    {
        int x, y, v;
        cin >> x >> y >> v;
        tree[x].push_back((Edge){y, v});
        tree[y].push_back((Edge){x, v});
    }
    dfs(1, 0);
    cout << max(dp[1][0], dp[1][1]);
    return 0;
}

F 交换

思路

由于询问数列长度小于 10,则可对于1~10序列执行反向指令串操作,将每个子串的操作结果存放在字典树里,每次更新最短操作数
针对字典树的存放,数据量大,但诸多叶节点不用向下扩展,故不会超出内存限制

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll M = 1e9 + 7;
pair<short, short> pr[2003];
struct
{
    int son[10] = {0};
    short mx = 5000;
} dict[12000007];
int idx = 0;
void mt(string g, short x) // 加入字典树
{
    // cout<<x<<' '<<g<<endl;
    int now = 0;
    for (int i = 0; i < 10; i++)
    {
        if (!dict[now].son[g[i] - '0'])
        {
            dict[now].son[g[i] - '0'] = ++idx;
            now = idx;
        }
        else
        {
            now = dict[now].son[g[i] - '0'];
        }
        dict[now].mx = min(dict[now].mx, x); // 更新前缀的最小操作数
    }
}
short ck(string g, int k) // 查找匹配的前缀
{
    int now = 0;
    for (int i = 0; i < k; i++)
    {
        if (!dict[now].son[g[i] - '0'])
        {
            return 5000;
        }
        else
        {
            now = dict[now].son[g[i] - '0'];
        }
    }
    return dict[now].mx;
}
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d", &pr[i].first, &pr[i].second);
    }
    for (int i = 1; i <= n; i++)
    {
        string tmp1 = "0123456789";
        // string tmp2="9876543210";
        if (i == 1)
            mt(tmp1, 0); //,mt(tmp2,0);
        for (int j = i; j >= 1; j--)
        {
            swap(tmp1[pr[j].first - 1], tmp1[pr[j].second - 1]);
            // swap(tmp2[pr[j].first-1],tmp2[pr[j].second-1]);
            mt(tmp1, i - j + 1);
            // mt(tmp2,i-j+1);
        }
    }
    int k, tp;
    while (m--)
    {
        string g1 = "0000000000";
        // string g2="0000000000";
        scanf("%d", &k);
        for (int i = 0; i < k; i++)
        {
            scanf("%d", &tp);
            tp--;
            g1[i] = tp + '0';
            // g2[i]=9-tp+'0';
        }
        tp = ck(g1, k);
        // cout<<g1<<' '<<g2<<endl;
        // tp=min(ck(g1,k),ck(g2,k));
        printf("%d\n", (tp == 5000) ? -1 : tp);
    }
    return 0;
}
### 关于小白109的信息 目前并未找到关于小白109的具体比信息或题解内容[^5]。然而,可以推测该事可能属于网举办的系列算法之一,通常这类比会涉及数据结构、动态规划、图论等经典算法问题。 如果要准备类似的事,可以通过分析其他场次的比题目来提升自己的能力。例如,在小白13中,有一道与二叉树相关的题目,其核心在于处理树的操作以及统计最终的结果[^3]。通过研究此类问题的解决方法,能够帮助理解如何高效地设计算法并优化时间复杂度。 以下是基于已有经验的一个通用解决方案框架用于应对类似场景下的批量更新操作: ```python class TreeNode: def __init__(self, id): self.id = id self.weight = 0 self.children = [] def build_tree(n): nodes = [TreeNode(i) for i in range(1, n + 1)] for node in nodes: if 2 * node.id <= n: node.children.append(nodes[2 * node.id - 1]) if 2 * node.id + 1 <= n: node.children.append(nodes[2 * node.id]) return nodes[0] def apply_operations(root, operations, m): from collections import defaultdict counts = defaultdict(int) def update_subtree(node, delta): stack = [node] while stack: current = stack.pop() current.weight += delta counts[current.weight] += 1 for child in current.children: stack.append(child) def exclude_subtree(node, total_nodes, delta): nonlocal root stack = [(root, False)] # (current_node, visited) subtree_size = set() while stack: current, visited = stack.pop() if not visited and current != node: stack.append((current, True)) for child in current.children: stack.append((child, False)) elif visited or current == node: if current != node: subtree_size.add(current.id) all_ids = {i for i in range(1, total_nodes + 1)} outside_ids = all_ids.difference(subtree_size.union({node.id})) for idx in outside_ids: nodes[idx].weight += delta counts[nodes[idx].weight] += 1 global nodes nodes = {} queue = [root] while queue: curr = queue.pop(0) nodes[curr.id] = curr for c in curr.children: queue.append(c) for operation in operations: op_type, x = operation.split(' ') x = int(x) target_node = nodes.get(x, None) if not target_node: continue if op_type == '1': update_subtree(target_node, 1) elif op_type == '2' and target_node is not None: exclude_subtree(target_node, n, 1) elif op_type == '3': path_to_root = [] temp = target_node while temp: path_to_root.append(temp) if temp.id % 2 == 0: parent_id = temp.id // 2 else: parent_id = (temp.id - 1) // 2 if parent_id >= 1: temp = nodes[parent_id] else: break for p in path_to_root: p.weight += 1 counts[p.weight] += 1 elif op_type == '4': pass # Implement similarly to other cases. result = [counts[i] for i in range(m + 1)] return result ``` 上述代码片段展示了针对特定类型的树形结构及其操作的一种实现方式。尽管它并非直接对应小白109中的具体题目,但它提供了一个可借鉴的设计思路。 ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶涟风不息

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值