跳到正文

MoonRebirth

1260016

这是 2026年11月13日 存档的第 2 版,不是当前版本。查看当前版本

资料、奖项与版本

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

int n,m; int a[200005];

void slove() { cin >> n >> m; for (int i = 1;i <= n;i ++) cin >> a[i];

C++
priority_queue<int>q;

int ans = 0,cnt = 0;

for (int i = m;i > 1;i --){
    cnt += a[i];
    q.push(a[i]);
    if (cnt>0) {
           cnt -= q.top()*2;
           q.pop();
           ans++;
       }
}
cnt = 0;

priority_queue<int,vector<int>,greater<int>>q2;

for (int i = m + 1;i <= n;i ++) {
       cnt += a[i];
       q2.push(a[i]);
       if(cnt<0){
           cnt -= q2.top()*2;
           q2.pop();
           ans++;
       }
}

cout << ans << '\n';

}

signed main() { ios::sync_with_stdio(0); cin.tie(0);

C++
int T;
cin>>T;
while (T--) {
    slove();
}

return 0;

}