2024.12.27 周五

2024.12.27 周五


Q1. 1100

Alex is participating in the filming of another video of BrMeast, and BrMeast asked Alex to prepare 250 thousand tons of TNT, but Alex didn't hear him well, so he prepared n boxes and arranged them in a row waiting for trucks. The i-th box from the left weighs ai tons.

All trucks that Alex is going to use hold the same number of boxes, denoted by k. Loading happens the following way:

  • The first k boxes goes to the first truck,
  • The second k boxes goes to the second truck,
  • The last k boxes goes to the nk-th truck.

Upon loading is completed, each truck must have exactly k boxes. In other words, if at some point it is not possible to load exactly k boxes into the truck, then the loading option with that k is not possible.

Alex hates justice, so he wants the maximum absolute difference between the total weights of two trucks to be as great as possible. If there is only one truck, this value is 0.

Alex has quite a lot of connections, so for every 1kn, he can find a company such that each of its trucks can hold exactly k boxes. Print the maximum absolute difference between the total weights of any two trucks.


Q2. 1100

You are given an array a of length n, consisting of positive integers, and an array x of length q, also consisting of positive integers.

There are q modification. On the i-th modification (1iq), for each j (1jn), such that aj is divisible by 2xi, you add 2xi1 to aj. Note that xi (1xi30) is a positive integer not exceeding 30.

After all modification queries, you need to output the final array.


------------------------独自思考分割线------------------------

  • 这两道题都和数学有些关系,暴力会超时,通过数学与前缀优化解决。


A1.

  1. 读懂题就是:对于每个可被n整除的数作为一个长度k,对每个长度将n分为n/k连续的块,问块和的最大差值。
  2. 因数最多为 logn ,前缀和优化,时间复杂度 O(nlogn)
  3. 初始化最大值上界开小了(区间和),wa一发。

A2.

  1. 若要被 2x 整除,其后 x 位需要全为 0 ,加上 1<<x1 后,再次遇到 x 就不会有贡献,即需要发现的点是q数组中的数只有第一次出现才会有贡献,因此最多 30 个,两层暴力即可。

------------------------代码分割线------------------------

A1.

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    vector<int> pre(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> pre[i], pre[i] += pre[i - 1];
    auto ask = [&](int l, int r)
    {
        return pre[r] - pre[l - 1];
    };
    vector<int> op;
    for (int i = 1; i <= n; i++)
        if (n % i == 0)
            op.push_back(i);
    int res = 0;
    auto get = [&](int d)
    {
        int l = 1, r = d;
        int maxw = 0, minw = 1e18;
        while (r <= n)
        {
            maxw = max(maxw, ask(l, r));
            minw = min(minw, ask(l, r));
            l+=d,r+=d;
        }
        return maxw - minw;
    };
    for (auto d : op)
        res = max(res, get(d));
    cout << res << endl;
}

A2.

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int &x : a)
        cin >> x;
    int has[31] = {}; // map自动排序 无法保证原始序列
    vector<int> b;
    while (q--)
    {
        int x;
        cin >> x;
        if (!has[x])
            b.push_back(x);
        has[x] = 1;
    }

    for (auto &x : b)
        for (auto &a : a)
        {
            int v = 1ll << x;
            // bug(v);
            a += a % v == 0 ? (v >> 1) : 0;
        }
    for (auto v : a)
        cout << v << ' ';
    cout << endl;
}

posted @   Jkke  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示