题意简述
给定一个有 对括号的括号串和 ,每个左括号附带有权值。可以进行如下两种操作:
- 交换形如 的串中 和 之间的两个括号,变换为 ,代价为 ,其中 和 分别是 和 左侧左括号的权值。
- 交换两个相邻合法括号串,没有代价。
现要求将原串变成 层括号嵌套的形式,求出最小代价。
建模
直接操作似乎没有好的性质,所以转移到括号树上处理。将左括号看成边,设 的最左括号权值为 , 的最左括号权值为 。则:
- 操作 相当于花费 的代价,将 的所有儿子和 连向父亲的边放到 的下方。但是 和 必须是 DFS 序上相邻的。
- 操作 相当于交换两个儿子,这去掉了操作 的限制。
- 目标是将树变成链。
我们可以把这些括号看作边,下文统一用“边”表述。由于只能在同一深度的边之间操作,而在不同深度之间跳跃操作和一次操作完同一深度是可以等效的,于是,将相同深度的边一起处理。
分析
从单边入手不好确定每个点的贡献,可以从层的贡献来分析。
设当前处理的一层边共有 条,并且令 为留在这一层的那条边。其它边如果要下放到下一层,需要经过一次操作,贡献是 。用于将这些边下放的边 显然越小越好,所以我们独立出当前层权值最小的边 。将除了 和 之外的所有边接到 的子树中,总代价是 ,加上将 接在 的子树上的贡献,总代价即为 。
情形
变形原式为 。由此可知,当 时令 尽可能大一定不劣,所以每一层的 选择边权最大值即可。
情形
此时原式退化为 。
观察式子的直观意义,除了链的最后一层,每条边都会因为在某层用于中转产生 的贡献,所以我们尽量把权值最大的边留在最后一层。
根据操作 的意义,向下操作的过程中,边数 应该先单调不降,达到某个峰值后每次单调减 。
如果当前层的边数 ,我们可以令任意一条非当前层最小最大的边留在当前层,从而将当前最小值和最大值一并下传,然后在最后一次操作留下最小值,将最大值下传至最后一层不计贡献,这样答案显然不劣。
由于 无需决策,现在只需考虑开头连续的一段 的情况。此时式子进一步退化为 ,所以实际上这一段对后续带来的影响只取决于最后下传到 的那条边。根据我们之前的分析,这条边只有影响后续边集的最小值或最大值,才会对答案造成影响。所以我们在 区域内按照留最小和留最大两种方案分别决策,计算答案,取其中较小者即可。
时间复杂度 。
实现
可以使用 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 进行了空格使用规范性审核。