大佬,这是我写的第三题,我没有参加笔试,看我写的对吗,单调队列维护这, #include <bits/stdc++.h> const int N = 1e5; const int M = 1e9; using namespace std; typedef long long ll; struct node { int x, val; bool operator<(const node& n) const { return x < n.x; } }e[N]; int n,k; //数量和 范围 int main() { cin >> n >> k; for(int i = 1; i <= n; i++) { cin >> e[i].x >> e[i].val; } sort(e + 1, e + 1 + n); deque<int> dq; int sum = 0; int max_ = 0; for(int i = 1; i <= n; i++) { while(dq.size() && e[i].x - e[dq.front()].x > 2*k) { sum += max_; dq.pop_front(); max_ = 0; } dq.push_back(i); max_ = max(max_,e[i].val); } sum += max_; cout << sum << endl; }