lz这个解法确实有点点毛病 可以使用滑动窗口做,AC,时间复杂度O(n) 我用C++写的,可以看下 #include<bits/stdc++.h> using namespace std; struct master{ int pos; int hp; master(int _pos,int _hp){ pos=_pos; hp=_hp; } }; bool cmp(master a,master b){ return a.pos<b.pos; } vector<master>v; int main() { int n,y; cin>>n>>y; for(int i=0;i<n;i++){ int x,h; cin>>x>>h; v.push_back(master(x,h)); } sort(v.begin(),v.end(),cmp); int size=2*y; int l=0; int r=0; int ans=0;  while(l<n){ while(r+1<n && v[r+1].pos-v[l].pos <= size)  r++; ans+=v[l].hp; for(int i=r;i>=l;i--) v[i].hp-=v[l].hp; while(l<n && v[l].hp<=0)  l++;  } cout<<ans; return 0; }