定义一个b[] --> AC
不定义一个b[] --> 11
然而我并没有用到b[]数组啊??!
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll k;
ll a[114514],b[114514],op;
int root[114514];
int cnt;
struct Node
{
int ls,rs;
ll sum;
ll s;
} tr[50451400];
ll ans=1e18,ansl,anss;
ll gen;
int update(ll p,int l,int r,ll k)
{
int rt=++cnt;
tr[rt].ls=tr[p].ls;
tr[rt].rs=tr[p].rs;
if(l==r)
{
tr[rt].sum=tr[p].sum+1;
tr[rt].s=tr[p].s+l;
}
if(l<r)
{
int mid=(l+r)>>1;
if(k<=mid)
tr[rt].ls=update(tr[rt].ls,l,mid,k);
else
tr[rt].rs=update(tr[rt].rs,mid+1,r,k);
tr[rt].sum=tr[tr[rt].ls].sum+tr[tr[rt].rs].sum;
tr[rt].s=tr[tr[rt].ls].s+tr[tr[rt].rs].s;
}
return rt;
}
ll askmid(int u,int v,int l,int r,ll k)
{
if(l==r)
return l;
int x=tr[tr[v].ls].sum-tr[tr[u].ls].sum;
int mid=(l+r)>>1;
if(x>=k)
return askmid(tr[u].ls,tr[v].ls,l,mid,k);
else
return askmid(tr[u].rs,tr[v].rs,mid+1,r,k-x);
}
ll askres(int u,int v,int l,int r,ll k)
{
if(l==r)
return min(k,tr[v].sum-tr[u].sum)*l;
int x=tr[tr[v].ls].sum-tr[tr[u].ls].sum;
int mid=(l+r)>>1;
if(x>=k)
return askres(tr[u].ls,tr[v].ls,l,mid,k);
else
return tr[tr[v].ls].s-tr[tr[u].ls].s+askres(tr[u].rs,tr[v].rs,mid+1,r,k-x);
}
int main()
{
cin>>n>>k;
op=1e6+114;
for(int i=1;i<=n;i++)
cin>>a[i];
//root[0]=build(0,op);
for(int i=1;i<k;i++)
{
gen+=a[i]-a[i-k];
root[i]=update(root[i-1],0,op,a[i]);
//cout<<a[i]<<' ';
}
/*if(k==1)
{
ans=114514/0;
} */
for(int i=k;i<=n;i++)
{
root[i]=update(root[i-1],0,op,a[i]);
gen+=a[i]-a[i-k];
int mid=askmid(root[i-k],root[i],0,op,(k+1)/2);
// cout<<mid<<' ';
ll res=askres(root[i-k],root[i],0,op,(k+1)/2);
res=gen-2*res-(k-(k+1)/2)*mid+(k+1)/2*mid;
if(res<ans)
{
ans=res;
ansl=i-k+1;
anss=mid;
}
}
/*ans=0;
for(int i=1;i<=k;i++)
{
ans+=a[i];
a[i]=0;
}*/
cout<<ans<<"\n";
//cout<<ans<<'\n';
for(int i=1;i<=n;i++)
{
if(i==ansl)
{
for(int j=i;j<=i+k-1;j++)
cout<<anss<<'\n';
i+=k-1;
}
else
cout<<a[i]<<'\n';
}
return 0;
}