2020牛客暑期多校训练营(第九场)A、B、E、F、I、J、K题解及补题

本文提供了2020年牛客暑期多校训练营第九场的比赛回顾和题解,涉及A到K题,包括题目描述、解题思路和Python代码实现。重点讲解了B题的树形DP和K题的追及问题,以及J题的矩阵转换策略。
摘要由CSDN通过智能技术生成

2020牛客暑期多校训练营(第九场)题解及补题

比赛过程

这场有很多基础的算法题,A题高精度+表达式用python的eval函数水过去了,K题题意挺清晰的但是没有很快想到思路写的时候还有出错,用的时间有点多。B题是树形dp,这个出现的频率还挺高的,应该多熟悉熟悉。还有J题是矩阵,限制上下边界然后转化成类似序列的问题,也是挺常见的。

题解

A

题意

2 ( i ) 2(i) 2(i) 表示 2 i 2^i 2i,计算一个式子的值。

解法

在左括号 ( 前面加 ∗ ∗ ** ,然后使用python的eval函数计算即可。

代码
s = input()
t = ''
for i in s:
    if i == '(':
        t += '**'
    t += i
print(eval(t))

B

题意

有一棵树,经过一条边会消耗体力,首次到达结点会得到体力,停下来可以每秒回复一点体力。问最少需要多少秒可以从结点 1 1 1 开始遍历每个结点再回到结点 1 1 1 。每条边最多遍历两次。

解法

首先先恢复足够的体力再开始出发一定是最优的,那么答案就是补足访问所有结点过程中的最小值,所以我们需要考虑访问的顺序来使访问过程中体力最小值最大化。

每一个结点有两个权值:

ad:遍历完这个儿子能够增加的体力。

ned:遍历完这个儿子最少需要多少初始体力。

那么访问的顺序就是,体力增加为正的排在前面,然后 n e d ned ned 从小到大排序,体力增加为负的排在后面,按照 a d + n e d ad+ned ad+ned 从大到小排序。按照这个顺序访问,那么 n e d [ 1 ] ned[1] ned[1] 就是所求答案。

代码
#pragma region
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define tr t[root]
#define lson t[root << 1]
#define rson t[root << 1 | 1]
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
   
#define BUF_SIZE 100000
#define OUT_SIZE 100000
//fread->R
bool IOerror = 0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {
   
    static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
    if (p1 == pend) {
   
        p1 = buf;
        pend = buf + fread(buf, 1, BUF_SIZE, stdin);
        if (pend == p1) {
   
            IOerror = 1;
            return -1;
        }
    }
    return *p1++;
}
inline bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline bool R(T &x) {
   
    bool sign = 0;
    char ch = nc();
    x = 0;
    for (; blank(ch); ch = nc())
        ;
    if (IOerror)
        return false;
    if (ch == '-')
        sign = 1, ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc())
        x = x * 10 + ch - '0';
    if (sign)
        x = -x;
    return true;
}
inline bool R(double &x) {
   
    bool sign = 0;
    char ch = nc();
    x = 0;
    for (; blank(ch); ch = nc())
        ;
    if (IOerror)
        return false;
    if (ch == '-')
        sign = 1, ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc())
        x = x * 10 + ch - '0';
    if (ch == '.') {
   
        double tmp = 1;
        ch = nc();
        for (; ch >= '0' && ch <= '9'; ch = nc())
            tmp /= 10.0, x += tmp * (ch - '0');
    }
    if (sign)
        x = -x;
    return true;
}
inline bool R(char *s) {
   
    char ch = nc();
    for (; blank(ch); ch = nc())
        ;
    if (IOerror)
        return false;
    for (; !blank(ch) && !IOerror; ch = nc())
        *s++ = ch;
    *s = 0;
    return true;
}
inline bool R(char &c) {
   
    c = nc();
    if (IOerror) {
   
        c = -1;
        return false;
    }
    return true;
}
template <class T, class... U>
bool R(T &h, U &... t) {
    return R(h) && R(t...); }
#undef OUT_SIZE
#undef BUF_SIZE
};  // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) {
    cout << x; }
void _W(const int &x) {
    printf("%d", x); }
void _W(const int64_t &x) {
    printf("%lld", x); }
void _W(const double &x) {
    printf("%.16f", x); }
void _W(const char &x) {
    putchar(x); }
void _W(const char *x) {
    printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
    _W(x.F), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x) {
   
    for (auto i = x.begin(); i != x.end(); _W(*i++))
        if (i != x.cbegin()) putchar(' ');
}
void W() {
   }
template <class T, class... U>
void W(const T &head, const U &... tail) {
    _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
const int maxn = 1e5 + 5;
ll a[maxn], ad[maxn], ned[maxn];
struct node {
   
    ll to, w;
    bool operator<(const node &A) const {
    return w < A.w; }
};
vector<node> g[maxn];
void dfs(int u, int fa) {
   
    ned[u] = a[u];
    ad[u] = 0;
    vector<pair<ll, node>> v1, v2;
    for (auto nex : g[u]) {
   
        int v = nex.to;
        if (v == fa) continue;
        dfs(v, u);
        ned[v] -= nex.w * 2;
        ad[v] += nex.w;
        ned[u] += ned[v];
        if (ned[v] >= 0)
            v1.push_back({
   ad[v], nex});
        else
            v2.push_back({
   ad[v] + ned[v], nex});
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    reverse(v2.begin(), v2.end());
    ll now = a[u];
    for (auto it : v1) {
   
        node son = it.second;
        if (now < ad[son.to]) ad[u] += ad[son.to] - now, now = ad[son.to];
        now += ned[son.to];
        if (now < 0) ad[u] -= now, now = 0;
    }
    for (auto it : v2) {
   
        node son = it.second;
        if (now < ad[son.to]) ad[u] += ad[son.to] - now, now = ad[son.to];
        now += ned[son.to];
        if (now < 0) ad[u] -= now, now = 0;
    }
}
int main() {
   
    int T;
    R(T);
    while (T--) {
   
        int n;
        R(n);
        rep(i, 1, n) {
   
            R(a[i]);
            g[i].clear();
        }
        rep(i, 1, n - 1) {
   
            int u, v, w;
            R(u, v, w);
            g[u].push_back({
   v, w});
            g[v].push_back({
   u, w});
        }
        dfs(1, 0);
        W(ad[1]);
    }
}

C

题意
解法
代码

D

题意
解法
代码

E

题意

∏ i =

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值