当前位置:首页 > 技术 > 正文内容

Codeforces Round 980 Div. 2 题解分析

访客 技术 2026年7月13日 7

A 题:基础数值计算

本题考察简单的条件判断与方程求解。给定两个整数,需根据大小关系决定输出策略。

核心逻辑:当第二个数不超过第一个数时,直接返回第一个数;否则通过方程推导,结果为 2*a - b 与 0 的较大值。

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int tc;
    cin >> tc;
    while (tc--) {
        long long p, q;
        cin >> p >> q;
        if (p >= q) {
            cout << p << '\n';
        } else {
            cout << max(2LL * p - q, 0LL) << '\n';
        }
    }
    return 0;
}

B 题:贪心策略与累计消耗

问题可转化为:将数组升序排列后,模拟逐层消耗资源的过程。每次将当前层所有元素提升至同一高度,计算所需代价。

关键观察:排序后,第 i 个元素与第 i-1 个元素的高度差,需要作用于后续 n-i+1 个元素。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int cases;
    cin >> cases;
    while (cases--) {
        int m;
        ll remain;
        cin >> m >> remain;
        vector<ll> h(m);
        for (auto &x : h) cin >> x;
        sort(h.begin(), h.end());
        
        ll res = 0, prev = 0;
        for (int i = 0; i < m; i++) {
            ll need = (h[i] - prev) * (m - i);
            res++;
            if (remain <= need) {
                res += remain;
                remain = 0;
                break;
            }
            remain -= need;
            res += need;
            prev = h[i];
        }
        cout << res - 1 << '\n';
    }
    return 0;
}

C 题:数对排序规则设计

需要为数对定义一种全序关系,使得按此顺序连接后满足特定条件。通过分析数对的包含关系,可确定排序关键字。

排序策略:以数对的最小值为首要关键字,最大值为次要关键字升序排列。这样可保证若一个数对完全"支配"另一个,则前者排在前面。

#include <bits/stdc++.h>
using namespace std;

struct PairInfo {
    int first, second;
    int lo, hi;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int cases;
    cin >> cases;
    while (cases--) {
        int n;
        cin >> n;
        vector<PairInfo> arr(n);
        for (auto &it : arr) {
            cin >> it.first >> it.second;
            it.lo = min(it.first, it.second);
            it.hi = max(it.first, it.second);
        }
        
        sort(arr.begin(), arr.end(), [](const PairInfo &x, const PairInfo &y) {
            if (x.lo != y.lo) return x.lo < y.lo;
            return x.hi < y.hi;
        });
        
        for (const auto &it : arr) {
            cout << it.first << ' ' << it.second << ' ';
        }
        cout << '\n';
    }
    return 0;
}

D 题:图论建模与最短路

将跳跃过程抽象为带权有向图,通过最短路算法求解最优策略。每个位置有两种转移方式:向左移动无代价,或跳跃到指定位置但损失当前分数。

算法思路:从起点运行 Dijkstra 算法,得到到达每个位置的最小损失。最终答案为前缀和减去对应位置的最短路距离的最大值。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Edge = pair<ll, int>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int cases;
    cin >> cases;
    while (cases--) {
        int n;
        cin >> n;
        vector<ll> score(n), jump(n);
        for (auto &x : score) cin >> x;
        for (auto &x : jump) cin >> x;
        
        vector<vector<Edge>> graph(n);
        for (int i = 0; i < n; i++) {
            if (i > 0) graph[i].push_back({0, i - 1});
            graph[i].push_back({score[i], jump[i] - 1});
        }
        
        const ll INF = 1e18;
        vector<ll> dist(n, INF);
        vector<bool> seen(n, false);
        priority_queue<Edge, vector<Edge>, greater<>> pq;
        
        dist[0] = 0;
        pq.push({0, 0});
        while (!pq.empty()) {
            auto [d, u] = pq.top(); pq.pop();
            if (seen[u]) continue;
            seen[u] = true;
            for (auto [w, v] : graph[u]) {
                if (dist[v] > d + w) {
                    dist[v] = d + w;
                    pq.push({dist[v], v});
                }
            }
        }
        
        ll pref = 0, best = 0;
        for (int i = 0; i < n; i++) {
            pref += score[i];
            best = max(best, pref - dist[i]);
        }
        cout << best << '\n';
    }
    return 0;
}

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Mac 安装 Node.js 指南

方法一:通过官网安装包(最简单,适合初学者)如果你只是想快速安装并开始使用,这是最直接的方法。访问 Node.js 官网。页面会显示两个版本:LTS (Recommended For Most Users):长期支持版,最稳定。建议选这个。Current:最新特性版,包含最新功能但可能不够稳定。下载 .pkg 安装包并运行。按照安装向导点击“下一步”即可完成。方法二:使用 Homebrew 安装(...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。