题解:P14595 [COCI 2025/2026 #2] 集训 / Dodatna
jhashexcoder · · 题解
理解题意
给定
思路
将学生按左端点
遍历时,前
用最小堆维护这
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<pair<int, int>> a(n);
for (auto &p : a) cin >> p.first >> p.second;
sort(a.begin(), a.end());
priority_queue<int, vector<int>, greater<int>> pq;
long long ans = 0;
for (int i = 0; i < n; ++i) {
pq.push(a[i].second);
if ((int)pq.size() > k) pq.pop();
if ((int)pq.size() == k && pq.top() > a[i].first)
ans = max(ans, (long long)pq.top() - a[i].first);
}
cout << ans << '\n';
return 0;
}
- 使用AI完善了公式化表达并优化了语言表达