题解:P8293 [省选联考 2022] 序列变换
Splay_trees · · 题解
题意简述
给定一个有
- 交换形如
\texttt{p(A)(B)q} 的串中\texttt{A} 和\texttt{B} 之间的两个括号,变换为\texttt{p(A()B)q} ,代价为xw_A+yw_B ,其中w_A 和w_B 分别是A 和B 左侧左括号的权值。 - 交换两个相邻合法括号串,没有代价。
现要求将原串变成
建模
直接操作似乎没有好的性质,所以转移到括号树上处理。将左括号看成边,设
- 操作
1 相当于花费xw_A+yw_B 的代价,将B 的所有儿子和B 连向父亲的边放到A 的下方。但是A 和B 必须是 DFS 序上相邻的。 - 操作
2 相当于交换两个儿子,这去掉了操作1 的限制。 - 目标是将树变成链。
我们可以把这些括号看作边,下文统一用“边”表述。由于只能在同一深度的边之间操作,而在不同深度之间跳跃操作和一次操作完同一深度是可以等效的,于是,将相同深度的边一起处理。
分析
从单边入手不好确定每个点的贡献,可以从层的贡献来分析。
设当前处理的一层边共有
x\leq y 情形
变形原式为
x=1,y=0 情形
此时原式退化为
观察式子的直观意义,除了链的最后一层,每条边都会因为在某层用于中转产生
根据操作
如果当前层的边数
由于
时间复杂度
实现
可以使用 multiset 配合邻接表方便维护每一层的边以及求最值。
细节:当前层只剩一条边的时候需要把它删掉。
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5+5;
typedef long long ll;
int n,x,y,w[N];
string s;
vector<int> e[N];
void workdep() // 将括号按深度分层
{
int cnt=0,tot=0;
for(int i=0;i<n*2;i++)
{
if(s[i]=='(')
{
cnt++;
e[cnt].emplace_back(++tot);
}
else
cnt--;
}
}
void greedy() // x <= y 的逐层贪心
{
ll cost = 0, sum = 0;
multiset<int> current;
for(int i=1;i<=n;i++)
{
for(auto j:e[i])
{
current.insert(w[j]);
sum += w[j];
}
int minn = *current.begin(), maxx= *prev(current.end());
if(current.size()>1)
cost += 1ll*(x-y)*maxx+x*(current.size()-2)*minn+y*sum;
sum -= maxx;
current.erase(prev(current.end()));
}
while(current.size()>1)
{
int minn = *current.begin(), maxx= *prev(current.end());
cost += 1ll*(x-y)*maxx+x*(current.size()-2)*minn+y*sum;
sum -= maxx;
current.erase(prev(current.end()));
}
cout<<cost<<'\n';
}
ll mincost(multiset<int> current,int dep) // x=1,y=0 在 |current| >= 3 及其后续的处理
{
ll cost = 0, sum = 0;
bool flag = 0;
for(int i=dep;i<=n;i++)
{
for(auto j:e[i])
{
current.insert(w[j]);
sum += w[j];
}
if(current.size()>=3)
{
flag=1;
int sec = *next(current.begin());
cost += (current.size()-2)*(*current.begin())+sec;
current.erase(current.find(sec));
sum -= sec;
}
else if(current.size()==2)
{
if(flag==1)
{
cost += *current.begin();
current.erase(current.begin());
break;
}
}
else
{
sum -= *current.begin();
current.erase(current.begin());
}
}
while(current.size()>1)
{
int minn = *current.begin();
if(current.size()>=3)
{
int sec = *next(current.begin());
cost += (current.size()-2)*minn+sec;
current.erase(current.find(sec));
sum -= sec;
}
else
{
cost += minn;
current.erase(current.begin());
break;
}
}
return cost;
}
void wise_greedy() // x=1,y=0 在 |current| <= 2 的开头一段的处理
{
multiset<int> small,big;
ll cost1=0,cost2=0;
for(int i=1;i<=n;i++)
{
if(small.size()+e[i].size()>=3)
{
cout<<min(cost1+mincost(small,i),cost2+mincost(big,i))<<'\n';
return;
}
for(auto j:e[i])
{
small.insert(w[j]);
big.insert(w[j]);
}
if(small.size()>1)
cost1+=*prev(small.end());
if(big.size()>1)
cost2+=*big.begin();
small.erase(prev(small.end()));
big.erase(big.begin());
}
while(small.size()>1)
{
cost1+=*prev(small.end());
small.erase(prev(small.end()));
}
while(big.size()>1)
{
cost2+=*big.begin();
big.erase(big.begin());
}
cout<<min(cost1,cost2)<<'\n';
return;
}
int main()
{
cin>>n>>x>>y>>s;
for(int i=1;i<=n;i++)
cin>>w[i];
workdep();
if(x<=y)
greedy();
else
wise_greedy();
return 0;
}
本题解在写作完成后使用 DeepSeek 进行了空格使用规范性审核。