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;
}

 

posted on 2018-10-04 16:13  JebediahKerman  阅读(482)  评论(0编辑  收藏  举报

导航