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];
}
相关推荐
find1star2 小时前
LeetCode 141:环形链表
java·算法·leetcode·链表
迅翼SwiftWing2 小时前
编队系列(总)|固定翼编队如何实现稳定飞行?通信架构、协同算法与工程约束深度解析
算法·架构
LB21122 小时前
力扣160 21 86
算法·leetcode·职场和发展
小智老师PMP4 小时前
2026深度解析|PMP第八版与NPDP核心侧重点本质区别(管理类证书怎么选)
开发语言·分布式·算法·职场和发展·产品经理
HugoStudio_SWAN4 小时前
洛谷 B4450 / B3867 / B3923 智慧购物、储蓄与做题——从程序到生活
c++·学习·程序人生·算法·生活
a187927218314 小时前
【算法】动态规划第二篇:双序列 DP 三课——继承、计步与断链
算法·leetcode·动态规划·dp·回溯·暴力·算法讲解
江畔柳前堤5 小时前
前台·中台·后台:2026年AI原生时代的架构全景图
开发语言·人工智能·算法·机器学习·架构·scala·ai-native
文心快码BaiduComate5 小时前
2026 OPC x AI Coding 趋势案例交流会圆满落幕:一个人就一支研发团队
算法
ACM-moran5 小时前
条件显化(二分单峰函数+构造数列)
算法·推公式·整数三分
闻缺陷则喜何志丹6 小时前
【栈 运算系统】P3719 [AHOI2017初中组] rexp|普及+
c++·算法··洛谷·运算系统