跳到正文

个人记录

SDFZ-OJ 考试题解

stripe_python约 14 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月30日
篇幅
约 14 分钟

版本

  1. v1当前

T1

你说的对,但是本蒟蒻 T1 读错题卡了 1.5h。

模拟即可。好像不用写这么多。

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = (1 << 20) + 1;

int n, k;
bool tag[N];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cin >> n >> k;
    int m = 1 << n;
    k--;
    while (k--) {
        int cur = 1;
        while (cur < m) {
            tag[cur] ^= 1;
            cur = tag[cur] ? (cur << 1) : (cur << 1 | 1);
        }
    }
    int cur = 1;
    while (cur < m) {
        tag[cur] ^= 1;
        if ((tag[cur] ? (cur << 1) : (cur << 1 | 1)) >= m) {
            return cout << cur, 0;
        }
        cur = tag[cur] ? (cur << 1) : (cur << 1 | 1);
    }

    return 0;
}

T2

70 pts

首先用 O(nn)O(n \sqrt {n}) 时间预处理约数和,并建图。采用 01-BFS,求出全图最长路。

C++
#include <bits/stdc++.h>
#define N 50005
using namespace std;

int n, a[N];

int tot = 0;
int head[N << 2];
struct Edge {
    int next, to;
} edge[N << 3];
inline void add_edge(int u, int v) {
    edge[++tot].next = head[u];
    edge[tot].to = v;
    head[u] = tot;
}

int dis[N];
bool vis[N];
inline void bfs(int s) {
    memset(dis, 0xcf, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    queue<int> q;
    q.push(s);
    dis[s] = 0;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        if (vis[u])
            continue;
        vis[u] = true;
        for (int j = head[u]; j != 0; j = edge[j].next) {
            int v = edge[j].to;
            if (dis[v] < dis[u] + 1) {
                dis[v] = dis[u] + 1;
                q.push(v);
            }
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j * j <= i; j++) {
            if (i % j == 0 && j != i) {
                a[i] += j;
                if (i / j != j && i / j != i)
                    a[i] += i / j;
            }
        }
    }
    // for (int i = 1; i <= n; i++) cout << a[i] << ' ';
    for (int u = 1; u <= n; u++) {
        if (a[u] <= n && a[u] < u)
            add_edge(u, a[u]), add_edge(a[u], u);
    }
    int res = 0;
    for (int u = 1; u <= n; u++) {
        bfs(u);
        res = max(res, *max_element(dis + 1, dis + n + 1) - 1);
    }
    cout << res;

    return 0;
}

复杂度 O(n2)O(n^2),TLE 70pts。

80 pts

随机地选取一些点作为源点跑 01-BFS。

100 pts

注意到,建出的图是一棵树或一片森林。求出树的直径即可。复杂度 O(nn)O(n \sqrt {n})。

T3

本题数据出锅。本蒟蒻写出 AC Code 应该花了 20min 左右。

82 pts

爆搜。令 dfs(x,t,w)dfs(x, t, w) 表示在第 xx 个湖泊,已停留 tt 时间,剩余时间为 ww 时的最优值。

  1. 到下一个湖泊,答案为 dfs(x+1,0,w−tx)dfs(x + 1, 0, w - t_x)。
  2. 在本湖泊停留,答案为 dfs(x,t+1,w−1)+fx−t×dxdfs(x, t + 1, w-1) + f_x - t \times d_x。

取 max⁡\max。

100 pts

记忆化搜索即可。

C++
#include <bits/stdc++.h>
#define N 50
using namespace std;

int n, h, f[N], d[N], tt[N];
int dp[205][205][205];

int dfs(int x, int t, int w) {
	//第x个湖泊,已停留t时间,剩余时间为w
	if (dp[x][t][w] > 0) return dp[x][t][w];
	if (w <= 0 || x > n) return 0;
	int res = 0;
	if (w - tt[x] >= 0) res = max(res, dfs(x + 1, 0, w - tt[x]));
	if (w - 1 >= 0 && f[x] > t * d[x]) res = max(res, dfs(x, t + 1, w - 1) + f[x] - t * d[x]);
	return dp[x][t][w] = res;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	cin >> n >> h;
	h *= 12;
	for (int i = 1; i <= n; i++) cin >> f[i];
	for (int i = 1; i <= n; i++) cin >> d[i];
	for (int i = 1; i < n; i++) cin >> tt[i];
	memset(dp, 0xcf, sizeof(dp));
	cout << dfs(1, 0, h);
	return 0;
}

