2017 NAIPC A:Pieces of Parentheses
this is a more deep analysis
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
pre = minimum prefix sum
len = length of bracken
sum = sum ( = +1 and ) = -1
1 - Open Type
(())(( --------> is ((
((()( ---------> is (((
pre >= 0
()))()( -------> is ))(
))))(())())(()(---> is )))))((
pre < 0 && pre != sum
)))())---------> is )))))
()()()())))----> is )))
pre < 0 && pre == sum
))()())(((( -----> is )))((((
)()(((((((( -----> is )((((((((
pre < 0 && pre != sum && sum >= 0
))()())(( -----> is )))((
))()( -----> is ))(
pre < 0 && pre != sum && sum < 0
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
for example:
- decreasing order of pre (My solution)
- decreasing order of par(pre - sum , sum)
Both criteries has WRONG SOLUTION to this case:
4
(((((
))))(
)))))((((
)
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
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的更多相关文章
- North American Invitational Programming Contest (NAIPC) 2017
(待补) A. Pieces of Parentheses 将括号处理完成后排序,方式参加下面的博客.然后做一遍背包即可. 2018 Multi-University Training Contest ...
- The North American Invitational Programming Contest 2017 题目
NAIPC 2017 Yin and Yang Stones 75.39% 1000ms 262144K A mysterious circular arrangement of black st ...
- XVII Open Cup named after E.V. Pankratiev. Grand Prix of America (NAIPC-2017)
A. Pieces of Parentheses 将括号串排序,先处理会使左括号数增加的串,这里面先处理减少的值少的串:再处理会使左括号数减少的串,这里面先处理差值较大的串.确定顺序之后就可以DP了. ...
- 干货云集 WOT 2017全球架构与运维技术峰会揭密技术难点
WOT,World Of Tech专注互联网IT技术领域,是一场不容错过的技术盛会!WOT 2017全球架构与运维技术峰会三大章节,15大技术专场,60+国内外一线互联网精英大咖站台,打造兼顾技术视野 ...
- python 错误之SyntaxError: Missing parentheses in call to 'print'
SyntaxError: Missing parentheses in call to 'print' 由于python的版本差异,造成的错误. python2: print "hello ...
- 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 ...
- 【PYTHON】 Missing parentheses in call to 'print'
Microsoft Windows [版本 10.0.15063] (c) 2017 Microsoft Corporation.保留所有权利. C:\Users\Jam>python Pyth ...
- ICCV 2017论文分析(文本分析)标题词频分析 这算不算大数据 第一步:数据清洗(删除作者和无用的页码)
IEEE International Conference on Computer Vision, ICCV 2017, Venice, Italy, October 22-29, 2017. IEE ...
- 2017 Multi-University Training 2 解题报告
Is Derek lying? Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
随机推荐
- discuz的css处理机制
common/common.css 是一个通用的css文件. common/module.css 是某个功能模块使用的css文件. module.css中,利用特殊语法: /** 标识 **/ c ...
- 【SQL模板】四.插入/更新 列模板TSQL
---Name: 插入/更新 列模板.sql ---Purpose: 用于更新 数据库中 列 的脚本模板 ---Author: xx ---Time: 2015-12-18 10:26:06 ---R ...
- PythonQt在windows下的编译
笔者最近在做Qt方面的开发工作,然后需要用到脚本程序对程序内部进行扩展,就很自然的想到了Python,度娘一下发现了一款神器,也就是今天给大家介绍的主角:PythonQt 今天首先给大家介绍下Pyth ...
- 《计算机网络》谢希仁(第7版) 第四章 c语言http://c.biancheng.net/cpp/html/3137.html
第四章 网络层 电信网使用面向连接的通信方式,使电信网络能够向用户提供可靠传输的服务. 互联网设计思路:网络层向上只提供简单灵活的.无连接的.尽最大努力交付的数据报(分组)服务. 网络层不提供可靠传输 ...
- 面向对象设计模式纵横谈:Prototype 原型模式(笔记记录)
有一段时间没写东西了,今天继续把没写完的设计模式写完,今天这堂课是创建型设计模式的最后一堂课,原型设计模式,它同样也是解决了对象在创建的过程中的解耦合的情况,面对变化使代码更稳定,更准确的说是使 ...
- WinScp获取一个文件
CD /d C:\Program Files (x86)\WinSCPWinSCP.exe /console /command "option batch continue" &q ...
- Laravel5.4 Oauth2.0认证应用 API 实战!
项目初始化 新建项目 lukeyans-MacBook-Pro:laravel lukeyan$ laravel new laravel_demo 添加laravel自带的Passport服务 luk ...
- jquery validate 之多tab页同时校验问题
1.设置多tab页同时校验: $("form").validate({ignore: ":hidden", ignore: ""}); 由于 ...
- 2018.08.11 洛谷P3224 [HNOI2012]永无乡(线段树合并)
传送门 给出n个带点权的点,支持连边和查询连通块第k大. 这个貌似就是一道线段树合并的裸板啊... 代码: #include<bits/stdc++.h> #define N 100005 ...
- Fortran 数据类型