提高算法思维能力测试(B)改错

· · 个人记录

A.符文大陆

题目大意

有一个 n \times n(n\le 10^5) 的矩阵,第 i 行 j 列的格子的初始符文能量值为 a_{i,j} = (A \times i^2 + B \times j^2 + C) \mod 10007, A, B, C 为给定的定值。
一共有 q(q\le10^5) 次操作,分为以下类型:

  1. R x k:行能量灌注。给当前矩阵第 x 行的所有符文能量加上 k。
  2. C y k:列能量灌注。给当前矩阵第 y 列的所有符文能量加上 k。
  3. A k:全局充能。给当前矩阵所有符文能量加上 k。
  4. T:空间转置。将当前矩阵进行转置操作(即第 i 行变成第 i 列,第 j 列变成第 j 行)。
  5. S u v:维度交换。交换当前矩阵的第 u 行与第 v 行。
  6. Q x y:能量查询。询问当前矩阵第 x 行第 y 列的符文能量值。

    题目分析

    如果直接开二维数组模拟的话,时间复杂度和空间复杂度都会炸,考虑优化。

int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);

cin >> n >> q >> a >> b >> c;

for (int i = 1; i <= n; i++)
    swr[i] = i, swc[i] = i;

while (q--) {
    char op;
    int x, y;
    cin >> op;

    if (op == 'R') {
        cin >> x >> y;
        tp ? (cs[swc[x]] += y) : (rs[swr[x]] += y);
    }
    else if (op == 'C') {
        cin >> x >> y;
        tp ? (rs[swr[x]] += y) : (cs[swc[x]] += y);
    }
    else if (op == 'A') {
        cin >> x;
        all += x;
    }
    else if (op == 'T') {
        tp = !tp;
    }
    else if (op == 'S') {
        cin >> x >> y;
        tp ? swap(swc[x], swc[y]) : swap(swr[x], swr[y]);
    }
    else if (op == 'Q') {
        cin >> x >> y;
        int nx = tp ? swr[y] : swr[x];
        int ny = tp ? swc[x] : swc[y];
        int ao = (1LL * a * nx * nx) % MOD;
        int bo = (1LL * b * ny * ny) % MOD;
        int initial = (ao + bo + c) % MOD;

        cout << initial + all + rs[nx] + cs[ny] << "\n";
    }
}
return 0;

}

### B.云计算中心
#### 题目大意
有 $C(c\le 2500)$ 台计算节点,每台节点只能接收频率在其指定区间 $[minF_i, maxF_i]$ 内的信号。有 $L(L\le 2500)$ 种信号发生器,每种发生器输出固定频率 $F_i$,最多可覆盖 $cover[i]$ 台节点。每台节点最多接入一种发生器。求通过合理分配,最多能让多少台计算节点同时稳定工作。
#### 题目分析
我们对这道题使用贪心思想。分为以下步骤:
1. **排序**:将所有计算节点按照最小频率需求 $minF$ 从大到小排序。
2. **贪心分配**:按排序后的顺序依次处理每台计算节点,为其选择频率最大且同时满足以下两个条件的信号发生器:
   - 频率F在该节点的允许范围$[minF_i, maxF_i]$内
   - 该信号发生器还有剩余覆盖容量
3. **统计结果**:所有成功分配到信号发生器的计算节点数量,即为最多可同时稳定工作的节点数。
时间复杂度 $O(c\times l)$即$O(c^2)$,数据范围允许。
#### 代码实现
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5;

struct node {
    int l, r;
    bool operator<(const node& o) const {
        return l > o.l;
    }
} nd[N];

struct gen {
    int f, c;
} g[N];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int c, l;
    cin >> c >> l;

    for (int i = 0; i < c; i++)
        cin >> nd[i].l >> nd[i].r;

    for (int i = 0; i < l; i++)
        cin >> g[i].f >> g[i].c;

    sort(nd, nd + c);

    int ans = 0;
    for (int i = 0; i < c; i++) {
        int best = -1;
        for (int j = 0; j < l; j++) {
            if (g[j].c > 0 && g[j].f >= nd[i].l && g[j].f <= nd[i].r) {
                if (best == -1 || g[j].f > g[best].f) {
                    best = j;
                }
            }
        }
        if (best != -1) {
            g[best].c--;
            ans++;
        }
    }

    cout << ans;
    return 0;
}

C.序列异或和

题目大意

