fix(mn1009t2): 修复队列处理逻辑中的数量更新错误

修正队列处理时未正确更新剩余数量的问题,确保计算准确
添加测试用例和性能优化实现
This commit is contained in:
Zengtudor 2025-10-10 16:22:52 +08:00
parent 8e8776d58b
commit 67b34507d8
3 changed files with 80 additions and 1 deletions

View File

@ -7,7 +7,7 @@ using ll = int64_t;
const ll maxn = 2e6+5;
ll n,m,v,d[maxn],p[maxn],ans=0;
std::deque<std::pair<ll, ll>> dq; // 价钱,剩余
std::deque<std::pair<ll, ll>> dq; // 价钱,剩余个数
int main(){
std::iostream::sync_with_stdio(false);
@ -24,6 +24,7 @@ int main(){
while(d[i]){
if(dq.front().second > d[i]){
ans+=(dq.front().first+i*m)*d[i];
dq.front().second-=d[i];
d[i]=0;
}else if(dq.front().second == d[i]){
ans+=(dq.front().first+i*m)*d[i];

24
src/10/10/mn1009t2pai.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <chrono>
#include <cstdint>
#include <iostream>
#include <istream>
#include <random>
using ll = int64_t;
std::mt19937 mt (std::chrono::high_resolution_clock::now);
std::uniform_int_distribution<ll> un(1,2e6);
int main(){
std::iostream::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n = un(mt);
std::cout<<n<<"\n";
for(ll i=1;i<=n;i++){
std::cout<<un(mt)<<" ";
}
std::cout<<"\n";
for(ll i=1;i<=n;i++){
std::cout<<un(mt)<<" ";
}
std::cout<<"\n";
}

54
src/10/10/mn1009t2t.cpp Normal file
View File

@ -0,0 +1,54 @@
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
const int N=2e6+100;
LL d[N],p[N],q[N],w[N];
int main()
{
// freopen("ice.in","r",stdin);
// freopen("ice.out","w",stdout);
int n,m,v,i;
cin>>n>>m>>v;
LL ans=0,dt=0,s,dn=0;
for (i=1;i<=n;i++) {
cin>>d[i];
}
for (i=1;i<=n;i++) {
cin>>p[i];
}
int l=1,r=0;
for (i = 1; i <= n; i++) {
// 1. 维护队列单调性:移除价格更高的元素
while (l <= r && p[i] - dt <= p[q[r]]) r--;
s = d[i]; // 当前需求量
// 2. 使用队列中更便宜的冰棍
while (l <= r)
if (s < w[q[l]] - dn) {
// 当前需求小于队首能提供的数量
ans += (p[q[l]] + dt) * s; // 使用s支
dn += s; // 累计使用量增加
s = 0;
break;
} else {
// 当前需求大于等于队首能提供的数量
ans += (p[q[l]] + dt) * (w[q[l]] - dn);
s -= w[q[l]] - dn;
dn = w[q[l++]]; // 队首元素完全使用,出队
}
// 3. 剩余需求用当前天的价格满足
ans += 1LL * s * p[i];
// 4. 将当前天加入队列
q[++r] = i;
w[i] = v + dn; // 当前天最多能提供v支考虑容量
p[i] -= dt; // 调整价格(减去时间偏移)
dt += m; // 时间偏移增加(存储成本)
}
printf("%lld\n",ans);
return 0;
}