CF2203E
Gaochenxi103_QAQ · · 题解
做法
首先
其次,把新增
假如 Alice 选择了一个
先说结论,无论
此时;
- 若 Bob 选择
a ,那么期望是。\ - 若 Bob 选择
b ,那么期望是。\
::::info[证明]
对于选择左边的
所以选择如果知道了 Alice 选
在固定的
这里就可以用三分,但复杂度要多一个
三分做法就不赘述了。对于二分,如果当前的
于是只需要二分
注意:二分计算的两者期望不能先取模,因为要比大小;所以要把 long long,也不会出现分数。
注意到二分的时候直接对
对于当前的
找到小于
然后
那么
同理
然后维护,最值,和,数量即可。
代码
::::success[Accepted code]
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
const int mod=998244353;
struct S{
int l,r,sum,num,mx,mi;
}tree[N*4];
int n,a[N],b[N];
unordered_map<int,int>mp;
unordered_map<int,int>rmp;
int mypow(int a,int k)
{
if(k==0)return 1;
int res=a;k--;
while(k)
{
if(k&1)res=(res*a)%mod;
a=(a*a)%mod;
k>>=1;
}
return res;
}
void build(int root,int l,int r)
{
tree[root].l=l,tree[root].r=r;
if(l==r)
{
tree[root].num=tree[root].sum=0;
tree[root].mx=-1;
tree[root].mi=1e17+7;
return ;
}
int mid=(l+r)/2;
build(root*2,l,mid);
build(root*2+1,mid+1,r);
tree[root].mx=max(tree[root*2].mx,tree[root*2+1].mx);
tree[root].mi=min(tree[root*2].mi,tree[root*2+1].mi);
}
void updata(int root,int x)
{
if(tree[root].l==tree[root].r)
{
tree[root].num=1;
tree[root].sum=tree[root].mi=tree[root].mx=rmp[x];
return ;
}
int mid=(tree[root].l+tree[root].r)/2;
if(x<=mid)updata(root*2,x);
else updata(root*2+1,x);
tree[root].mx=max(tree[root*2].mx,tree[root*2+1].mx);
tree[root].mi=min(tree[root*2].mi,tree[root*2+1].mi);
tree[root].num=tree[root*2].num+tree[root*2+1].num;
tree[root].sum=(tree[root*2].sum+tree[root*2+1].sum);
}
pair<int,int> merge(pair<int,int> x,pair<int,int> y)
{
int A=x.first+y.first;
int B=x.second+y.second;
return {A,B};
}
pair<int,int> query(int root,int l,int r)
{
if(l<=tree[root].l && tree[root].r<=r)
{
return {tree[root].num,tree[root].sum};
}
int mid=(tree[root].l+tree[root].r)/2;
pair<int,int> res={0,0};
if(l<=mid)res=merge(res,query(root*2,l,r));
if(mid+1<=r)res=merge(res,query(root*2+1,l,r));
return res;
}
int findmx(int root,int l,int r)
{
if(l<=tree[root].l && tree[root].r<=r)
{
return tree[root].mx;
}
int mid=(tree[root].l+tree[root].r)/2;
int res=-1;
if(l<=mid)res=max(res,findmx(root*2,l,r));
if(mid+1<=r)res=max(res,findmx(root*2+1,l,r));
return res;
}
int findmi(int root,int l,int r)
{
if(l<=tree[root].l && tree[root].r<=r)
{
return tree[root].mi;
}
int mid=(tree[root].l+tree[root].r)/2;
int res=1e17+7;
if(l<=mid)res=min(res,findmi(root*2,l,r));
if(mid+1<=r)res=min(res,findmi(root*2+1,l,r));
return res;
}
int pre(int x)
{
int cnt=findmx(1,1,x-1);
return mp[cnt];
}
int nxt(int x)
{
int cnt=findmi(1,x+1,n);
return mp[cnt];
}
int L(int x)//Alice选x,Bob选左边的值
{
if(pre(x)<1)return -1e18-7;
int A=b[pre(x)];
auto p=query(1,1,pre(x)-1);
int B=p.first;
int C=p.second;
return A*B-C;
}
int R(int x)
{
if(nxt(x)<1)return -1e18-7;
int A=b[nxt(x)];
auto p=query(1,nxt(x)+1,n);
int B=p.first;
int C=p.second;
return C-A*B;
}
void solve()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)b[i]=a[i];
sort(b+1,b+n+1);
for(int i=1;i<=n;i++)mp[b[i]]=i,rmp[i]=b[i];
build(1,1,n);
updata(1,mp[a[1]]);
updata(1,mp[a[2]]);
for(int i=3;i<=n;i++)
{
updata(1,mp[a[i]]);
int l=1,r=n,res=max(L(mp[a[i]]),R(mp[a[i]]));
while(l<=r)
{
int mid=(l+r)/2;
int xx=L(mid),yy=R(mid);
if(xx<=yy)
{
res=min(res,yy);
l=mid+1;
}
else
{
res=min(res,xx);
r=mid-1;
}
}
cout<<((res%mod+mod)%mod*mypow(i-2,mod-2))%mod<<"\n";
}
}
signed main()
{
// freopen("E.in","r",stdin);
// freopen("E.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
::::