数据规模很小,可以直接用小根堆,以购买日期为关键字维护鸡蛋数量。
#include<iostream>
#include<queue>
using namespace std;
int T;
int n,d;
struct node
{
int cnt,date;
bool operator > (const node &a) const
{
return date > a.date;
}
};
priority_queue <node,vector<node>,greater<node>> q;
int a[200005],b[200005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> T;
while(T--)
{
cin >> n >> d;
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = 1;i <= n;i++) cin >> b[i];
for(int i = 1;i <= n;i++)
{
q.push({a[i],i});
while(b[i])
{
if(b[i] >= q.top().cnt)
{
b[i] -= q.top().cnt;
q.pop();
}
else
{
node tmp = q.top();
q.pop();
tmp.cnt -= b[i];
b[i] = 0;
q.push(tmp);
}
}
while(!q.empty() && q.top().date <= i - d)
{
q.pop();
}
}int ans = 0;
while(!q.empty())
{
ans += q.top().cnt;
q.pop();
}
cout << ans << '\n';
}
}