Codeforces Round #536 (Div. 2) B. Lunar New Year and Food Ordering

时间:2023-03-08 22:11:33
Codeforces Round #536 (Div. 2) B. Lunar New Year and Food Ordering
#include <bits/stdc++.h>
#define N 300010
#define PII pair<int, int>
using namespace std; typedef long long LL; int n, m, a[N], c[N], t, d;
LL ans = ; priority_queue<PII, vector<PII>, greater<PII> > Q; int main(){
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++){
scanf("%d", &a[i]);
} for (int i = ; i <= n; i++){
scanf("%d", &c[i]);
Q.push(make_pair(c[i], i));
} for (int i = ; i <= m; i++){
scanf("%d%d", &t, &d);
if (d <= a[t]){
a[t] -= d;
printf("%lld\n", 1LL * d * c[t]);
} else {
bool flag = false;
LL ans = 1LL * a[t] * c[t];
d -= a[t];
a[t] = ;
while (!Q.empty()){
while (!Q.empty() && a[Q.top().second] == ) Q.pop();
if (Q.empty()) break;
PII now = Q.top();
if (d <= a[now.second]){
a[now.second] -= d;
ans += 1LL * d * now.first;
flag = true;
printf("%lld\n", ans);
break;
} else {
ans += 1LL * a[now.second] * now.first;
d -= a[now.second];
a[now.second] = ;
Q.pop();
}
} if (!flag){
puts("");
}
}
}
}

以上是标准程序,只用了155ms,而下面的我的代码用了904ms。差距还是很大的,仔细看,发现是细节的优化。

#include<iostream>
#include<queue>
#include<algorithm>
#define pii pair<int,int>
using namespace std;
int num[];
int price[];
typedef long long LL;
priority_queue<pii,vector<pii>,greater<pii> > q;
int main(){
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++)
cin>>num[i];
for(int i=;i<=n;i++){
cin>>price[i];
q.push(make_pair(price[i],i));
}
for(int i=;i<m;i++){
int t,d;
cin>>t>>d;
LL sum=;
int remain=d;
if(num[t]>=d){
sum+=1LL*d*price[t];
num[t]-=d;
remain=;
}
else{
sum+=1LL*num[t]*price[t];
remain-=num[t];
num[t]=;
}
while(remain>){
if(q.empty()){
cout<<<<endl;
break;
}
pii f=q.top();
if(num[f.second]>remain){
sum+=1LL*remain*f.first;
num[f.second]-=remain;
remain=;
}
else{
sum+=1LL*num[f.second]*f.first;
remain-=num[f.second];
num[f.second]=;
q.pop();
}
}
if(remain==)
cout<<sum<<endl;
}
return ;
}