本篇博客还在持续修改润色中~
敬请期待~
贪心
题单 🧑🏻💻
题目来源 | 题目名称 | 题目链接 | 备注 | 难度 |
---|---|---|---|---|
Codeforces - 489B | BerSU Ball | 链接🔗 | 贪心 | 🟢 |
Codeforces - 285C | Building Permutation | 链接🔗 | 贪心 | 🟢 |
Codeforces - 1355B | Young Explorers | 链接🔗 | 贪心 | 🟢 |
Codeforces - 1537C | Challenging Cliffs | 链接🔗 | 贪心 + 构造 | 🟡 |
题解 🚀
Building Permutation
题目信息 📚
【题目名称】Codeforces 285C - Building Permutation
【题目描述】
Permutation $p$ is an ordered set of integers $p_1, p_2, \ldots, p_n$, consisting of $n$ distinct positive integers, each of them does not exceed $n$. We’ll denote the $i$-th element of permutation $p$ as $p_i$. We’ll call number $n$ the size or the length of permutation $p_1, p_2, \ldots, p_n$.
You have a sequence of integers $a_1, a_2, \ldots, a_n$. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves needed to build a permutation from this sequence.
【输入】
The first line contains the integer $n$ $(1 \leq n \leq 3 \cdot 10^5)$ — the size of the sought permutation. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ $(-10^9 \leq a_i \leq 10^9)$.
【输出】
Print a single number — the minimum number of moves.
Please, do not use the %lld
specifier to read or write 64-bit integers in C++. It is preferred to use the cin
, cout
streams or the %I64d
specifier.
【输入样例1】
2
3 0
【输出样例1】
2
【输入样例2】
3
-1 -1 2
【输出样例2】
6
【原题链接】
https://codeforces.com/problemset/problem/285/C
题目解析 🍉
【题目分析】
很容易猜到の贪心策略:令排序后的 a[i]
→ i
具体数学证明其实有点困难,多做题提高直觉吧~
来自GPT的证明:
To prove that this approach is optimal, let’s consider two elements $a_i$ and $a_j$ such that $a_i < a_j$ and their corresponding targets in the permutation are $t_i$ and $t_j$ respectively. For the total cost to be minimized, it must be the case that $t_i \leq t_j$.
If $t_i > t_j$, you can swap $t_i$ and $t_j$, which will decrease the total cost because the sum of $|a_i - t_j| + |a_j - t_i|$ is less than $|a_i - t_i| + |a_j - t_j|$. This swap contradicts the assumption that we have an optimal solution.
Thus, for every pair $(a_i, a_j)$ with $a_i < a_j$, their targets must be in non-decreasing order. This ordering is exactly what is achieved by sorting the sequence $a$.
【C++ 代码】
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 3e5 + 10;
int n, a[N];
int main() {
ios::sync_with_stdio(false); //cin读入优化
cin.tie(0);
// 读入数据并且排序
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
// 贪心策略:令排序后的a[i] -> i
LL cnt = 0;
for (int i = 1; i <= n; i++) {
cnt += abs(a[i] - i);
}
cout << cnt << endl;
return 0;
}
Young Explorers
题目信息 📚
【题目名称】Codeforces 1355B - Young Explorers
【题目描述】
Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp, and decided to split into groups to explore as many interesting locations as possible. Russell was trying to form groups, but ran into some difficulties…
Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became a senior explorer not long ago. Each of the young explorers has a positive integer parameter $e_i$ — his inexperience. Russell decided that an explorer with inexperience $e_i$ can only join the group of $e_i$ or more people.
Now Russell needs to figure out how many groups he can organize. It’s not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.
【输入】
The first line contains the number of independent test cases $T$ $(1 \leq T \leq 2 \times 10^5)$. The next $2T$ lines contain a description of the test cases.
The first line of the description of each test case contains the number of young explorers $N$ $(1 \leq N \leq 2 \times 10^5)$.
The second line contains $N$ integers $e_1, e_2, \ldots, e_N$ $(1 \leq e_i \leq N)$, where $e_i$ is the inexperience of the $i$-th explorer.
It’s guaranteed that the sum of all $N$ doesn’t exceed $3 \times 10^5$.
【输出】
Print $T$ numbers, each number on a separate line.
In the $i$-th line, print the maximum number of groups Russell can form in the $i$-th test case.
【输入样例】
2
3
1 1 1
5
2 3 1 2 2
【输出样例】
3
2
【提示】
In the first example, we can organize three groups. There will be only one explorer in each group. It’s correct because the inexperience of each explorer equals 1, so it’s not less than the size of his group.
In the second example, we can organize two groups. Explorers with inexperience 1, 2, and 3 will form the first group, and the other two explorers with inexperience equal to 2 will form the second group.
This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to 2, and the second group using only one explorer with inexperience equal to 1. In this case, the young explorer with inexperience equal to 3 will not be included in any group.
【原题链接】
https://codeforces.com/problemset/problem/1355/B
题目解析 🍉
【题目分析】
【C++ 代码】