题解:P12912 [POI 2020/2021 R2] 收拾背包 / Pakowanie plecaka
Solution
令
令初始承重为
容易发现
由此推出
题目要求出
在取走对应的
-
对于
j\in[1,i-1] 的s_n-s_j-a_j 降低a_i ,满足s_n-s_i-a_i<0 的决策集合扩大。 -
题意转化完毕,现在考虑怎么维护这个东西。
对于
删除操作则可以考虑使用平衡树或者线段树维护
若存在,则将此位置加入堆中,并将此位置的
每个数最多加入堆
总体时间复杂度
Code
#include<bits/stdc++.h>
#define int long long
#define INF 1e18
#define N 1000005
using namespace std;
struct tree{
int lz,v,vp;
}t[N<<1];
struct node{
int val,id;
bool operator<(const node& a)const{
return val<a.val;
}
};
int n,a[N],s[N],ans[N],tot;
priority_queue<node>q;
void push_up(int p){
if(t[p<<1].v<t[p<<1|1].v) t[p].v=t[p<<1].v,t[p].vp=t[p<<1].vp;
else t[p].v=t[p<<1|1].v,t[p].vp=t[p<<1|1].vp;
}
void push_down(int p){
t[p<<1].v+=t[p].lz,t[p<<1].lz+=t[p].lz;
t[p<<1|1].v+=t[p].lz,t[p<<1|1].lz+=t[p].lz;
t[p].lz=0;
}
void add(int p,int l,int r,int L,int R,int v){
if(L<=l&&r<=R){
t[p].lz+=v,t[p].v+=v;
if(l==r) t[p].vp=l;
return;
}
push_down(p);
int mid=l+r>>1;
if(L<=mid) add(p<<1,l,mid,L,R,v);
if(R>mid) add(p<<1|1,mid+1,r,L,R,v);
push_up(p);
}
tree query(int p,int l,int r,int L,int R){
if(L<=l&&r<=R) return t[p];
push_down(p);
int mid=l+r>>1; tree ls,rs,res;
if(L<=mid) ls=query(p<<1,l,mid,L,R);
if(R>mid) rs=query(p<<1|1,mid+1,r,L,R);
if(L>mid) return rs;
if(R<=mid) return ls;
if(ls.v<rs.v) res.v=ls.v,res.vp=ls.vp;
else res.v=rs.v,res.vp=rs.vp;
return res;
}
signed main(){
ios::sync_with_stdio(false);
cin>>n,tot=n;
for(int i=1;i<=n;i++) cin>>a[i],s[i]=s[i-1]+a[i];
ans[tot]=s[n];
for(int i=1,tmp;i<=n;i++){
tmp=s[n]-s[i]-a[i];
if(tmp<0) q.push(node{a[i],i}),add(1,1,n,i,i,INF);
else add(1,1,n,i,i,tmp);
}
while(tot!=1){
node g=q.top(); q.pop();
tot--,ans[tot]=ans[tot+1]-g.val;
while(true){
tree tmp=query(1,1,n,1,g.id);
if(tmp.v>=g.val) break;
q.push(node{a[tmp.vp],tmp.vp});
add(1,1,n,tmp.vp,tmp.vp,INF);
}
add(1,1,n,1,g.id,-g.val);
}
for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
return 0;
}