PS:以上代码复杂度较高。本题 std 是堆优化贪心。

T4

50 pts

暴力 BFS。每次贪心地扩展到最小的字符。

C++
#include <bits/stdc++.h>
#define N 2005
using namespace std;

int n, m;
string s;
char a[N][N];

struct state {
    int x, y;
    string s;

    state() {}
    state(int a, int b) : x(a), y(b) {}
    state(int a, int b, char c) : x(a), y(b) { s.push_back(c); }
    state(int a, int b, const string& c) : x(a), y(b), s(c) {}
};

queue<state> q;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> s;
        for (int j = 1; j <= m; j++) a[i][j] = s[j - 1];
    }

    q.emplace(1, 1, a[1][1]);
    string res;
    while (!q.empty()) {
        int x = q.front().x, y = q.front().y;
        string t = q.front().s;
        q.pop();
        if (x == n && y == m) {
            if (res.empty())
                res = t;
            else
                res = min(res, t);
            continue;
        }
        if (y + 1 <= m && (x + 1 > n || a[x + 1][y] > a[x][y + 1])) {
            t.push_back(a[x][y + 1]);
            q.emplace(x, y + 1, t);

        } else if (x + 1 <= n && a[x + 1][y] < a[x][y + 1]) {
            t.push_back(a[x + 1][y]);
            q.emplace(x + 1, y, t);
        } else {
            //相等
            if (y + 1 <= m) {
                t.push_back(a[x][y + 1]);
                q.emplace(x, y + 1, t);
            }
            if (x + 1 <= n) {
                t.push_back(a[x + 1][y]);
                q.emplace(x + 1, y, t);
            }
        }
    }
    cout << res;
    return 0;
}

56 pts

相等时特判。会爆空间。

C++
#include <bits/stdc++.h>
//#include <bits/extc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#define N 2005
using namespace std;
using namespace __gnu_pbds;

int n, m;
string s;
char a[N][N];

int ghash = 1;

struct state {
    int x, y, h;

    state() {}
    state(int a, int b) : x(a), y(b), h(++ghash) {}
};

queue<state> q;
cc_hash_table<int, int> mp;  // ghash
cc_hash_table<int, state> mp2;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> s;
        for (int j = 1; j <= m; j++) a[i][j] = s[j - 1];
    }

    srand(time(0));

    q.emplace(1, 1);
    int ff = ghash;
    string res;
    while (!q.empty()) {
        int x = q.front().x, y = q.front().y, h = q.front().h;
        mp2[h] = q.front();
        q.pop();

        if (x == n && y == m) {
            string k;
            for (int c = h; c != ff; c = mp[c]) {
                // c: hash
                int x = mp2[c].x, y = mp2[c].y;
                k.push_back(a[x][y]);
            }
            k.push_back(a[1][1]);
            reverse(k.begin(), k.end());
            if (res.empty())
                res = k;
            else
                res = min(res, k);

            continue;
        }
        if (y + 1 <= m && (x + 1 > n || a[x + 1][y] > a[x][y + 1])) {
            state nxt(x, y + 1);
            mp[nxt.h] = h;
            q.emplace(nxt);
        } else if (x + 1 <= n && a[x + 1][y] < a[x][y + 1]) {
            state nxt(x + 1, y);
            mp[nxt.h] = h;
            q.emplace(nxt);
        } else {
            //相等
            if (y + 1 <= m) {
                state nxt(x, y + 1);
                mp[nxt.h] = h;
                q.emplace(nxt);
            }
            if (x + 1 <= n) {
                state nxt(x + 1, y);
                mp[nxt.h] = h;
                q.emplace(nxt);
            }
        }
    }
    cout << res;
    return 0;
}

100 pts

只需要记录一个最优路径(初始化为全 z),在扩展时,判断当前位置的扩展是否优于最优路径。

原文