题解:P8293 [省选联考 2022] 序列变换

· · 题解

题意简述

给定一个有 n 对括号的括号串和 x,y\in \{0,1\},每个左括号附带有权值。可以进行如下两种操作:

现要求将原串变成 n 层括号嵌套的形式,求出最小代价。

建模

直接操作似乎没有好的性质,所以转移到括号树上处理。将左括号看成边,设 A 的最左括号权值为 w_A,B 的最左括号权值为 w_B。则:

我们可以把这些括号看作边,下文统一用“边”表述。由于只能在同一深度的边之间操作,而在不同深度之间跳跃操作和一次操作完同一深度是可以等效的,于是,将相同深度的边一起处理。

分析

从单边入手不好确定每个点的贡献,可以从层的贡献来分析。

设当前处理的一层边共有 t 条,并且令 u 为留在这一层的那条边。其它边如果要下放到下一层,需要经过一次操作,贡献是 \sum\limits_{i\neq u}w_i。用于将这些边下放的边 x 显然越小越好,所以我们独立出当前层权值最小的边 v。将除了 v 和 u 之外的所有边接到 v 的子树中,总代价是 y\sum\limits_{i\notin\{u,v\}}w_i+(t-2)xw_v,加上将 v 接在 u 的子树上的贡献,总代价即为 y\sum\limits_{i\neq u}w_i+(t-2)xw_v+xw_u。

x\leq y 情形

变形原式为 y\sum w_i+(t-2)xw_v+(x-y)w_u。由此可知,当 x-y\leq 0 时令 w_u 尽可能大一定不劣,所以每一层的 u 选择边权最大值即可。

x=1,y=0 情形

此时原式退化为 (t-2)w_v+w_u。

观察式子的直观意义,除了链的最后一层,每条边都会因为在某层用于中转产生 w_u 的贡献,所以我们尽量把权值最大的边留在最后一层。

根据操作 1 的意义,向下操作的过程中,边数 t 应该先单调不降,达到某个峰值后每次单调减 1。

如果当前层的边数 t\geq 3,我们可以令任意一条非当前层最小最大的边留在当前层,从而将当前最小值和最大值一并下传,然后在最后一次操作留下最小值,将最大值下传至最后一层不计贡献,这样答案显然不劣。

由于 t=1 无需决策,现在只需考虑开头连续的一段 t=2 的情况。此时式子进一步退化为 w_u,所以实际上这一段对后续带来的影响只取决于最后下传到 t=3 的那条边。根据我们之前的分析,这条边只有影响后续边集的最小值或最大值,才会对答案造成影响。所以我们在 t=2 区域内按照留最小和留最大两种方案分别决策,计算答案,取其中较小者即可。

时间复杂度 O(n \log n)。

实现

可以使用 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 进行了空格使用规范性审核。