2025-10-04 16:00:00 ~ 2025-11-05 16:07:00
| 题目编号 | T1 | T2 | T3 | T4 |
|---|---|---|---|---|
| 题目难度 | 黄 | 绿 | 绿 | 蓝 |
| 考察知识点 | 邻项交换类型贪心 | 果子合并类型堆贪心 | 排序不等式优化 | 果子合并类型堆贪心 |
本题考察一个知识点,交换相邻的两个,对其他答案并没有影响,另外这里的传递性比较明显,可以直接使用邻项交换贪心。
假设现在的话
假设
上述公式可以直接化简为:
转换之后可以得到
按照
参考代码实现:
#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;
}
直接进行石子合并的模拟,每次取出最小的两堆进行合并,但是此时会超时。
所以考虑使用类似于贪心的思路来进行实现,我们考虑到一个实际的问题,每次合并的过程之中,最小值来源于两个最小的值。
直接使用一个队列来存储原来的数据,队列
另外一个队列存储已经合并之后的数据,队列
答案一定来自于两个队列的队首元素,而且新组成的两个堆一定比原来的堆大,加入到存储已经合并的队列
时间复杂度是
参考代码实现:
#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;
}
题目的突破口来自于上述给出的公式,我们假设已经安排了一个比较合适的全排列,那么首要做到的一点是,计算答案
两个的公式是
处理的过程之中,我们需要考虑的是
考虑
比如
此时
对应
#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;
}
哈夫曼编码的本质是优化编码
多叉哈夫曼树不能直接进行贪心合并,比如四个节点
但是很明显的是下面的更加优秀,
所以标准版本的多叉哈夫曼树会引入一部分的
参考代码实现:
#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;
}