10.4-409-国庆集训第一天-下午

2025-10-04 16:00:00 ~ 2025-11-05 16:07:00

10.4测试卷解析

题目编号 T1 T2 T3 T4
题目难度 黄 绿 绿 蓝
考察知识点 邻项交换类型贪心 果子合并类型堆贪心 排序不等式优化 果子合并类型堆贪心

T1

P1842 [USACO05NOV] 奶牛玩杂技

本题考察一个知识点,交换相邻的两个,对其他答案并没有影响,另外这里的传递性比较明显,可以直接使用邻项交换贪心。

假设现在的话 i 和 j 相邻,之前的重量总和位 sum 。

假设 i 在 j 的上面,此时需要满足

max(sum - s_i, sum + w_i -s_j) < max(sum - s_j, sum + w_j-s_i)

上述公式可以直接化简为:

sum + w_i -s_j < sum + w_j-s_i

转换之后可以得到

w_i + s_i < w_j +s_j

按照 s+w 的方式上升即可。

参考代码实现:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 7;
const int INF = 1e9;
int n;
int ans = -INF;
int tot = 0;
struct Node
{
    int w;
    int s;
} a[maxn];
bool cmp(Node A, Node B)
{
    return A.s + A.w < B.s + B.w;
}
signed main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].w >> a[i].s;
    }
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    {
        ans = max(ans, tot - a[i].s);
        tot += a[i].w;
    }
    cout << ans << endl;
    return 0;
}

T2

P6033 [NOIP 2004 提高组] 合并果子 加强版

直接进行石子合并的模拟,每次取出最小的两堆进行合并,但是此时会超时。

所以考虑使用类似于贪心的思路来进行实现,我们考虑到一个实际的问题,每次合并的过程之中,最小值来源于两个最小的值。

直接使用一个队列来存储原来的数据,队列 Q_1 。

另外一个队列存储已经合并之后的数据,队列 Q_2 。

答案一定来自于两个队列的队首元素,而且新组成的两个堆一定比原来的堆大,加入到存储已经合并的队列 Q2 之中即可。

时间复杂度是 O(n) 级别的。

参考代码实现:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e5 + 7;
queue<int> q1;
queue<int> q2;
int n, tong[maxn];
void read(int &x)
{
    int f = 1;
    x = 0;
    char s = getchar();
    while (s < '0' || s > '9')
    {
        if (s == '-')
            f = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9')
    {
        x = x * 10 + s - '0';
        s = getchar();
    }
    x *= f;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    read(n);
    for (int i = 1; i <= n; i++)
    {
        int x;
        read(x);
        tong[x]++;
    }
    for (int i = 1; i <= 100000; i++) // 桶排序
    {
        while (tong[i])
        {
            tong[i]--;
            q1.push(i);
        }
    }
    int ans = 0;
    for (int i = 1; i <= n - 1; i++)
    {
        int x, y;
        if ((!q1.empty() && q1.front() < q2.front()) || q2.empty())
        {
            x = q1.front();
            q1.pop();
        }
        else
        {
            x = q2.front();
            q2.pop();
        }
        if ((!q1.empty() && q1.front() < q2.front()) || q2.empty())
        {
            y = q1.front();
            q1.pop();
        }
        else
        {
            y = q2.front();
            q2.pop();
        }
        ans += x + y;
        q2.push(x + y);
    }
    cout << ans;
    return 0;
}

T3

P1966 [NOIP 2013 提高组] 火柴排队

题目的突破口来自于上述给出的公式,我们假设已经安排了一个比较合适的全排列,那么首要做到的一点是,计算答案

两个的公式是

(a_i - b_i) ^2 = (a_i ^2 + b_i^2 - 2 \times a_i \times b_i)

处理的过程之中,我们需要考虑的是 a_i ^ 2 + b_i ^2 这个数值的总和是不变的,最终只有 a_i 和 b_i 的乘积会通过修改顺序发生变化,按照排序不等式,最大的这个乘积和应当是顺序和,所以只需要处理其中一个的顺序即可。

考虑 a_i 的最大值,和 b_i 的最大值一一对应,假设 a_i 排好序,此时 a_i 的相对位置的下标是已经得到的,那么只需要修改 b_i 到达对应的下标即可

比如

a = 3, 1, 2,4 \\ b = 2, 1, 3, 4

此时 a 的第三大的数字需要排在第一位,对应的也就是数组 b 之中的数字也是放在第一位,

对应 b 数组就是转换之后的数组变成 1, 2, 3, 4 ,也就是逆序对。

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 7;
const int mod = 1e8 - 3;
int n, c[maxn], t[maxn], ans; // c[i]:a[i]代表的最终位置
struct Node
{
    int id, v;
} a[maxn], b[maxn];
bool cmp(Node a, Node b)
{
    return a.v < b.v;
}
int lowbit(int x)
{
    return x & -x;
}
void modify(int x, int v)
{
    while (x <= n)
    {
        t[x] += v;
        x += lowbit(x);
    }
}
int query(int x)
{
    int sum = 0;
    while (x > 0)
    {
        sum += t[x];
        x -= lowbit(x);
    }
    return sum;
}
signed main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].v, a[i].id = i;
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> b[i].v, b[i].id = i;
    }
    sort(a + 1, a + 1 + n, cmp);
    sort(b + 1, b + 1 + n, cmp);
    for (int i = 1; i <= n; i++)
    {
        c[b[i].id] = a[i].id;
    }
    for (int i = n; i; i--)
    {
        ans += query(c[i]);
        modify(c[i], 1);
    }
    cout << ans % mod << "\n";
    return 0;
}

T4

哈夫曼编码的本质是优化编码

多叉哈夫曼树不能直接进行贪心合并,比如四个节点

但是很明显的是下面的更加优秀,

所以标准版本的多叉哈夫曼树会引入一部分的 0 代价的点加入,保证最终得到的一定是上层最优

参考代码实现:

#include <bits/stdc++.h>
#define int long long
using namespace std;

struct Node
{
    int w, h;
    // 权值和高度
    bool operator<(const Node &rhs) const
    {
        if (w == rhs.w)
        {
            return h > rhs.h;
        }
        return w > rhs.w;
    }
};
int ans;
priority_queue<Node, vector<Node>> Q;
signed main()
{
    int n, k;
    ans = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        int w;
        cin >> w;
        Q.push({w, 1});
    }
    while ((Q.size() - 1) % (k - 1) != 0)
    {
        Q.push({0, 1});
    }
    while (Q.size() >= k)
    {
        int h = -1;
        int w = 0;
        for (int i = 1; i <= k; ++i)
        {
            Node t = Q.top();
            Q.pop();
            h = max(h, t.h);
            w += t.w;
        }
        ans += w;
        Q.push({w, h + 1});
    }
    cout << ans << endl;
    cout << Q.top().h - 1 << endl;
    return 0;
}