有 T 组询问,每组给定正整数 n 和 l,构造一个长度为 l 的正整数序列(每个元素在 [1,n] 范围内),求所有两两元素异或值的总和的最大值,结果对 10^9+7 取模。

题目分析

异或运算的每一位相互独立,可分别计算每一位对总和的最大贡献后相加。所以:
对于任意一位,要使其贡献最大,需让序列中该位为0和1的数量乘积最大。
当长度为 l 时,最优分配是 \lfloor \frac{l}2 \rfloor 个0和\lceil l/2 \rceil 个1,乘积为\lfloor\frac{l}{2}\rfloor \times (l - \lfloor\frac{l}{2}\rfloor),每对贡献 2^k。
一共能用 \log_2(n)为,当 n=1 时,答案为0。

代码实现

#include <bits/stdc++.h>
const int MOD = 1000000007;
using namespace std;
int t;
int main () {
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin >> t;
    while (t--) {
        long long n, l;
        cin >> n >> l;
        if (n == 1) {
            cout << "0\n";
            continue;
        }
        n = 63 - __builtin_clzll(n);//long long类型用log2或者__lg可能会溢出,__builtin_clzll可以求出前导0个数,再用总位数减掉
        long long ans = (((l / 2) * (l - (l / 2))) % MOD) * ((((1ll << (n + 1)) - 1)) % MOD);
        cout << ans % MOD << '\n';
    }
    return 0;
}

D.彩带

题目大意

给定 n 个彩珠,分为 k 种,每个彩珠有一个整数坐标(坐标可重复,可有空隙)。 要求找到一段连续的区间,使得该区间内包含所有 k 种颜色的彩珠。 求满足条件的区间的最小长度(区间长度 = 右端点坐标 - 左端点坐标)。

题目分析

本题使用双指针 + 滑动窗口,步骤如下:

  1. 输入彩珠坐标与种类
  2. 按坐标排序所有彩珠
  3. 双指针维护窗口,批量移动边界
  4. 记录满足条件的最小区间长度

效率 O(n \log n)

代码实现

#include<bits/stdc++.h>
using namespace std;
const int N = 1000005;
int l, r, n, m, k, cont, b[105], cnt, ans;
struct node {
    int val, id;
} a[N];
bool cmp(node a, node b) {
    return a.val < b.val;
}
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d", &k);
        for (int j = 1; j <= k; j++) {
            scanf("%d", &a[++cont].val);
            a[cont].id = i;
        }
    }
    ans = 1e9;
    sort(a + 1, a + cont + 1, cmp);
    l = r = 1;
    b[a[1].id] = 1;
    cnt = 1;
    for (int i = 2; i <= n; i++)
        if (a[i].val == a[i - 1].val) {
            r++;
            b[a[i].id]++;
            if (b[a[i].id] == 1)cnt++;
        } else break;
    while (l <= n && r <= n) {
        if (cnt == m) {
            ans = min(ans, a[r].val - a[l].val);
            b[a[l].id]--;
            if (b[a[l].id] == 0)cnt--;
            l++;
            while (a[l].val == a[l - 1].val) {
                b[a[l].id]--;
                if (b[a[l].id] == 0)cnt--;
                l++;
                if (l > n)
                    break;
            }
        } else {
            r++;
            b[a[r].id]++;
            if (b[a[r].id] == 1)cnt++;
            while (a[r + 1].val == a[r].val) {
                r++;
                b[a[r].id]++;
                if (b[a[r].id] == 1)cnt++;
            }
        }
    }
    cout << ans;
}

E.青蛙

题目大意

有 n 块石头按距离源头从小到大排列为 p_1 < p_2 < \dots < p_n。 青蛙每次从当前石头跳到 距离它第 k 近 的石头;若有多个石头距离相同,选择 离源头更近 的那个。 对每一块石头,计算从它出发连续跳跃 m 次后最终停留的石头位置。

题目分析

本体使用双指针预处理单次跳跃 + 倍增法处理多次跳跃

二、双指针求单次跳跃目标

石头坐标严格递增,离 i 最近的 k 个石头必构成包含 i 的长度为 k+1 的连续窗口,第k近的石头只能是窗口左/右端点。

三、倍增法处理 m 次跳跃

利用跳跃传递性和二进制分解,将m次跳跃拆分为若干2的幂次跳跃的组合,总时间复杂度 O(n \log m)。

代码实现

