分析 代码 #includebits/stdc++.h using namespace std; #define int long long const int inf = 1e18; char s[ 1000100 ]; int a[ 1000100 ],pre[ 1000100 ],sur[ 1000100 ],mn[ 1000100 ]; signed main(){ int n,m,p,q,i,j,k,x,y,Ans= inf; cin npqx y;
分析
代码
#include<bits/stdc++.h>
using namespace std; #define int long long
const int inf =1e18; char s[1000100]; int a[1000100],pre[1000100],sur[1000100],mn[1000100]; signed main(){ int n,m,p,q,i,j,k,x,y,Ans=inf; cin>>n>>p>>q>>x>>y; scanf("%s",s+1); for(i=1;i<=n;i++)a[i]=(s[i]==‘-‘?-1:1); for(i=1;i<=n;i++)pre[i]=pre[i-1]+a[i]; for(i=1;i<=n;i++)mn[i]=min(mn[i-1],pre[i]); k=(q-p-pre[n])/2; for(i=1;i<=n;i++){ int sum=min(pre[n]-pre[n-i+1]+mn[n-i+1],sur[i-1]); int t=max(0ll,(-p-sum+1)/2); Ans=min(Ans,(abs(k-t)+t)*x+(i-1)*y); sur[i]=min(sur[i-1]+a[n-i+1],0ll); } cout<<Ans; return 0; }