Please sign in first.
C - Omelette Restaurant Editorial
by
YocyCraft
O(N) solution
The problem can be solved in \(O(n)\) by the same algorithm of offcial solution, with Run-length Compression on the queue.
using pii = pair<int, int>;
const int MAX=200005;
int a[MAX];
int b[MAX];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int _t;
cin>>_t;
while(_t--){
int n,d;
cin>>n>>d;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
cin>>b[i];
}
queue<pii> eggs;
ll ans=0;
for(int i=0;i<n;i++){
eggs.emplace(i,a[i]);
ans+=a[i];
int t=b[i];
ans-=b[i];
while(t>0){
auto& [j,cnt]=eggs.front();
int s=min(t,cnt);
t-=s;
cnt-=s;
if(s==0) eggs.pop();
}
while(!eggs.empty()){
auto [j,cnt]=eggs.front();
if(j>i-d) break;
ans-=cnt;
eggs.pop();
}
}
cout<<ans<<'\n';
}
}
posted:
last update:
