题解 P7912 【[CSP-J 2021] 小熊的果篮】
考虑维护每个连续段的长度和颜色,每次枚举连续段并将长度减一。如果长度为
但这种方法不好维护编号。考虑直接使用 pbds 的平衡树暴力维护还剩哪些编号,那每次查第
举个例子,1 1 0 1 1 0 0,我们用
枚举这四个二元组,长度减一,
此时注意到第二个长度已经为
继续长度减一(注意上一次枚举到的
那么怎么知道编号呢?
初始状态编号集合为
当删去
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
struct Q
{
int len,v;
Q(int a,int b):len(a),v(b){}
};
int main()
{
ios::sync_with_stdio(0);cin.tie(0);
int n,i,x;
tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> s;
cin>>n;
for (i=1;i<=n;i++) s.insert(i);
vector<Q> a;
for (i=1;i<=n;i++)
{
cin>>x;
if (!a.size()||a.back().v!=x) a.emplace_back(1,x); else ++a.back().len;
}
while (a.size())
{
vector<Q> b;
int t=0;
for (auto &[x,y]:a)
{
int ans=*s.find_by_order(t);
cout<<ans<<' ';
s.erase(ans);
if (--x)
{
if (!b.size()||b.back().v!=y) b.emplace_back(x,y); else b.back().len+=x;
}
t+=x;
}
cout<<'\n';
a=b;
}
}