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];
}
相关推荐
shirsl7 小时前
算法 Day 2 滑动窗口 + 栈 / 单调栈
开发语言·python·算法
土司大王8 小时前
LeetCode hot100 回溯专题总结:Java 通用模板、决策树模型
java·算法·leetcode·决策树
2601_962218618 小时前
万象生鲜系统订单全生命周期状态同步技术实现业务可视
大数据·数据库·人工智能·python·算法
那年窗外下的雪.9 小时前
AIDC 学习日志|第 22 天|All-Active/Single-Active 故障演练设计
服务器·网络·学习·算法·哈希算法
wzdark10 小时前
基于启发式搜索的最优路径规划算法研究4
算法
l1t10 小时前
用superpi、tinypi、tpi等工具计算圆周率的比较
c语言·算法
a1879272183110 小时前
【算法】链表(二):链表上的双指针——变速、异链与定距,和一份路程账本
数据结构·算法·leetcode·链表·go·指针·环形链表
2601_9583529011 小时前
还要写 AEC 算法?0 代码 + 6 个引脚,F-18 让通话清晰度提升 300%
人工智能·算法·降噪消回音
happyprince11 小时前
03-regmix-深刻观-哲学与升华
算法
一路向阳~负责的男人11 小时前
运动控制算法收录
算法