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];
}
相关推荐
WL学习笔记3 分钟前
通讯录(顺序表实现)
c语言·数据结构·算法
Jerryhut22 分钟前
opencv对齐算法及其应用
人工智能·opencv·算法
果丁智能34 分钟前
智慧校园一卡通深度融合方案:基于超级SIM卡的手机碰一碰智能开锁技术落地实践
数据结构·人工智能·python·科技·算法·智能家居·信息与通信
满怀冰雪41 分钟前
第13篇-栈算法入门-括号匹配-表达式与单调栈基础
java·算法
TCW112144 分钟前
AI底层系列:用C++实现线性代数的公式推导与算法设计-基础篇-5.矩阵方程
人工智能·线性代数·算法
叫我:松哥1 小时前
基于机器学习和flask的体育健身风险智能分析系统,系统集成DeepSeek、聚类算法、分类算法等,准确率达90%
人工智能·python·神经网络·算法·机器学习·flask·聚类
wabs6661 小时前
关于动态规划【0-1背包思想在实际问题中是怎么转化的?】
算法·动态规划
阿文的代码库1 小时前
欧拉回路与欧拉路径的算法流程演示
算法
汤姆yu1 小时前
云知声 U2 原生智能体大模型深度解析
大数据·人工智能·算法·ai·大模型·多模态·智能体
syt_biancheng1 小时前
贪心算法(1)---简介
算法·贪心算法