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 = ,
b = ,
c = s.length();
for (int i = ; i < s.length(); i++)
{
switch (s[i])
{
case '(':
a++;
break;
case ')':
if (a > )
{
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{};
cin >> n;
int A[], B[];
memset(A, 0xf0, sizeof(A));
memset(B, 0xf0, sizeof(B));
A[] = ;
B[] = ;
heap<triple, greater<triple>> I;
heap<triple, less<triple>> D;
for (int i = ; i <= n; i++)
{
string s;
cin >> s;
int a{}, b{}, c{};
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 = ; 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 = ; x >= max(a, b - a); x--)
{
B[x] = max(B[x], B[x - b + a] + c);
}
D.pop();
}
int reponse{};
for (int x = ; x <= ; x++)
{
reponse = max(reponse, A[x] + B[x]);
}
cout << reponse << endl;
return ;
}

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

  1. North American Invitational Programming Contest (NAIPC) 2017

    (待补) A. Pieces of Parentheses 将括号处理完成后排序,方式参加下面的博客.然后做一遍背包即可. 2018 Multi-University Training Contest ...

  2. The North American Invitational Programming Contest 2017 题目

    NAIPC 2017 Yin and Yang Stones 75.39% 1000ms 262144K   A mysterious circular arrangement of black st ...

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

    A. Pieces of Parentheses 将括号串排序,先处理会使左括号数增加的串,这里面先处理减少的值少的串:再处理会使左括号数减少的串,这里面先处理差值较大的串.确定顺序之后就可以DP了. ...

  4. 干货云集 WOT 2017全球架构与运维技术峰会揭密技术难点

    WOT,World Of Tech专注互联网IT技术领域,是一场不容错过的技术盛会!WOT 2017全球架构与运维技术峰会三大章节,15大技术专场,60+国内外一线互联网精英大咖站台,打造兼顾技术视野 ...

  5. python 错误之SyntaxError: Missing parentheses in call to 'print'

    SyntaxError: Missing parentheses in call to 'print' 由于python的版本差异,造成的错误. python2: print "hello ...

  6. SyntaxError: Missing parentheses in call to 'print'

    C:\Users\konglb>python Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (I ...

  7. 【PYTHON】 Missing parentheses in call to 'print'

    Microsoft Windows [版本 10.0.15063] (c) 2017 Microsoft Corporation.保留所有权利. C:\Users\Jam>python Pyth ...

  8. ICCV 2017论文分析(文本分析)标题词频分析 这算不算大数据 第一步:数据清洗(删除作者和无用的页码)

    IEEE International Conference on Computer Vision, ICCV 2017, Venice, Italy, October 22-29, 2017. IEE ...

  9. 2017 Multi-University Training 2 解题报告

    Is Derek lying? Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

随机推荐

  1. discuz的css处理机制

    common/common.css 是一个通用的css文件. common/module.css 是某个功能模块使用的css文件.   module.css中,利用特殊语法: /** 标识 **/ c ...

  2. discuz回贴通知插件实现-插件后台管理配置

    1.登出discuz后台,再次设计插件 2.使用变量    

  3. Spring框架的AOP的底层实现

    1. Srping框架的AOP技术底层也是采用的代理技术,代理的方式提供了两种 1. 基于JDK的动态代理 * 必须是面向接口的,只有实现了具体接口的类才能生成代理对象 2. 基于CGLIB动态代理 ...

  4. CookiesHelper

    /// <summary> ///CookiesHelper 的摘要说明 /// </summary> public class CookiesHelper { public ...

  5. PAT 1015 德才论 (25)(代码+思路)

    1015 德才论 (25)(25 分)提问 宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子, ...

  6. vue的过滤器

    Vue.Js 提供了强大的过滤器API,能够对数据进行各种过滤处理,返回需要的结果 vue的过滤器一般在JavaScript 表达式的尾部,由“|”符号指示: 过滤器可以让我们的代码更加优美,一般可以 ...

  7. 构造函数constructor 与析构函数destructor(二)

    (1)转换构造函数 转换构造函数的定义:转换构造函数就是把普通的内置类型转换成类类型的构造函数,这种构造函数只有一个参数.只含有一个参数的构造函数,可以作为两种构造函数,一种是普通构造函数用于初始化对 ...

  8. 如何配置JDK?

    有很多人,java都下载好,却因不会配置JDK,而无法编程.今天巩固就来教大家配置JDK.​ 第一步:将下载好的java放在D盘(最好不要占用C盘).​​​ 第二步:右击我的电脑,选择"属性 ...

  9. 2018.06.30 BZOJ4765: 普通计算姬(dfs序+分块+树状数组)

    4765: 普通计算姬 Time Limit: 30 Sec Memory Limit: 256 MB Description "奋战三星期,造台计算机".小G响应号召,花了三小时 ...

  10. 对MVC模型的自悟,详尽解释,为了更多非计算机人员可以理解

    今天小编在复习之前刚刚学会的一个小项目,然后突然对MVC有了新的理解,决定迅速将其写成文档,否则可能会忘记,就算是一个顿悟,学了java语言好久了,刚刚才对其有了比较深入的理解,希望对于同样的人能够有 ...