Google APAC 2017 University Test Round E
2016-11-06 16:24
387 查看
Problem A. Diwali
lightings
transfinite powers, so she buys an infinite number of red and blue light bulbs. As a programmer, she also loves patterns, so she arranges her lights by infinitely repeating a given finite pattern S.
For example, if S is
Blue is Pari's favorite color, so she wants to know the number of blue bulbs between theIth bulb and Jth bulb, inclusive, in the infinite sequence she built (lights are
numbered with consecutive integers starting from 1). In the sequence above, the indices would be numbered as follows:
So, for example, there are 4 blue lights between the 4th and 8th positions, but only 2 between the 10th and 12th.
Since the sequence can be very long, she wrote a program to do the count for her. Can you do the same?
First line of each test case consists of a string S, denoting the initial finite pattern.
Second line of each test case consists of two space separated integers I and J, defined above.
the test case number (starting from 1) and
1 ≤ length of S ≤ 100.
Each character of S is either uppercase
Cases #1 and #2 are explained above.
In Case #3, bulbs at odd indices are always blue, and bulbs at even indices are always red, so there are half a million blue bulbs between positions 1 and 106.
Problem B. Beautiful
Numbers
it in another base.
Given an integer N, can you find a base B (with B > 1) to write it in such that all of its digits become 1? If there are multiple bases that satisfy this property, choose
the one that maximizes the number of 1 digits.
the test case number (starting from 1) and
In case #1, the optimal solution is to write 3 as 11 in base 2.
In case #2, the optimal solution is to write 13 as 111 in base 3. Note that we could also write 13 as 11 in base 12, but neither of those representations has as many 1s.
Problem C. Partioning
Number
The numbers of balls in the buckets must be in non-decreasing order when read from left to right.
The leftmost bucket must be non-empty and the number of balls in the leftmost bucket must be divisible by D.
The difference (in number of balls) between any two buckets (not just any two adjacent buckets) must be less than or equal to 2.
How many different ways are there for Shekhu to do this? Two ways are considered different if the lists of numbers of balls in buckets, reading left to right, are different.
T test cases follow. Each test case consists of one line with two integers N and D, as described above.
the test case number (starting from 1) and
1 ≤ D ≤ 100.
In sample case #1, the possible distributions are:
1 1 1 1 1 1 1
1 1 1 1 1 2
1 1 1 1 3
1 1 1 2 2
1 2 2 2
1 1 2 3
1 3 3
2 2 3
3 4
7
Note that 1 2 4 is not a valid distribution, since the difference between 1 and 4 is more than 2.
In sample case #2, the possible distributions are:
2 2 3
3 4 is not possible, since the first term is not divisible by 2.
In sample case #3, no possible arrangement exists.
Problem D. Sorting
Array
input order, the algorithm depends on two integers P(which would be at most 3) andK. Here is how the algorithms works:
Partition A into K disjoint non-empty subarrays A1, A2, ..., AK such that concatenating them in order A1A2 ... AK produces
A.
Sort each subarray individually.
Choose up to P of the subarrays, and swap any two of them any number of times.
For example, consider A = [1 5 4 3 2] and P = 2. A possible partition into K = 4 disjoint subarrays is:
We want to show the algorithm is good for distributed environments by finding, for a fixed input and value of P, the maximum number of partitions K such that, choosing
the partitions and swaps wisely, we can achieve a sorting of the original order. Can you help us to calculate that K?
T test cases follow. Each test case consists of two lines. The first line contains two integers N and P, as described above.
The second line of the test case contains N integers X1, X2, ..., XN represting array A.
the test case number (starting from 1) and
1 ≤ N ≤ 5000.
1 ≤ Xi ≤ N, for all i.
Xi ≠ Xj for all i ≠ j.
Case #1:
Same as walk through in the statement.
Case #2:
[4 5] [1 2 3]
Swap the 2 blocks: [1 2 3] [4 5]
Case #3:
[6] [3 5 2 4] [1]
Sort [3 5 2 4], then swap [6] and [1], we get: [1] [2 3 4 5] [6]
Case #4:
[4 5] [1] [2 3]
Swap [4 5] and [1], then swap [2 3] and [4 5]: [1] [2 3] [4 5]
Case #5:
[1] [2] [6] [4] [5] [3]
Swap [6] and [3]: [1] [2] [3] [4] [5] [6]
Note: First 3 sample cases would not appear in the Large dataset and the last 2 sample cases would not appear in the Small dataset.
lightings
Problem
Diwali is the festival of lights. To celebrate it, people decorate their houses with multi-color lights and burst crackers. Everyone loves Diwali, and so does Pari. Pari is very fond of lights, and hastransfinite powers, so she buys an infinite number of red and blue light bulbs. As a programmer, she also loves patterns, so she arranges her lights by infinitely repeating a given finite pattern S.
For example, if S is
BBRB, the infinite sequence Pari builds would be
BBRBBBRBBBRB...
Blue is Pari's favorite color, so she wants to know the number of blue bulbs between theIth bulb and Jth bulb, inclusive, in the infinite sequence she built (lights are
numbered with consecutive integers starting from 1). In the sequence above, the indices would be numbered as follows:
B B R B B B R B B B R B... 1 2 3 4 5 6 7 8 9 10 11 12
So, for example, there are 4 blue lights between the 4th and 8th positions, but only 2 between the 10th and 12th.
Since the sequence can be very long, she wrote a program to do the count for her. Can you do the same?
Input
The first line of the input gives the number of test cases, T. T test cases follow.First line of each test case consists of a string S, denoting the initial finite pattern.
Second line of each test case consists of two space separated integers I and J, defined above.
Output
For each test case, output one line containingCase #x: y, where
xis
the test case number (starting from 1) and
yis number of blue bulbs between the Ith bulb and Jth bulb of Pari's infinite sequence, inclusive.
Limits
1 ≤ T ≤ 100.1 ≤ length of S ≤ 100.
Each character of S is either uppercase
Bor uppercase
R.
Small dataset
1 ≤ I ≤ J ≤ 106.Large dataset
1 ≤ I ≤ J ≤ 1018.Sample
Input | Output |
3 BBRB 4 8 BBRB 10 12 BR 1 1000000 | Case #1: 4 Case #2: 2 Case #3: 500000 |
In Case #3, bulbs at odd indices are always blue, and bulbs at even indices are always red, so there are half a million blue bulbs between positions 1 and 106.
#include <iostream> #include <string> using namespace std; int main() { //freopen("A-small-attempt0.in","r",stdin); //freopen("output_A.txt","w",stdout); int T; cin >> T; int count = 1; while (count <= T) { string s; cin >> s; int len = s.size(); int i; int num[100]; int B = 0; for (i = 0; i < len; i++) { if (s[i] == 'B') B++; num[i+1] = B; } num[0] = num[i-1]; int m, n; cin >> m >> n; int mth = m % len; int nth = n % len; int beishu = (n - m) / len; int result = num[0] * beishu + (num[0] - num[mth]) + num[nth]; cout << "Case #" << count << ": " << result << endl; count++; } return 0; }
Problem B. Beautiful
Numbers
Problem
We consider a number to be beautiful if it consists only of the digit 1 repeated one or more times. Not all numbers are beautiful, but we can make any base 10 positive integer beautiful by writingit in another base.
Given an integer N, can you find a base B (with B > 1) to write it in such that all of its digits become 1? If there are multiple bases that satisfy this property, choose
the one that maximizes the number of 1 digits.
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 an integer N.Output
For each test case, output one line containingCase #x: y, where
xis
the test case number (starting from 1) and
yis the base described in the problem statement.
Limits
1 ≤ T ≤ 100.Small dataset
3 ≤ N ≤ 1000.Large dataset
3 ≤ N ≤ 1018.Sample
Input | Output |
2 3 13 | Case #1: 2 Case #2: 3 |
In case #2, the optimal solution is to write 13 as 111 in base 3. Note that we could also write 13 as 11 in base 12, but neither of those representations has as many 1s.
#include <iostream> using namespace std; int main() { //freopen("B-small-attempt0.in","r",stdin); //freopen("output_B.txt","w",stdout); int T; cin >> T; int count = 1; while (count <= T) { int N; cin >> N; int tmp = N; int i; for (i = 2; tmp > 1;) { if ((tmp % i) != 1) { i++; tmp = N; } else { tmp /= i; } } if (tmp == 1) cout << "Case #" << count << ": " << i << endl; count++; } return 0; }
Problem C. Partioning
Number
Problem
Shekhu has N balls. She wants to distribute them among one or more buckets in a way that satisfies all of these constraints:The numbers of balls in the buckets must be in non-decreasing order when read from left to right.
The leftmost bucket must be non-empty and the number of balls in the leftmost bucket must be divisible by D.
The difference (in number of balls) between any two buckets (not just any two adjacent buckets) must be less than or equal to 2.
How many different ways are there for Shekhu to do this? Two ways are considered different if the lists of numbers of balls in buckets, reading left to right, are different.
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 N and D, as described above.
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.1 ≤ D ≤ 100.
Small dataset
1 ≤ N ≤ 2000.Large dataset
1 ≤ N ≤ 105.Sample
Input | Output |
3 7 1 7 2 2 4 | Case #1: 10 Case #2: 1 Case #3: 0 |
1 1 1 1 1 1 1
1 1 1 1 1 2
1 1 1 1 3
1 1 1 2 2
1 2 2 2
1 1 2 3
1 3 3
2 2 3
3 4
7
Note that 1 2 4 is not a valid distribution, since the difference between 1 and 4 is more than 2.
In sample case #2, the possible distributions are:
2 2 3
3 4 is not possible, since the first term is not divisible by 2.
In sample case #3, no possible arrangement exists.
Problem D. Sorting
Array
Problem
We are in the process of creating a somehow esoteric sorting algorithm to sort an array A of all integers between 1 and N. The integers in A can start in an arbitrary order. Besides theinput order, the algorithm depends on two integers P(which would be at most 3) andK. Here is how the algorithms works:
Partition A into K disjoint non-empty subarrays A1, A2, ..., AK such that concatenating them in order A1A2 ... AK produces
A.
Sort each subarray individually.
Choose up to P of the subarrays, and swap any two of them any number of times.
For example, consider A = [1 5 4 3 2] and P = 2. A possible partition into K = 4 disjoint subarrays is:
A1 = [1] A2 = [5] A3 = [4] A4 = [3 2] After Sorting Each Subarray: A1 = [1] A2 = [5] A3 = [4] A4 = [2 3] After swapping A4 and A2: A1 = [1] A2 = [2 3] A3 = [4] A4 = [5]
We want to show the algorithm is good for distributed environments by finding, for a fixed input and value of P, the maximum number of partitions K such that, choosing
the partitions and swaps wisely, we can achieve a sorting of the original order. Can you help us to calculate that K?
Input
The first line of the input gives the number of test cases, T.T test cases follow. Each test case consists of two lines. The first line contains two integers N and P, as described above.
The second line of the test case contains N integers X1, X2, ..., XN represting array A.
Output
For each test case, output one line containingCase #x: y, where
xis
the test case number (starting from 1) and
yis the maximum possible value for the parameter K.
Limits
1 ≤ T ≤ 100.1 ≤ N ≤ 5000.
1 ≤ Xi ≤ N, for all i.
Xi ≠ Xj for all i ≠ j.
Small dataset
P = 2.Large dataset
P = 3.Sample
Input | Output |
5 5 2 1 5 4 3 2 5 2 4 5 1 2 3 6 2 6 3 5 2 4 1 5 3 4 5 1 2 3 6 3 1 2 6 4 5 3 | Case #1: 4 Case #2: 2 Case #3: 3 Case #4: 3 Case #5: 6 |
Same as walk through in the statement.
Case #2:
[4 5] [1 2 3]
Swap the 2 blocks: [1 2 3] [4 5]
Case #3:
[6] [3 5 2 4] [1]
Sort [3 5 2 4], then swap [6] and [1], we get: [1] [2 3 4 5] [6]
Case #4:
[4 5] [1] [2 3]
Swap [4 5] and [1], then swap [2 3] and [4 5]: [1] [2 3] [4 5]
Case #5:
[1] [2] [6] [4] [5] [3]
Swap [6] and [3]: [1] [2] [3] [4] [5] [6]
Note: First 3 sample cases would not appear in the Large dataset and the last 2 sample cases would not appear in the Small dataset.
相关文章推荐
- Google APAC 2017 University Test Round B
- 【在线笔试题解题报告系列】Google APAC 2017 University Test Round B
- Problem D. Stretch Rope Google APAC 2017 University Test Round D
- 【在线笔试题解题报告系列】Google APAC 2017 University Test Round E
- Problem D. Soldiers Google APAC 2017 University Test Round C
- Google APAC 2017 University Test Round A
- 【在线笔试题解题报告系列】Google APAC 2017 University Test Round A
- Problem B. Rain Google APAC 2017 University Test Round A
- Problem C. Evaluation Google APAC 2017 University Test Round C
- Google APAC 2016 University Graduates Test Round B解题报告
- Problem B. gBalloon Google APAC 2016 University Test Round D
- Google APAC 2016 University Graduates Test Round A
- Google APAC 2016 University Graduates Test Round D
- Problem A. Diwali lightings Google APAC 2017 University Test Round E
- Problem B. Beautiful Numbers Google APAC 2017 University Test Round E
- Problem A. Lazy Spelling Bee Google APAC 2017 University Test Practice Round
- Problem C. Jane's Flower Shop Google APAC 2017 University Test Round A
- Problem C. Watson and Intervals Google APAC 2017 University Test Round B
- Problem B. Robot Rock Band Google APAC 2017 University Test Practice Round
- Problem B. Sherlock and Watson Gym Secrets Google APAC 2017 University Test Round B