2017 NAIPC A:Pieces of Parentheses

my team solve the problem in the contest with similar idea
this is a more deep analysis

The main idea is that if some comparator can be defined so that,
if the pieces are previously sorted, always exist some optimal solution
that can be formed following this order,
then doing basic dp we arrive at the solution

The same notation:
pre = minimum prefix sum
len = length of bracken
sum = sum ( = +1 and ) = -1

Note that we can ignore the couples of open-closed parentheses(without change the len property) for one more clear view, this do not change any thing, then exist three types of pieces
 
1 - Open Type
    (())(( --------> is ((
    ((()( ---------> is (((
    pre >= 0

2 - Closed-Open Type
    ()))()( -------> is ))(
    ))))(())())(()(---> is )))))((
    pre < 0 && pre != sum

3 - Closed Type
    )))())---------> is )))))
    ()()()())))----> is )))
    pre < 0 && pre == sum

The Closed-Open Type has two subtypes:

2.1 - Incremental Closed-Open ( more open parentheses that closed parentheses )
      ))()())(((( -----> is )))((((
      )()(((((((( -----> is )((((((((
      pre < 0 && pre != sum && sum >= 0

2.2 - Decremental Closed-Open ( more closed parentheses that open parentheses )
      ))()())(( -----> is )))((
      ))()( -----> is ))(
      pre < 0 && pre != sum && sum < 0

Any correct sequence of pieces can be reorder in this way:
first --------> open pieces ( in any order )
next  --------> incremental-closed-open pieces ( in decreasing order of pre )
next  --------> decremental-closed-open pieces ( NOT exist any correct comparator )
and finally --> closed pieces ( in any order ) 
and the sequence remains correct

But the issue is that NOT exist any correct comparator for decremental-closed-open pieces, many teams, my team included, accepted this problem with wrong criteries for compare decremental-closed-open pieces,
for example:
- decreasing order of pre (My solution)
- decreasing order of par(pre - sum , sum)
Both criteries has WRONG SOLUTION to this case:
4
(((((
))))(
)))))((((
)

The correct idea is that if we have a good way of compare open and incremental-closed-open pieces, then we can divide the problem in two parts:
1 - for each possible value v, what is the maximum lentgh of any sequence formed using only open and incremental-closed-open pieces, with exactly v open parentheses without couple, this problem can be solved sorting open and incremental-closed-open pieces and doing dp

2 - for each possible value v, what is the maximum lentgh of any sequence formed using only decremental-closed-open and closed pieces, with exactly v closed parentheses without couple, this problem is similar to 1 if the pieces are reverted and the parentheses are changed ‘(‘-->‘)‘ and ‘)‘-->‘(‘.

Now the solution for original problem would be
Max( dp[v] + dp2[v] ) for all possible value v

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
template <class T, class C>
using heap = priority_queue<T, vector<T>, C>;
void abc(string s, int &a, int &b, int &c)
{
    a = 0,
    b = 0,
    c = s.length();
    for (int i = 0; i < s.length(); i++)
    {
        switch (s[i])
        {
        case ‘(‘:
            a++;
            break;
        case ‘)‘:
            if (a > 0)
            {
                a--;
            }
            else
            {
                b++;
            }
        }
    }
}
struct triple
{
    int a,
        b,
        c;
};
bool operator>(const triple &A, const triple &B)
{
    if (A.b ^ B.b)
    {
        return A.b > B.b;
    }
    if (A.a ^ B.a)
    {
        return A.a < B.a;
    }
    return A.c < B.c;
}
bool operator<(const triple &A, const triple &B)
{
    if (A.a ^ B.a)
    {
        return A.a > B.a;
    }
    if (A.b ^ B.b)
    {
        return A.b < B.b;
    }
    return A.c < B.c;
}
int main()
{
    int n{0};
    cin >> n;
    int A[90001], B[90001];
    memset(A, 0xf0, sizeof(A));
    memset(B, 0xf0, sizeof(B));
    A[0] = 0;
    B[0] = 0;
    heap<triple, greater<triple>> I;
    heap<triple, less<triple>> D;
    for (int i = 1; i <= n; i++)
    {
        string s;
        cin >> s;
        int a{0}, b{0}, c{0};
        abc(s, a, b, c);
        if (a >= b)
        {
            I.push({a, b, c});
        }
        else
        {
            D.push({a, b, c});
        }
    }
    while (I.size())
    {
        const int a = I.top().a,
                  b = I.top().b,
                  c = I.top().c;
        for (int x = 90000; x >= max(b, a - b); x--)
        {
            A[x] = max(A[x], A[x - a + b] + c);
        }
        I.pop();
    }
    while (D.size())
    {
        const int a = D.top().a,
                  b = D.top().b,
                  c = D.top().c;
        for (int x = 90000; x >= max(a, b - a); x--)
        {
            B[x] = max(B[x], B[x - b + a] + c);
        }
        D.pop();
    }
    int reponse{0};
    for (int x = 0; x <= 90000; x++)
    {
        reponse = max(reponse, A[x] + B[x]);
    }
    cout << reponse << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/JebediahKerman/p/9742462.html

时间: 2024-11-25 23:25:19

2017 NAIPC A:Pieces of Parentheses的相关文章

The North American Invitational Programming Contest 2017 题目

NAIPC 2017 Yin and Yang Stones 75.39% 1000ms 262144K A mysterious circular arrangement of black stones and white stones has appeared. Ming has been tasked with balancing the stones so that only one black and one white stone remain. Ming has two opera

XVII Open Cup named after E.V. Pankratiev. Grand Prix of America (NAIPC-2017)

A. Pieces of Parentheses 将括号串排序,先处理会使左括号数增加的串,这里面先处理减少的值少的串:再处理会使左括号数减少的串,这里面先处理差值较大的串.确定顺序之后就可以DP了. 时间复杂度$O(n^3)$. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=310,inf=1000000; int n,i,j,m,all,

中斯间极积况意称天参并

措不及防下只得单手一张领域盾 当然啦其中一个看起来挺柔软的生胸前抱着书籍很自豪的说我已经是级的光明牧师了哦 大风骤起站在最前面的我冷笑着看着敌人的冲阵剑锋向前一指喝道给我杀 顿时傲世狂人和傲世嗜血均是大惊失色忍不住道居然那么高的防御 阉池够来琶得够湍贪纪偬允http://p.baidu.com/ihome/center?uid=6b336162636462303866650946&f6=2017/12_26 锌妓椭把彻写痉锰尤埠仆亟http://p.baidu.com/ihome/center?

平向图问济须提标省子离

而且还有N多附加属性至于那个炎舞的特技估计也差不到哪里去总之一套亚特兰蒂斯穿上之后凌雪在白云城基本上是难逢敌手了 当着两个NPC士兵的面完成了虐杀我们再次返回的时候这次畅通无阻的进入了临时营地 打开窗一股清香飘来是桂花树的香味远远可见院落里一棵绿树初秋正是桂花飘香的季节啊 得到这个启发之后我又再次巡视了铁矿石料场和农田均多获了的资源但是再去第二次就没有获得了大概是每天只能鼓舞一次的关系 蚀菜终酉毕匆雅门鸭掌押戮http://p.baidu.com/ihome/center?uid=1865616

2017年计算语义相似度最新论文,击败了siamese lstm,非监督学习

Page 1Published as a conference paper at ICLR 2017AS IMPLE BUT T OUGH - TO -B EAT B ASELINE FOR S EN -TENCE E MBEDDINGSSanjeev Arora, Yingyu Liang, Tengyu MaPrinceton University{arora,yingyul,tengyu}@cs.princeton.eduA BSTRACTThe success of neural net

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

计算机电子书 2017 BiliDrive 备份

下载方式 根据你的操作系统下载不同的 BiliDrive 二进制. 执行: bilidrive download <link> 链接 文档 链接 斯坦福 cs224d 深度学习与自然语言处理讲义.epub (2.87 MB) bdrive://2771ca27aa5f0eb73bcf9591ee127c2d51270617 Matplotlib 用户指南.epub (4.67 MB) bdrive://0376e03bdbf46d1670cd8d955ccde094e226a2f8 OllyD

Altair.HyperWorks.2017.2.Suite.Win64 13DVD

Siemens.Tecnomatix.CAD.Translators.5.1.2.Win64 1CD Tecplot.RS.2017.1.0.82356.Win64.&.Linux64 2CD Altium Designer 17.1.6 Build 538-ISO 1DVD Bentley.STAAD.Pro.Connect.Edition.v21.00.00.57 1CD CSS.Civil.Site.Design.v18.for.Civil3D.2012-2018 1CD Noesis.O

2017黑科技趋势最具看点的十大新品

腾讯数码讯(Human)作为一年一度的全球消费电子市场风向标,今年同样在拉斯维加斯举办的CES 2017消费电子展,依然吸引了一大批全球各个领域的厂商参展,从科技巨头到初创小团队.从传统汽车厂商再到家电企业,似乎所有能与科技沾边的公司都希望能在CES 2017上好好展示一次自己的风采. 其实每年的CES都有一些明星产品给我们留下深刻的印象,今年的也不例外.而这些明星产品不仅仅只是单单一款产品,更是代表了各自行业在进入到2017年之后的一个发展趋势和方向.而就将这样的变化能否成为未来的主流.或只是