#P16580. [GKS 2016 #B] Sherlock and Parentheses

    ID: 16553 Type: RemoteJudge 1000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 3 Uploaded By: Tags>数学2016Google Kick Start

[GKS 2016 #B] Sherlock and Parentheses

题目描述

Sherlock and Watson have recently enrolled in a computer programming course. Today, the tutor taught them about the balanced parentheses problem. A string SS consisting only of characters ( and/or ) is balanced if:

  • It is the empty string, or:
  • It has the form (S)(S), where SS is a balanced string, or:
  • It has the form S1S2S_1 S_2, where S1S_1 is a balanced string and S2S_2 is a balanced string.

Sherlock coded up the solution very quickly and started bragging about how good he is, so Watson gave him a problem to test his knowledge. He asked Sherlock to generate a string SS of L+RL + R characters, in which there are a total of LL left parentheses ( and a total of RR right parentheses ). Moreover, the string must have as many different balanced non-empty substrings as possible. (Two substrings are considered different as long as they start and end at different indexes of the string, even if their content happens to be the same). Note that SS itself does not have to be balanced.

Sherlock is sure that once he knows the maximum possible number of balanced non-empty substrings, he will be able to solve the problem. Can you help him find that maximum number?

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case consists of one line with two integers: LL and RR.

输出格式

For each test case, output one line containing Case #xx: yy, where xx is the test case number (starting from 11) and yy is the answer, as described above.

3
1 0
1 1
3 2
Case #1: 0
Case #2: 1
Case #3: 3

提示

In Case 11, the only possible string is ( . There are no balanced non-empty substrings.

In Case 22, the optimal string is () . There is only one balanced non-empty substring: the entire string itself.

In Case 33, both strings ()() and (()() give the same optimal answer.

For the case ()()(, for example, the three balanced substrings are () from indexes 11 to 22, () from indexes 33 to 44, and ()() from indexes 11 to 44.

Limits

1T1001 \le T \le 100.

Small dataset (Test set 1 - Visible)

0L200 \le L \le 20.

0R200 \le R \le 20.

1L+R201 \le L + R \le 20.

Large dataset (Test set 2 - Hidden)

0L1050 \le L \le 10^5.

0R1050 \le R \le 10^5.

1L+R1051 \le L + R \le 10^5.