Round B APAC Test 2017 Problem A. Sherlock and Parentheses
2016-10-07 21:51
621 查看
Problem A. 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 stringSconsisting
only of characters
(and/or
)is balanced if:
It is the empty string, or:
It has the form
(S
), where S is a balanced string, or:
It has the form S1S2, where S1 is a balanced string and S2 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 S of L
+ R characters, in which there are a total of L left parentheses
(and
a total ofR 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 or end at different indexes of the string, even if their content happens to be the same). Note that S
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?
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of one line with two integers: L and R.Output
For each test case, output one line containingCase #x: y, where
xis
the test case number (starting from 1) and
yis the answer, as described above.
Limits
1 ≤ T ≤ 100.Small dataset
0 ≤ L ≤ 20.0 ≤ R ≤ 20.
1 ≤ L + R ≤ 20.
Large dataset
0 ≤ L ≤ 105.0 ≤ R ≤ 105.
1 ≤ L + R ≤ 105.
Sample
Input | Output |
3 1 0 1 1 3 2 | Case #1: 0 Case #2: 1 Case #3: 3 |
(. There are no
balanced non-empty substrings.
In Case 2, the optimal string is
(). There is only one
balanced non-empty substring: the entire string itself.
In Case 3, both strings
()()(and
(()()give
the same optimal answer.
For the case
()()(, for example, the three balanced substrings
are
()from indexes 1 to 2,
()from
indexes 3 to 4, and
()()from indexes 1 to 4.
Solution
#include <iostream>#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <list>
#include <iterator>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef long long ll;
int main(int argc, char* argv[])
{
#ifndef TEST
if (argc != 2)
{
cout << "Invalid input" << endl;
return 1;
}
string input = argv[1];
string output = input.substr(0, input.length() - 2) + "out";
freopen(input.c_str(), "r", stdin);
freopen(output.c_str(), "w", stdout);
#endif
int T, N;
int L, R;
cin >> T;
for (int i = 1; i <= T; i++)
{
cin >> L >> R;
ll res = min(L, R);
if (res > 1)
res = res * (res + 1) / 2;
cout << "Case #" << i << ": " << res << endl;
}
fclose(stdin);
fclose(stdout);
return 0;
}
Reference
https://code.google.com/codejam/contest/5254487/dashboard相关文章推荐
- Problem A. Sherlock and Parentheses Google APAC 2017 University Test Round B
- Round B APAC Test 2017 Problem D. Sherlock and Permutation Sorting
- Kickstart Practice Round 2017 Problem C. Sherlock and Parentheses
- Round B APAC Test 2017 Problem B. Sherlock and Watson Gym Secrets
- Problem B. Sherlock and Watson Gym Secrets Google APAC 2017 University Test Round B
- Round B APAC Test 2017 Problem C. Watson and Intervals
- Google APAC test 2015 Round B Problem D-Parentheses Order
- Problem C. Watson and Intervals Google APAC 2017 University Test Round B
- Round D APAC Test 2017 Problem D. Stretch Rope (C++)
- Problem A. Vote Google APAC 2017 University Test Round D
- Problem A. Diwali lightings Google APAC 2017 University Test Round E
- Round C APAC Test 2017 Problem C. Evaluation
- Problem A. Country Leader Google APAC 2017 University Test Round A
- Practice Round APAC test 2017——Problem C. Not So Random
- Practice Round APAC test 2017——Problem B. Robot Rock Band
- Problem C. Codejamon Cipher Google APAC 2017 University Test Round D
- Round D APAC Test 2017--Problem C. Codejamon Cipher(dp)
- Problem A. Monster Path Google APAC 2017 University Test Round C
- Problem B. Beautiful Numbers Google APAC 2017 University Test Round E
- Round C APAC Test 2017 Problem D. Soldiers