题目难度感觉是顺序的,就顺着讲了(虽然乱序的情况我也顺着讲)
A
题意简单明了,升序排序输出即可
#include <bits/stdc++.h>
using namespace std;int main() {int n, m;cin >> n >> m;vector<int> a(m);for (int i=0; i<m; i ++) {cin >> a[i];}sort(a.begin(), a.end());for (auto &x: a) {cout << x << ' ';}return 0;
}
B
数学题
首先我们要想清楚总时间最短可以转化为什么问题,显然就是尽量让车和两个人都没停下来,也就是最后一定是走路的人和车同时到终点,那么后面走路的人一定是先上车的。
那么在距离为 d 的位置放下 1 号乘客,转头去接 2 号,这样做是更优的。
我们还要思考如果我频繁地来回接送会更优吗?显然不会的,因为两个人只有编号的区别,那么调头换人上车的过程中就会出现只有人的速度做贡献的情况,所以只进行一次调头换人上车是最优的。
现在,我们把过程稍微抽象一下
- 第一阶段:1 号从左端点以 a 的速度向右走,车和 2 号从左端点以 b 的速度向右走
- 第二阶段:1 号继续向右走,2 号在 d 处被放下,向右端点走,车从 d 处向左走
- 第三阶段:1 号遇到车上车,以 b 的速度向右走,2 号尚未走到右端点
- 第四阶段:1 号坐车和走路的 2 号同时抵达终点
我们可以注意到,这个过程是完全对称的,这意味着 2 号下车的位置和 1 号上扯的位置也是对称的
那么 1 号就是在 s - d 处上的车,这可以简化我们的运算
以时间列等式
总时间就是 \(\frac{s-d}{a} + \frac{d}{b} \rightarrow \frac{4d-s}{b}\)
#include <bits/stdc++.h>
using namespace std;int main() {double s, a, b;cin >> s >> a >> b;double d = (s*b + s*a) / (3*a + b);double t = (4*d - s) / b;cout << fixed << setprecision(6) << t << endl;return 0;
}
C
这道题是考察对栈的基础运用
用栈维护尚未配对的 ( 和 [ ,然后用一个数组记录每一位的字符是否需要在其相邻位置补充配对括号。
具体实现可看代码,代码中涉及到两个 char 变量的加减运算,这与他们的 ASCII 码有关,主要用处是把多个判断化简为一个
#include <bits/stdc++.h>
using namespace std;int main() {string s;cin >> s;stack<int> st;vector<bool> v(s.size());for (int i=0; i<s.size(); i++) {char c = s[i];if (c == '(' || c == '[') st.push(i);else if (st.empty() || abs(c - s[st.top()]) > 2) v[i] = true;else if (!st.empty()) st.pop();}while (!st.empty()) {v[st.top()] = true;st.pop();}for (int i=0; i<s.size(); i++) {if (v[i]) cout << (s[i] <= ')'? "()": "[]");else cout << s[i];}return 0;
}
D
一道模拟题,考察队列的基础使用
因为当数量超过上限时,删除最早加入的,那么就符合先进先出原则,使用队列模拟,
然后用一个 vis 数组记录每个词在不在快速翻译的列表中
细节见代码
#include <bits/stdc++.h>
using namespace std;int main() {int n, m;cin >> m >> n;vector<bool> vis(1001);queue<int> q;int cnt{ 0 };for (int i = 1; i <= n; i ++) {int x;cin >> x;if (!vis[x]) {q.push(x), vis[x] = true;if (q.size() > m) vis[q.front()] = false, q.pop();cnt ++;}}cout << cnt;return 0;
}
E
题目说到最多 6 个单词,也就意味着最多 6 个数字,因此可以暴力枚举每一种排列方式,复杂度最多是 6! ,要注意如果 \(x^2\ mod\ 100 = y < 10\) ,那么实际组合时是拼接 0y 而不是 y
next_permutation 是一个库函数,可以返回一个刚好比传入数组字典序小的另一种排列方式,想详细了解可以取网上学习。这道题只需知道,在升序排序数组后,使用示范代码中的 do-while 循环可以枚举出每一种排列方式
#include <bits/stdc++.h>
using namespace std;map<string, int> num;void init() {num["one"] = 1, num["two"] = 2, num["three"] = 3, num["four"] = 4, num["five"] = 5;num["six"] = 6, num["seven"] = 7, num["eight"] = 8, num["nine"] = 9, num["ten"] = 10;num["eleven"] = 11, num["twelve"] = 12, num["thirteen"] = 13, num["fourteen"] = 14, num["fifteen"] = 15;num["sixteen"] = 16, num["seventeen"] = 17, num["eighteen"] = 18, num["nineteen"] = 19, num["twenty"] = 20;num["zero"] = 0;num["a"] = 1, num["both"] = 2, num["another"] = 1, num["first"] = 1, num["second"] = 2, num["third"] = 3;
}int main() {init();string s;vector<int> x;while (cin >> s) {if (s.back() == '.') s = s.substr(0, s.size() - 1);for (auto &ch: s) {if (ch <= 'Z') ch += 32;}if (num.count(s)) x.push_back(num[s] * num[s] % 100);}sort(x.begin(), x.end());long long ans{ 0x3f3f3f3f3f3f3f3f };do {long long res{ 0 };for (auto &i: x) {res *= 100;res += i;}ans = min(ans, res);} while (next_permutation(x.begin(), x.end()));cout << ans;return 0;
}
F
这是一道贪心题
因为每一个有泥的地方都要盖上,还要最少木板,所以我们按淤泥区间的左端点升序排序,每次找到最左边没有被盖住的淤泥,然后更新到木板覆盖的范围,因为从左到右遍历,所以已经覆盖的地方的左边一定是全部淤泥都被覆盖的。
对于有重合的区间,如果淤泥已经被覆盖,那么第一个有淤泥的点一定在这个区间左端点的右边,不会重复。
#include <bits/stdc++.h>
using namespace std;int main() {int n, L;cin >> n >> L;vector<pair<int, int>> a(n);for (int i = 0; i < n; i ++) {cin >> a[i].first >> a[i].second;}sort(a.begin(), a.end());int ans{ 0 }, l{ 0 };for(int i = 0; i < n; i ++) {l = max(l, a[i].first);ans += (a[i].second - l + L-1) / L;l += (a[i].second - l + L-1) / L * L;}cout << ans;return 0;
}
G
这也是可以贪心做的题
我们可以想到,如果走到最后才回头还钱,那么本来在中途只需要回头一步的情况,就会在最后需要额外走终点到当前为止的距离,所以只要当前的钱足够还清之前累积的债务,那么就立刻回头,想到这个代码就很好写了
#include <bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;vector<int> a(n+1);queue<int> q;for (int i = 1; i <= n; i ++) {cin >> a[i];if (a[i] < 0) q.push(i);}int ans{ n }, sum{ 0 }, lst{ 0 }, l{ n+1 };for (int i = 1; i <= n; i ++) {if (a[i] > 0) {sum += a[i];if (sum >= lst && lst) {ans += 2 * (i - l);sum -= lst;lst = 0, l = n+1;}} else {if (sum + a[i] >= 0) {sum += a[i];} else {lst -= a[i];l = min(i, l);}}}cout << ans;return 0;
}
H
这是一道 01 背包扩展来的分组背包,而且是一道板子题
思路就是从「在所有物品中选择一件」变成了「从当前组中选择一件」,于是就对每一组进行一次 0-1 背包就可以了。
对于数据的存储,我们正常存储每件物品的重量和价值后,分组只需记录对应索引即可
更加详细的讲解可以去看洛谷的「背包九讲」或 OI Wiki
#include <bits/stdc++.h>
using namespace std;int main() {int n, m;cin >> m >> n;vector<long long> a(n+1), b(n+1), dp(m+1, 0);map<int, vector<int>> c;for (int i = 1; i <= n; i++) {int k;cin >> a[i] >> b[i] >> k;c[k].push_back(i);}for (auto [k, y]: c) {for (int i = m; i >= 0; i --) {for (auto x: y) {if (i >= a[x]) dp[i] = max(dp[i], dp[i-a[x]] + b[x]);}}}cout << dp[m];return 0;
}