CF1097C.Yuhao and a Parenthesis

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.

A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.

Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.

This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it?

有一天,Yuhao 遇到了一个关于判断某些括号序列是否为“正确括号序列”的问题。

括号序列是指由左括号和右括号组成的任意非空序列。若能在该序列中插入字符 "+" 和 "1",从而得到一个合法的算术表达式,则称该括号序列为正确括号序列。例如,序列 "(())()"、"()" 和 "(())(())" 是正确的;而 ")("、"(()" 和 "(())(" 则不是正确的括号序列。

Yuhao 觉得这个问题对他而言过于简单,于是决定加大难度。现在给你若干(不一定正确)的括号序列。你的任务是将其中一些序列两两配对(形成有序对),使得每个括号序列至多出现在一个对中,且每对中两个括号序列按顺序拼接后构成一个正确括号序列。目标是使配对数尽可能多。

不幸的是,这个问题对 Yuhao 来说变得过于困难了。你能帮他解决吗?

输入格式

The first line contains one integer nn (1≤n≤1051 \leq n \leq 10^5) — the number of bracket sequences.

Each of the following nn lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")".

The sum of lengths of all bracket sequences in the input is at most 5⋅1055 \cdot 10^5.

Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter.

第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5)—— 表示括号序列的个数。

接下来的 nn 行,每行包含一个括号序列 —— 一个非空字符串,仅由字符 "(" 和 ")" 组成。

输入中所有括号序列的长度总和不超过 5⋅1055 \cdot 10^5。

注意:某个括号序列可能在输入中多次出现。此时,你可以将每次出现的该序列视为独立副本分别使用。此外,输入中字符串的出现顺序无关紧要。

输出格式

Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement.

输出一个整数——在满足题目所述条件的前提下,最多能组成的对数。

输入输出样例

  • 输入#1

    7
    )())
    )
    ((
    ((
    (
    )
    )

    输出#1

    2
  • 输入#2

    4
    (
    ((
    (((
    (())

    输出#2

    0
  • 输入#3

    2
    (())
    ()

    输出#3

    1

说明/提示

In the first example, it's optimal to construct two pairs: "(( )())" and "( )".

在第一个示例中,最优方案是构造两对:"(( )())" 和 "( )".

输入解题思路,AI测评打分。不知道怎么写?

首页