跳到正文

题解

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

Splay_trees约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年2月13日
收录
2026年10月15日
更新
2026年11月9日
篇幅
约 10 分钟
获赞
6
题目
P8293

版本

  1. v1当前

题意简述

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

  • 交换形如 p(A)(B)q\texttt{p(A)(B)q} 的串中 A\texttt{A} 和 B\texttt{B} 之间的两个括号,变换为 p(A()B)q\texttt{p(A()B)q},代价为 xwA+ywBxw_A+yw_B,其中 wAw_A 和 wBw_B 分别是 AA 和 BB 左侧左括号的权值。
  • 交换两个相邻合法括号串,没有代价。

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

建模

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

  • 操作 11 相当于花费 xwA+ywBxw_A+yw_B 的代价,将 BB 的所有儿子和 BB 连向父亲的边放到 AA 的下方。但是 AA 和 BB 必须是 DFS 序上相邻的。
  • 操作 22 相当于交换两个儿子,这去掉了操作 11 的限制。
  • 目标是将树变成链。

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

分析

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

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

x≤yx\leq y 情形

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

x=1,y=0x=1,y=0 情形

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

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

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

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

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

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

实现

可以使用 multiset 配合邻接表方便维护每一层的边以及求最值。

细节:当前层只剩一条边的时候需要把它删掉。

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

原文
存档评论8 条抓取于