Google APAC 2017 University Test Round E
来源:互联网 发布:淘宝网天猫吉他 编辑:程序博客网 时间:2024/06/07 02:08
Problem A. Diwali 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 has 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 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 containing Case #x: y
, where x
is the test case number (starting from 1) and y
is 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 B
or uppercase R
.
Small dataset
1 ≤ I ≤ J ≤ 106.
Large dataset
1 ≤ I ≤ J ≤ 1018.
Sample
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.
#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 writing 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.
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 containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the base described in the problem statement.
Limits
1 ≤ T ≤ 100.
Small dataset
3 ≤ N ≤ 1000.
Large dataset
3 ≤ N ≤ 1018.
Sample
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.
#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 containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the answer, as described above.
Limits
1 ≤ T ≤ 100.
1 ≤ D ≤ 100.
Small dataset
1 ≤ N ≤ 2000.
Large dataset
1 ≤ N ≤ 105.
Sample
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
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 the 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:
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 containing Case #x: y
, where x
is the test case number (starting from 1) and y
is 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
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.
- Google APAC 2017 University Test Round E
- Google APAC 2017 University Test Round A
- Google APAC 2017 University Test Round B
- 【在线笔试题解题报告系列】Google APAC 2017 University Test Round E
- Problem A. Diwali lightings Google APAC 2017 University Test Round E
- Problem B. Beautiful Numbers Google APAC 2017 University Test Round E
- Problem C. Partioning Number Google APAC 2017 University Test Round E
- [ Google APAC 2015 University Graduates Test ] Round C APAC Test
- Google APAC 2016 University Graduates Test Practice Round APAC test
- Problem C. Evaluation Google APAC 2017 University Test Round C
- Problem D. Soldiers Google APAC 2017 University Test Round C
- Problem A. Vote Google APAC 2017 University Test Round D
- Problem B. Rain Google APAC 2017 University Test Round A
- Google APAC 2017 Round E
- Google APAC 2016 University Graduates Test Round A
- Google APAC 2016 University Graduates Test Round A解题报告
- Google APAC 2016 University Graduates Test Round B解题报告
- Google APAC 2016 University Graduates Test Round C解题报告
- [Android开发]从Android官方Demo谈RecyclerView的用法
- jQuery select下拉选框操作小结
- Javascript基础——利用定时器实现的图片无缝滚动(offsetLeft)
- 本地套接字domain通信
- jQuery 实现邮箱下拉列表自动补全功能
- Google APAC 2017 University Test Round E
- 编写精美的聊天界面
- hdu 1062 Text Reverse
- Android之自定义ViewGroup经典例子
- [Linux编程]VIMTUTOR
- 从头到尾彻底解析Hash表算法
- ST
- HTML5
- MutatorMath 与响应式字体设计