牛客小白月赛87 A-G 题解 | JorbanS

文章目录

A - 小苯的石子游戏

cpp 复制代码
string solve() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];
    int A = 0, B = 0, mode = 0;
    for (int i = n - 1; i >= 0; i --, mode ^= 1)
        if (mode & 1) B += a[i];
        else A += a[i];
    return A > B ? "Alice" : "Bob";
}

B - 小苯的排序疑惑

cpp 复制代码
string solve() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i], b[i] = a[i];
    sort(a, a + n);
    return a[0] == b[0] || a[n - 1] == b[n - 1] ? yes : no;
}

C - 小苯的IDE括号问题(easy)

双指针

cpp 复制代码
void solve() {
    cin >> n >> m >> s;
    int l = s.find('I') - 1, r = l + 2;
    while (m --) {
        string op; cin >> op;
        if (op[0] == 'b') {
            if (l != -1) {
                if (r != n && s[l] == '(' && s[r] == ')') r ++;
                l --;
            }
        } else {
            if (r != n) r ++;
        }
    }
    for (int i = 0; i <= l; i ++) cout << s[i];
    cout << 'I';
    for (int i = r; i < n; i ++) cout << s[i];
    cout << endl;
}

D - 小苯的IDE括号问题(hard)

用两个栈模拟

cpp 复制代码
void solve() {
    cin >> n >> m >> s;
    int idx = s.find('I');
    stack<char> l, r, t;
    for (int i = 0; i < idx; i ++) l.push(s[i]);
    for (int i = n - 1; i > idx; i --) r.push(s[i]);
    while (m --) {
        string op; cin >> op;
        if (op[0] == 'b') {
            if (!l.empty()) {
                if (!r.empty() && l.top() == '(' && r.top() == ')') r.pop();
                l.pop();
            }
        } else if (op[0] == 'd') {
            if (!r.empty()) r.pop();
        } else if (op[0] == '<') {
            if (!l.empty()) {
                r.push(l.top());
                l.pop();
            }
        } else {
            if (!r.empty()) {
                l.push(r.top());
                r.pop();
            }
        }
    }
    while (!l.empty()) t.push(l.top()), l.pop();
    while (!t.empty()) cout << t.top(), t.pop();
    cout << 'I';
    while (!r.empty()) cout << r.top(), r.pop();
    cout << endl;
}

E - 小苯的数组构造

贪心,让 a i + b i = m a x j = 1 i − 1 a j ai+bi=max_{j=1}^{i-1}a_j ai+bi=maxj=1i−1aj

cpp 复制代码
void solve() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];
    int Max = -1e9;
    for (int i = 0; i < n; i ++) {
        b[i] = max(0, Max - a[i]);
        Max = max(Max, a[i]);
    }
    for (int i = 0; i < n; i ++) cout << b[i] << ' ';
    cout << endl;
}

F - 小苯的数组切分

由于一个数如果给 ∣ | ∣ 操作只会多不会少,但是给 & \& & 操作只会少不会多,因此贪心地让 & \& & 操作只包含 a n an an,接下来遍历一下前两个操作的分界点即可

cpp 复制代码
ll solve() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i], b[i] = a[i] ^ b[i - 1];
    ll res = 0, cnt = a[n - 1];
    for (int i = n - 2; i; i --) {
        res = max(res, cnt + b[i]);
        cnt |= a[i];
    }
    return res + a[n];
}

G - 小苯的逆序对

树状数组维护逆序对, f i = n u m i ∣ a r r k − f 2 i − f 3 i − ... f ⌊ n i ⌋ i fi=num_{i|arrk}-f2i-f3i-...f\\left \\lfloor \\frac n i \\right \\rfloor i fi=numi∣arrk−f2i−f3i−...f⌊in⌋i

其中逆序操作保证 f 2 i , f 3 i , ... ... f ⌊ n i ⌋ i f2i,~f3i,~......~f\\left \\lfloor \\frac n i \\right \\rfloor i f2i, f3i, ...... f⌊in⌋i 是已经求得的

cpp 复制代码
ll f[N];

struct BIT {
    int tr[N];

    inline int lowbit(int x) { return x & -x; }

    void modify(int x, int k) {
        while (x) tr[x] += k, x -= lowbit(x);
    }

    int query(int x) {
        int res = 0;
        while (x <= n) res += tr[x], x += lowbit(x);
        return res;
    }
} bit;

ll solve() {
    cin >> n;
    for (int i = 1; i <= n; i ++) {
        int x; cin >> x;
        a[x] = i;
    }
    for (int i = n; i; i --) {
        for (int j = i; j <= n; j += i) {
            f[i] += bit.query(a[j]);
            bit.modify(a[j], 1);
        }
        for (int j = i; j <= n; j += i) {
            bit.modify(a[j], -1);
            if (j != i) f[i] -= f[j];
        }
    }
    return f[1];
}
相关推荐
JieE21241 分钟前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack208 小时前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树10 小时前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2121 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2121 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术1 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦1 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
用户497863050731 天前
(一)小红的数组操作
算法·编程语言
怕浪猫2 天前
Electron 系列文章封面图
算法·架构·前端框架