I'm trying to solve atcoder ABC167F. https://atcoder.jp/contests/abc167/tasks/abc167_f .
I started by questioning what conditions decide whether a concatenation is valid. We start by defining sum of a bracketed sequence, which is count('(')-count(')')
. I then found the sum of every string, and the smallest sum of a prefix of the string. They are denoted by $$$sum$$$ and $$$mnprefix$$$ in my code. If my current string is valid which is equivalent to $$$mnprefix\ge 0$$$ and has a sum of $$$x$$$, then a concatenation is valid if and only if $$$x+mnprefix\ge 0$$$ and our new sequnce has a sum of $$$x+sum$$$. Let's say we have two strings $$$a$$$ and $$$b$$$. Let $$$a.sum$$$ denote the sum of $$$a$$$ and the rest of the notation is deductible.
Let's say $$$a$$$ comes before $$$b$$$. Then the two conditions that need to be valid are $$$x\ge a.mnprefix$$$ and $$$x\ge b.mnprefix-a.sum$$$. So we want $$$\max(a.mnprefix, b.mnprefix-a.sum)\le \max(b.mnprefix, a.mnprefix - b.sum)$$$ for $$$a$$$ to be chosen before $$$b$$$. However my proof has one flaw, which is that it doesn't account for transitivity. I don't know how to prove another string $$$c$$$ will not affect my result, and how do I know my comparator function is transitive, as each string doesn't have an exact value, and the comparator values are differing according to the string being compared to.
If my solution is correct, can someone tell me if I have made an error in my implementation.
Edit : nvm, I was using abs(mnprefix)
in my calculation but not in my code.
Auto comment: topic has been updated by Everule (previous revision, new revision, compare).
In this problem you have to split input strings into two groups: with positive total balance and with negative total balance. You don't do it.
This comparator (or similar) works for one of these groups. To sort the other group you should look at the string from right to left, considering ')' as open bracket and '(' as close bracket.
My comparator is correct for both groups. I took absolute value of $$$mnprefix$$$ while calculating without noticing.
Here is my submission https://atcoder.jp/contests/abc167/submissions/13255900
I like your proof of transitivity.
Of course your solution is wrong.
It passes 46 / 81 in 101341A - Streets of Working Lanterns - 2
Edit: after 'Edit' it does much better but still wrong :)
Okay thank you. You are correct.
And lifehack how to prove transitivity: