牛客小白月赛101

比赛链接

https://ac.nowcoder.com/acm/contest/90072

A题 tb的区间问题

思路

实际上是求长度为 n − k n-k nk的区间的和的最大值。

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5;
int n, k;
int a[N], sum[N];
void solve()
{
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        sum[i] = sum[i - 1] + a[i];
    }
    int ans = 0;
    int len = n - k;
    for (int i = 1; i + len - 1 <= n; i++)
    {
        int j = i + len - 1;
        ans = max(ans, sum[j] - sum[i - 1]);
    }
    cout << ans << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; i++)
    {
        solve();
    }
    return 0;
}

B题 tb的字符串问题

思路

用栈从前往后模拟,类似于括号匹配。

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
string s;
void solve()
{
    cin >> n >> s;
    stack<char> st;
    for (int i = 0; i < n; i++)
    {
        if (st.empty())
        {
            st.push(s[i]);
        }
        else
        {
            if ((s[i] == 'c' && st.top() == 'f') || (s[i] == 'b' && st.top() == 't'))
            {
                st.pop();
            }
            else
                st.push(s[i]);
        }
    }
    cout << st.size() << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; i++)
    {
        solve();
    }
    return 0;
}

C题 tb的路径问题

思路

打表观察样例即可。

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
void solve()
{
    cin >> n;
    if (n == 1)
    {
        cout << 0 << endl;
        return;
    }
    if (n == 2)
    {
        cout << n << endl;
        return;
    }
    if (n == 3)
    {
        cout << 4 << endl;
        return;
    }
    if (n % 2 == 0)
    {
        cout << 4 << endl;
    }
    else
        cout << 6 << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; i++)
    {
        solve();
    }
    return 0;
}

D题 tb的平方问题

思路

n n n最大为 1 e 3 1e3 1e3,因此我们可以先用前缀和进行预处理,之后暴力枚举每一个连续子数组,对于和为完全平方数的连续子数组,我们对整个区间的个数加 1 1 1

因此,本题便转化为了区间加减的问题,可以使用树状数组进行求解。

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e3 + 5;
int n, q;
int a[N], s[N];
struct BIT
{
    const static int maxnum = 1e3 + 10;
    int tree[maxnum];
    int lowbit(int x)
    {
        return (x) & (-x);
    }
    void add(int idx, int x)
    {
        for (int pos = idx; pos < maxnum; pos += lowbit(pos))
        {
            tree[pos] += x;
        }
    }
    int query(int n)
    {
        int ans = 0;
        for (int pos = n; pos; pos -= lowbit(pos))
        {
            ans += tree[pos];
        }
        return ans;
    }
    int query(int a, int b)
    {
        return query(b) - query(a - 1);
    }
    void init(int n)
    {
        for (int i = 0; i <= n + 2; i++)
            tree[i] = 0;
    }
} tree;
bool check(int x)
{
    int op = sqrt(x);
    if (op * op == x)
        return true;
    return false;
}
void solve()
{
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        s[i] = s[i - 1] + a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = i; j <= n; j++)
        {
            int sum = s[j] - s[i - 1];
            if (check(sum))
            {
                tree.add(i, 1);
                tree.add(j + 1, -1);
            }
        }
    }
    while (q--)
    {
        int x;
        cin >> x;
        cout << tree.query(x) << endl;
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; i++)
    {
        solve();
    }
    return 0;
}

E题 tb的数数问题

思路

找每个没有出现的数,他的倍数都是false的,其余的都是true。

时间复杂度为调和级数。

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 5;
int n;
int a[N];
bool st[N];
void solve()
{
    cin >> n;
    map<int, int> mp;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        mp[a[i]]++;
    }
    int ans = 0, high = *max_element(a + 1, a + 1 + n);
    for (int i = 1; i <= high; i++)
    {
        if (mp.count(i) && !st[i])
        {
            ans++;
        }
        else
        {
            for (int j = i; j <= high; j += i)
            {
                st[j] = true;
            }
        }
    }
    cout << ans << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; i++)
    {
        solve();
    }
    return 0;
}

F题 tb的排列问题

思路

对于任意一个 b i b_{i} bi,当且仅当 p o s [ b i ] > i + w pos[b_{i}] > i + w pos[bi]>i+w时无解。

如果 b i b_{i} bi没有在数组 a a a中出现过,那么 b i b_{i} bi可以出现在 ≤ i + w \le i + w i+w的任何地方。因此我们可以使用类似于前缀和的方式, c n t [ i ] cnt[i] cnt[i]表示有多少个数字可以放到 i i i上,又因为前面已经假设放了 p r e pre pre个数字,那么当前位置就有 p r e pre pre个数字不能放了,所以答案与 c n t [ i ] − p r e cnt[i]-pre cnt[i]pre相乘。

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long i64;
const int N = 2e5 + 5;
template <int P>
struct MInt
{
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(i64 x) : x{norm(x % getMod())} {}

    static int Mod;
    constexpr static int getMod()
    {
        if (P > 0)
        {
            return P;
        }
        else
        {
            return Mod;
        }
    }
    constexpr static void setMod(int Mod_)
    {
        Mod = Mod_;
    }
    constexpr int norm(int x) const
    {
        if (x < 0)
        {
            x += getMod();
        }
        if (x >= getMod())
        {
            x -= getMod();
        }
        return x;
    }
    constexpr int val() const
    {
        return x;
    }
    explicit constexpr operator int() const
    {
        return x;
    }
    constexpr MInt operator-() const
    {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MInt inv() const
    {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MInt &operator*=(MInt rhs) &
    {
        x = 1LL * x * rhs.x % getMod();
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) &
    {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) &
    {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) &
    {
        return *this *= rhs.inv();
    }
    friend constexpr MInt operator*(MInt lhs, MInt rhs)
    {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs)
    {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs)
    {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs)
    {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a)
    {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a)
    {
        return os << a.val();
    }
    friend constexpr bool operator==(MInt lhs, MInt rhs)
    {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MInt lhs, MInt rhs)
    {
        return lhs.val() != rhs.val();
    }
};

template <>
int MInt<0>::Mod = 998244353;

template <int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();

constexpr int P = 998244353;
using Z = MInt<P>;
int n, w;
int a[N], b[N];
void solve()
{
    cin >> n >> w;
    vector<int> pos(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if (a[i] != -1)
        {
            pos[a[i]] = i;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> b[i];
    }
    vector<int> cnt(n + 1);
    for (int i = n; i >= 1; i--)
    {
        int high = min(n, i + w);
        if (pos[b[i]] && pos[b[i]] > high)
        {
            cout << 0 << endl;
            return;
        }
        if (!pos[b[i]])
            cnt[high]++;
    }

    Z ans = 1;
    int pre = 0;
    for (int i = n; i >= 1; i--)
    {
        cnt[i - 1] += cnt[i];
        if (a[i] != -1)
            continue;
        ans *= (cnt[i] - pre);
        pre++;
    }
    cout << ans << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    cin >> T;
    for (int i = 1; i <= T; i++)
    {
        solve();
    }
    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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值