AtCoder Beginner Contest 317 题解 ABCDE | JorbanS

A. Potions

cpp 复制代码
int solve() {
    int n, h, x; cin >> n >> h >> x;
    int y = x - h, res = 0, minn = 1e3;
    for (int i = 1; i <= n; i ++) {
        int x; cin >> x;
        if (x >= y && x < minn) {
            res = i;
            minn = y - x;
        }
    }
    return res;
}

B. MissingNo.

cpp 复制代码
int solve() {
    int n; cin >> n;
    set<int> s;
    int l = 1e3, r = 1;
    while (n --) {
        int x; cin >> x;
        l = min(l, x);
        r = max(r, x);
        s.insert(x);
    }
    for (int i = l + 1; i < r; i ++) if (!s.count(i)) return i;
}

C. Remembering the Days

题意 求权值和最大的一条路径

Tag dfs

cpp 复制代码
int n, m, res;
vector<vector<int>> v(N);
vector<vector<int>> d(N, vector<int>(N));
vector<bool> vis(N);

void dfs(int x, int dis) {
    vis[x] = true;
    bool flag = true;
    for (auto i : v[x]) {
        if (vis[i]) continue;
        flag = false;
        dfs(i, dis + d[x][i]);
    }
    if (flag) res = max(res, dis);
    vis[x] = false;
}

int solve() {
    cin >> n >> m;
    while (m --) {
        int a, b, c; cin >> a >> b >> c;
        v[a].push_back(b);
        v[b].push_back(a);
        d[a][b] = d[b][a] = c;
    }
    for (int i = 1; i <= n; i ++) {
        vis.assign(vis.size(), false);
        dfs(i, 0);
    }
    return res;
}

D. President

cpp 复制代码
ll solve() {	
    int n; cin >> n;
    vector<ll> f{0};
    ll sum = 0;
    for (int i = 0; i < n; i ++) {
        int x, y, z; cin >> x >> y >> z;
        sum += z;
        int cost = max(0, (x + y + 1) / 2 - x);
        f.resize(sum + 1, inf);
        for (int i = sum; i >= z; i --)
            f[i] = min(f[i], f[i - z] + cost);
    }
    return *min_element(f.begin() + (sum + 1) / 2, f.end());
}

E. Avoid Eye Contact

题意 n × m n×m n×m 的迷宫,. 代表可以走,# 代表障碍物,<>^v 代表射线,射线方向不能走,知道射线遇到非 . 的格子,求最短路

Tag bfs

cpp 复制代码
int solve() {
    cin >> n >> m;
    for (int i = 0; i < n; i ++) cin >> s[i];
    for (int i = 0; i < n; i ++)
        for (int j = 0; j < m; j ++)
            d[i][j] = -1, vis[i][j] = false;
    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < m; j ++) {
            char c = s[i][j];
            int op = 0;
            if (c == '#') vis[i][j] = true;
            else if (c == 'S') sx = i, sy = j;
            else if (c == 'G') gx = i, gy = j;
            else if (c == '>') op = 1;
            else if (c == '<') op = 2;
            else if (c == 'v') op = 3;
            else if (c == '^') op = 4;
            if (!op) continue;
            int px = i, py = j;
            while (check(px, py) && (s[px][py] == '.' || i == px && j == py)) {
                vis[px][py] = true;
                px += dx[op], py += dy[op];
            }
        }
    }
    d[sx][sy] = 0;
    queue<Node> q;
    q.push({sx, sy});
    while (!q.empty()) {
        Node t = q.front();
        q.pop();
        for (int i = 1; i < 5; i ++) {
            int px = t.x + dx[i], py = t.y + dy[i];
            if (!check(px, py) || vis[px][py] || d[px][py] != -1) continue;
            d[px][py] = d[t.x][t.y] + 1;
            q.push({px, py});
        }
    }
    return d[gx][gy];
}
相关推荐
JieE21212 小时前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
JieE2121 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack202 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树2 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2122 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2122 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术3 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦3 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
用户497863050733 天前
(一)小红的数组操作
算法·编程语言