不会取标题
Dragon33038 · · 题解
题面
题面
注意到标签单调队列所以考虑使用单调队列解决这道题。
思路
为了让总花费最少,我们就要尽量在价格小的时候存储电量。我们既需要维护当前的最小费用和电池容量,又要防止比当前价格更大的时刻储存的电量在现在使用,单调队列显然是维护这个东西不错的选择。由于只要每种电量购买次数不变,不管储存的电量给哪个时刻使用都不会影响总和,所以直接取队列内最小的给当前使用是不劣的。
接下来我们考虑如何处理容量。
由于我们不知道接下来的决策,我们应该记录当前能用的时刻而不是直接买,当消耗储存的电量时再计算价格。为了防止容量超出总额
要注意会爆 unsigned long long。
代码
#include <bits/stdc++.h>
using namespace std;
#define QwQ return
#define egg 0;
#define retrun return
#define int long long
#define iny unsigned long long
// ? ? ?
// ? ?
//? ?
//? ?
// ?
// ?
// ?
// ?
//
// ?
constexpr int maxn=5e5+14;
constexpr int maxm=1e2+14;
constexpr int maxl=5e6+13;
constexpr double pi=acos(-1);
constexpr double eps=1e-7;
constexpr int mod=1e9+7;
constexpr int inf=LONG_LONG_MAX>>1;
constexpr double INF=1e12;
__int128 n,k;
__int128 w[maxn];
__int128 c[maxn];
inline __int128 read()
{
__int128 x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
{
f=-1;
}
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
inline void print(__int128 x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
print(x/10);
}
putchar(x%10+'0');
}
signed main()
{
n=read();
k=read();
for(int i=1;i<=n;++i)
{
w[i]=read();
}
for(int i=1;i<=n;++i)
{
c[i]=read();
}
deque<pair<int,__int128>>q;
__int128 ans=0;
__int128 can=k;
for(int i=1;i<=n;++i)
{
while(!q.empty()&&c[q.back().first]>=c[i])
{
can+=q.back().second;
q.pop_back();
}
while(!q.empty())
{
pair<int,__int128>l=q.front();
q.pop_front();
if(l.second>w[i])
{
can+=w[i];
l.second-=w[i];
ans+=w[i]*c[l.first];
w[i]=0;
q.push_front(l);
break;
}
else
{
can+=l.second;
w[i]-=l.second;
ans+=c[l.first]*l.second;
}
}
ans+=w[i]*c[i];
//printf("%lld %lld\n",ans,can);
q.push_back({i,can});
can=0;
}
print(ans);
/*
//I often die
//I often die
//I often die
//I often die
//I often die
//I often die
//I often die
//I often die
//I always die */
QwQ egg
}