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];
}
相关推荐
码流怪侠3 小时前
2026年8月GitHub热榜深度拆解:Agent Skills席卷开源圈,一个“技能包“收割5万星
算法·程序员·github
hold?fish:palm3 小时前
30 两两交换链表中的节点
数据结构·算法·链表
Nil2083 小时前
leetcode 98验证二叉搜索树
算法·leetcode·职场和发展
不正经学生3 小时前
C语言结构体:自定义数据类型,让变量打包出行
java·c语言·开发语言·数据结构·算法
cxr8284 小时前
数学的本质:关系、模式与不变量的结构世界
人工智能·算法·机器学习
初夏睡觉5 小时前
原创题目 - 空壳恋爱
c++·算法·dp·oi·信息学·原创题目·值得挑战的题目
Buke..5 小时前
【APP 逆向】哔哩哔哩 sign 参数逆向(下):OLLVM 混淆还原 sign 算法
java·javascript·爬虫·python·算法
linux-hzh6 小时前
百日算法修炼 · Day 15
java·算法
闲研随记6 小时前
【文献阅读 ICLR 2026】RL算法:DECS
算法·llm·强化学习·iclr·rl
量化小c6 小时前
一行代码查 BTCUSDT 和 AAPL 最新价?QuantDash 统一多市场实时行情接口实战
后端·算法·github