考试链接。
概述
| 题号 | 难度 | 主要知识点 | 其它知识点 |
|---|---|---|---|
| 橙 | BFS | 拆位、质数判断 | |
| 黄 | 棋盘 DFS | 小剪枝、路径输出 | |
| 绿 | DFS | STL 全排列 | |
| 蓝 | 棋盘 BFS | 图论 | |
| 橙 | 模拟 | 时间复杂度证明 |
个人评价:
- 第一、二题较易,是课上讲的内容。必须掌握,应该拿到满分。
- 第三题用到 DFS + 预处理全排列,用到
next_permutation函数,争取拿下。 - 第四题可能是最难的一道题,其实就是 BFS 棋盘最短路的改编版,注意细节以及审题即可。如果不会,也应该拿到部分分。
- 你是否注意到最后一道题最简单了?枚举(小优化)即可通过。
分数线:
- 分数线 :。
- 分数线 :。【越过这条线】
- 分数线 :。
质数序列
前言
本题主要考察 BFS,注意多测清空。
预估难度:橙或黄。
题意
给定四位质数 ,从 变换到 ,每一步只能变一位,且必须变成质数,求最少步数。
题解
每次转移时,对于当前队首的质数 ,我们枚举改变第几位数,以及改变成多少,并计算出新的数 。当 是质数且没有被访问过时,更新 的答案,标记被访问,然后入队。如果队首恰好等于 ,那么直接输出距离。如果一直没有访问到,最后输出 impossible。注意多测清空!!!
复杂度分析
这里定义值域上限 ,质数个数 。
显然空间复杂度 ,下面分析时间复杂度。
每个质数最多被入栈出栈一次,;每一次转移枚举 种情况,然后用 判断质数,复杂度 。所以单测 ,总复杂度 ,约为 ,可以通过。
标程
#include <bits/stdc++.h>
using namespace std;
int T, a, b;
inline bool isPrime(int x) {
if (x < 2) return 0;
for (int i = 2; i * i <= x; i++)
if (x % i == 0)
return 0;
return 1;
}
const int N = 1e4 + 5;
int dis[N], p[5];
inline int Bfs(void) {
memset(dis, 0x3f, sizeof dis);
queue<int> q;
q.push(a);
dis[a] = 0;
while (!q.empty()) {
int x = q.front(); q.pop();
if (x == b) return dis[x];
p[0] = x % 10, p[1] = x / 10 % 10, p[2] = x / 100 % 10, p[3] = x / 1000;
int Pow = 1;
for (int k = 0; k <= 3; k++) {
for (int i = 0; i <= 9; i++) {
if (k == 3 && i == 0) continue;
int nx = x + (i - p[k]) * Pow;
if (!isPrime(nx)) continue;
if (dis[nx] <= dis[x] + 1) continue;
dis[nx] = dis[x] + 1;
q.push(nx);
}
Pow *= 10;
}
}
return -1;
}
int main(void) {
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> T;
while (T--) {
cin >> a >> b;
int res = Bfs();
if (res == -1)
cout << "impossible" << endl;
else
cout << res << endl;
}
return 0;
}马的路径
前言
本题考查棋盘 DFS。不需要任何剪枝。
预估难度:黄。
题意
给定一个棋盘,马从左上角跳到右下角,不重复。已知每一行、每一列有多少个格子被经过。求所有可能的路径。
题解
DFS 到点 时,先标记访问过这个点,然后将 和 分别减 。枚举 个方向,算出对应的 。如果合法,未被访问过并且 和 都大于 ,那么去 DFS 这一个点。DFS 结束时将 和标记还原。
当 时,如果所有的 都为 ,那么将路径加入答案列表。最后输出答案。
注:事实上,如果你想要骗分,你可以输出 ,然后不给出路径(按照个数输出任意整数)。在这道题中,有 个测试点答案为 ,所以你能得到 分。
标程
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
const int dx[8] = {-2, -2, -1, -1, 1, 1, 2, 2}, dy[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
int n, m, a[N], b[N];
bool vis[N][N];
struct route {
vector<int> vx, vy;
inline bool operator < (const route &cmp) const {
for (int i = 0; i < vx.size(); i++) {
if (vx[i] != cmp.vx[i])
return vx[i] < cmp.vx[i];
if (vy[i] != cmp.vy[i])
return vy[i] < cmp.vy[i];
}
return 0;
}
inline void push_back(int x, int y) {
vx.push_back(x);
vy.push_back(y);
}
inline void pop_back(void) {
vx.pop_back();
vy.pop_back();
}
inline void output(void) {
cout << vx.size() << ' ';
for (int i = 0; i < vx.size(); i++)
cout << vx[i] << ' ' << vy[i] << ' ';
cout << endl;
}
} now;
set<route> ans;
inline bool check(void) {
for (int i = 1; i <= n; i++)
if (a[i] != 0)
return 0;
for (int j = 1; j <= m; j++)
if (b[j] != 0)
return 0;
return 1;
}
void Dfs(int x, int y) {
vis[x][y] = 1;
a[x]--, b[y]--;
now.push_back(x, y);
if (x == n && y == m) {
if (check())
ans.insert(now);
vis[x][y] = 0;
a[x]++, b[y]++;
now.pop_back();
return;
}
for (int i = 0; i < 8; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
if (vis[nx][ny]) continue;
if (a[nx] == 0 || b[ny] == 0) continue;
Dfs(nx, ny);
}
vis[x][y] = 0;
a[x]++, b[y]++;
now.pop_back();
}
int main(void) {
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int j = 1; j <= m; j++)
cin >> b[j];
Dfs(1, 1);
cout << ans.size() << endl;
for (route r : ans)
r.output();
return 0;
}货物运输
前言
本题改编自吃奶酪,但只考察 DFS(而不是状压 DP)。
预估难度:黄或绿。
题意
从 出发,到 送货。货车只能装一定的体积,并且货物不可被分割。求送完所有货物后回到 的最小距离。
题解
首先,我们知道,一辆车从 出发了,中途一定不会回到 。 否则剩下的那些货物装上去,占用体积而不运输,不如再来一趟车运输。
对于每个货物 ,假设前面的货物一共用了 趟车(注意:用了不代表用完,也不代表用的最少),第 趟车装 号货物,总体积 。
那么对于货物 ,它有以下两种选择:
- 选择已有的一趟车,如果还能装得下,那么试图去运输。
- 新建一趟车,装上 。
注意:我们并不知道运输的顺序,只是记录运输哪些东西。
全部选完后,枚举每一辆车货物如何运输最优(全排列),将所有车的最小值相加,更新答案。
标程
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 12;
int n, m, ans = 1e18, v[N], idx[N], a[N][N];
int tot, status[N], sum[N];
inline int Dist(int s) {
int ans = 1e18;
int cnt = 0;
for (int i = 2; i <= n; i++)
if (s >> (i - 2) & 1)
idx[++cnt] = i;
do {
int res = a[1][idx[1]] + a[idx[cnt]][1];
for (int i = 2; i <= cnt; i++) {
res += a[idx[i - 1]][idx[i]];
if (res >= ans) break;
}
ans = min(ans, res);
} while (next_permutation(idx + 1, idx + cnt + 1));
return ans;
}
void Dfs(int step) {
if (step > n) {
int res = 0;
for (int i = 1; i <= tot; i++)
res += Dist(status[i]);
ans = min(ans, res);
return;
}
for (int i = 1; i <= tot; i++) {
if (sum[i] + v[step] > m) continue;
sum[i] += v[step];
status[i] ^= (1 << step - 2);
Dfs(step + 1);
sum[i] -= v[step];
status[i] ^= (1 << step - 2);
}
tot++;
status[tot] = (1 << step - 2), sum[tot] = v[step];
Dfs(step + 1);
tot--;
}
signed main(void) {
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 2; i <= n; i++)
cin >> v[i];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> a[i][j];
// for (int k = 1; k <= n; k++)
// for (int i = 1; i <= n; i++)
// for (int j = 1; j <= n; j++)
// a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
Dfs(2);
cout << ans << endl;
return 0;
}开灯
前言
本题考查 BFS 求解棋盘问题。
这道题是 USACO 年 月银组原题。
预估难度:黄或绿。(这是个人评价。洛谷上评为蓝,有点虚。)
题意
给定一个棋盘,贝茜只能在亮着灯的格子走动,一些房间有其他房间灯的开关。贝茜从左上角(开始亮着灯)开始,只能上下左右走,求最终最多能开多少灯,而不是能走到多少个格子!(样例如果这样理解还可以通过。)
题解
首先建图,用一个二维 vector(定义:vector<pair<int, int>> graph[105][105])。
记录两个状态: 表示是否走到过(也表示是否可以走到), 表示是否开着灯。
BFS 的过程如下:
- 开始时将 放入队列,标记 和 状态。
- 每次取出队首 ,将所有可以开的灯开开(标记 状态)。
- 随后扫描所有的开开的灯,如果这个格子旁边有可以到达的格子( 被标记过),那么这个格子可以到达,放入队列,标记 。
答案为 ,即开着的灯的数量。
时间复杂度:每个点至多入队一次,每次转移 ,所以总时间复杂度 。,刚好可以通过(有可能需要一点卡常)。
有更优的解法,请自行思考。
标程
#include <bits/stdc++.h>
#define r q.front()
using namespace std;
const int N = 105;
const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, -1, 1, 0};
int n, m, a, b, c, d;
struct node {
int x, y;
};
vector<node> v[N][N];
queue<node> q;
bool vis[N][N], on[N][N];
inline bool check(int x, int y) {
for (int i = 0; i < 4; i++) {
int lx = x + dx[i], ly = y + dy[i];
if (lx < 1 || lx > n || ly < 1 || ly > n) continue;
if (vis[lx][ly]) return 1;
}
return 0;
}
int main(void) {
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a >> b >> c >> d;
v[a][b].push_back({c, d});
}
vis[1][1] = on[1][1] = 1;
q.push({1, 1});
while (!q.empty()) {
int x = r.x, y = r.y; q.pop();
for (auto i : v[x][y]) {
int nx = i.x, ny = i.y;
on[nx][ny] = 1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (on[i][j] && !vis[i][j] && check(i, j)) {
vis[i][j] = 1;
q.push({i, j});
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
ans += on[i][j];
cout << ans << endl;
return 0;
}数学题
前言
骗人题,这么大的数据范围怎么可能是搜索。
本题考查枚举和时间复杂度计算。
预估难度:橙。
初步想法
看到向下取整,心生厌恶。
如果没有向下取整,即 ,结合 可以直接解出 ,,,非常轻松。
于是,我们暴力枚举三个向下取整(范围: 的整数),解出 后再看向下取整是否满足条件。
然而……这样是 的,肯定会超时。
小优化
记 。我们可以只用枚举 的整数。
另外,在枚举的时候,我们需要保证 。
这样,复杂度立刻变成 的!
你肯定不信,请看下面的证明:
复杂度证明
注:此段用了大量的数学知识,并不要求掌握,只需了解。
一共枚举的次数就等于满足 的三元正整数组 的个数,我们只需要证明这种数组的个数与 同阶。
先证明几个引理:
引理 : 对于 ,。 证明: 设 。则
于是 在 上单调递减。 所以 ,故左式小于右式,得证。
引理 : 对于任意正整数 ,。 证明: 由引理 ,,对 求和,得到 ,两边同时加 得到右侧式子。
引理 对于 ,满足 的正整数对 的个数不超过 。 证明: 固定 ,有 个满足条件的 ,所以一共有 个满足条件的正整数对。 由引理 ,,即为结论。
再看原题,固定 时,满足条件的三元数组等于 的个数。由引理 ,可以知道是不超过 的。对 求和,知满足条件的三元数组个数不超过
这个数与 同阶,所以此算法的时间复杂度为 。
标程
#include <bits/stdc++.h>
#define int long long
using namespace std;
int cnt, a, b, c;
signed main(void) {
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> a >> b >> c;
int m = min({a, b, c});
for (int i = 1; i <= m; i++)
for (int j = 1; i * j <= m; j++)
for (int k = 1; i * j * k <= m; k++) {
double x = sqrt(1.0 * b * c * i / a / j / k);
double y = sqrt(1.0 * a * c * j / b / k / i);
double z = sqrt(1.0 * a * b * k / c / i / j);
if ((int)floor(x) == i && (int)floor(y) == j && (int)floor(z) == k)
cnt++;
}
cout << cnt << endl;
return 0;
}