#include <bits/stdc++.h>
#define ll long long //需要开long long
const int N = 1000005;
using namespace std;
ll a[N], m;
ll p[N], tmp[N], ans[N], n, k;
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> k >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        ans[i] = i;
    }
    int l = 1;
    for (int i = 1; i <= n; i++) {
        while (l + k < i) l++;
        while (l + 1 + k <= n) {
            int d1 = max(a[i] - a[l], a[l + k] - a[i]);
            int d2 = max(a[i] - a[l + 1], a[l + 1 + k] - a[i]);
            if (d2 < d1) l++;
            else break;
        }
        int r = l + k;
        if (a[i] - a[l] > a[r] - a[i]) p[i] = l;
        else if (a[i] - a[l] < a[r] - a[i]) p[i] = r;
        else p[i] = l;
    }
    for (int j = 0; j <= 63; j++) {
        if (m & (1LL << j)) {
            for (int i = 1; i <= n; i++) {
                ans[i] = p[ans[i]];
            }
        }
        if (j == 63) break;
        for (int i = 1; i <= n; i++) {
            tmp[i] = p[p[i]];
        }
        swap(p, tmp);
    }
    for (int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    return 0;
}

F.羊的才艺

题目大意

给定 n 头羊,第 i 头羊的重量为 w_i,才艺值为 t_i。 要求选出若干头羊,满足总重量 \ge W,最大化 比值 \displaystyle\frac{\sum t_i}{\sum w_i}。

题目分析

本题使用二分答案 + 01背包优化完成

一、为什么使用二分答案
  1. 要最大化 \displaystyle\frac{\sum t_i}{\sum w_i},等价于找最大的 x,使得存在子集满足 \sum (t_i - x \cdot w_i) \ge 0 且 \sum w_i \ge W。
  2. 若 x 可行,则所有 x' < x 都可行,因此可以二分查找最大的可行 x。
    01背包实现
  3. 验证 x 是否可行,等价于求总重量 \ge W 时,\sum (t_i - x \cdot w_i) 的最大值。若最大值 \ge 0,则 x 可行。
  4. 总重量超过 W 时统一按 W 处理(只需满足 \ge W),将 \text{dp} 状态压缩到 W 以内,时间复杂度 O(nW)。

    代码实现

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int inf = 1e7;
    int n, W;
    int w[300], t[300];
    ll dp[10000];
    bool check(int z) {
    memset(dp, 128, sizeof(dp));
    dp[0] = 0;
    long long tmp = dp[W];
    for (int i = 1; i <= n; i++) {
        for (int j = W; j >= 0; j--)if (dp[j] != tmp) {
                int tmp2 = j + w[i];
                tmp2 = min(tmp2, W);
                dp[tmp2] = max(dp[tmp2], dp[j] + t[i] - ll(w[i]) * z);
            }
    }
    return dp[W] >= 0;
    }
    int solve() {
    int l = 0, r = inf;
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (check(mid))l = mid + 1;
        else r = mid - 1;
    }
    return l - 1;
    }
    int main() {
    cin >> n >> W;
    for (int i = 1; i <= n; i++) {
        cin >> w[i] >> t[i];
        t[i] *= 1000;
    }
    cout << solve();
    return 0;
    }

    G.股票交易

    题目大意

    已知 S 只股票未来 D 天的价格,初始资金为 M 元。 每天可任意买卖任意数量的股票(交易单位为整数),求 D 天后能获得的最大资金。

    题目分析

    本题使用背包动态规划思想,设 f(n) 为第 n 天的最大收益。设 a(i,j) 为第 j 只股票在第 i 天的价格,则:

    f(n) = \max\left( f(n),\ f\left(k - a(j)(i-1)\right) + \left(a(j)(i) - a(j)(i-1)\right) \right)

    代码实现

    #include<bits/stdc++.h>
    using namespace std;
    int a[65][25], dp[500005];
    int s, d, m;
    int main() {
    cin >> s >> d >> m;
    for (int i = 1; i <= s; i++) {
        for (int j = 1; j <= d; j++) {
            cin >> a[i][j];
        }
    }
    for (int i = 2; i <= d; i++) {
        memset(dp, 0, sizeof dp);
        int mx = 0;
        for (int j = 1; j <= s; j++) {
            for (int k = a[j][i - 1]; k <= m; k++) {
                dp[k] = fmax(dp[k], dp[k - a[j][i - 1]] + a[j][i] - a[j][i - 1]);
                mx = fmax(dp[k], mx);
            }
        }
        m += mx;
    }
    cout << m;
    return 0;
    }

    H.括号序列