2017 NAIPC A:Pieces of Parentheses
my team solve the problem in the contest with similar idea
this is a more deep analysis
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
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
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
1 - Open Type
(())(( --------> is ((
((()( ---------> is (((
pre >= 0
2 - Closed-Open Type
()))()( -------> is ))(
))))(())())(()(---> is )))))((
pre < 0 && pre != sum
()))()( -------> is ))(
))))(())())(()(---> is )))))((
pre < 0 && pre != sum
3 - Closed Type
)))())---------> is )))))
()()()())))----> is )))
pre < 0 && pre == sum
)))())---------> 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
))()())(((( -----> 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
))()())(( -----> 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
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
(((((
))))(
)))))((((
)
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
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
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 阅读(492) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通