Konjac Konjac or brush the water ... (Don't ask me why I have to write a question about this tea.)
Orz Sea tree: "Consider that each closing parenthesis must have a pair of opening parentheses before it, so the sum is recorded to the current position without the number of pairs of opening parentheses.
If negative, this means that a closing parenthesis must be turned into an opening parenthesis, that is, ans++ and sum+=2 (because a closing parenthesis is missing one more opening parenthesis).
The final plus one-second of the remaining pairs of open parentheses is the answer. "
1 /**************************************************************2 problem:30163 User:rausen4 language:c++5 result:accepted6 time:36 Ms7 memory:804 KB8 ****************************************************************/9 Ten#include <cstdio> One A using namespacestd; - - intans, sum; the - intMain () { - CharCH =GetChar (); - while(ch = ='('|| ch = =')') { + if(ch = ='(') ++sum; - Else--sum; + if(Sum <0) ++ans, sum + =2; ACH =GetChar (); at } -Ans + = sum >>1; -printf"%d\n", ans); - return 0; -}
View Code
BZOJ3016 [Usaco2012 Nov]clumsy cows