• Study Resource
• Explore

Survey
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts

Elementary mathematics wikipedia, lookup

Transcript
```2015
Hong Kong Contest
The University of Hong Kong
March 5, 2015
Please decide and work on EITHER the JUNIOR set
OR the SENIOR set but not both.
****** JUNIOR Question Set ******
Problem J1: Rock-paper-scissors
Time Limit: 1 second
Memory Limit: 256MB
Alice and Bob are playing rock-paper-scissors game. The game rules are simple: rock crushes
scissors; paper covers rock; and scissors cut paper. If both players throw the same shape, the game is
tied. Your task is to determine the number of games won by Alice and Bob, respectively.
Input specification:
 The first line contains one integer N (1 <= N <= 100) that represents the number of games.
 The second line is Alice’s shape sequence. The shape sequence contains N shapes and they
are separated by a space. The i–th shape in the sequence represents the shape thrown by Alice
in the i–th game. There are only three shape values: “rock”, “paper”, and “scissors”.
 The third line is Bob’s shape sequence.
Output specification: Two integers separated by a space, representing the number of games won by
Alice and the number of games won by Bob.
Sample Input 1
3
rock rock scissors
paper rock rock
Sample Input 2
4
paper rock rock scissors
rock scissors rock rock
Sample Output 1
02
Sample Output 2
21
1
Problem J2: What is the minimum area?
Time Limit: 1 second
Memory Limit: 256MB
There are N points in a 2D plane. You can place any square on the plane as long as the square
is rectilinearly oriented, i.e., its sides are paralleled to the x and y axis. What is the minimum area of a
square that can cover at least two points in the plane?
Input specification:
 The first line contains one integer N (2 <= N <= 100) representing the number of points in the
plane.
 The next N lines are the x and y coordinates of the points. The x and y coordinate values are
separated by a space. It is guaranteed that x and y are integers and in the range of [-10000,
10000].
 You can assume that the points are unique.
Output specification: An integer represents the minimum area of a square that can cover at least two
points in the plane.
Sample Input 1
3
00
21
-2 -4
Sample Input 2
3
00
22
33
Sample Output 1
4
Sample Output 2
1
Explanation:
 A possible square for Sample 1 is with the lower left corner and upper right corner locating at
(0,0) and (2,2), which can cover points (0,0) and (2,1). The area of this square is 4.
 A possible square for Sample 2 is with the lower left corner and upper right corner locating at
(2,2) and (3,3), which can cover points (2,2) and (3,3). The area of this square is 1.
2
Problem J3: Queens can’t attack me!
Time Limit: 1 second
Memory Limit: 256MB
Consider a square chessboard with N × N cells and M queens on the chessboard (Note: there
are no other chesses besides the queens).
A queen can move vertically, horizontally or diagonally. As an example, consider the square
chessboard with 6*6 cells with one queen (denoted by the ♕ notation) in Figure 1 below. The cells
that can be reached by the queen are marked with the ⃝ notation. There are 16 cells cannot be
reached by the queen.
⃝
⃝
⃝
⃝
⃝
⃝ ⃝ ⃝
⃝ ⃝
⃝
⃝
⃝ ⃝ ⃝
⃝ ⃝ ⃝ ⃝
⃝ ⃝ ♕ ⃝ ⃝ ⃝
⃝ ⃝ ⃝
⃝ ⃝ ♕ ⃝ ⃝ ⃝
⃝ ⃝ ⃝ ⃝
⃝
⃝ ⃝ ⃝ ♕ ⃝ ⃝
⃝
⃝
Figure 1
Figure 2
Your task is to calculate the number of cells that are NOT reachable by any queens.
Input specification: The first line contains two integers, N and M. (1 <= N <= 100, 1 <= M <= 10).
Following are M lines. Each line contains two integers x and y, representing the location of a queen,
i.e., the queen is at x-th row and y-th column (1 <= x, y <= N).
Output specification: The number of cells that are not reachable by any queens.
Sample Input 1
61
43
Sample Input 2
62
43
64
Sample Output 1
16
Sample Output 2
9
Explanation: Figure 1 and Figure 2 correspond to Sample 1 and Sample 2, respectively.
3
Problem J4: Where to build my house?
Time Limit: 1 second
Memory Limit: 256MB
Bob wants to build a house along a river. Unfortunately some parts of the river are occupied
by animals and cannot be used. Please write a program to help Bob to calculate the length of the
longest continuous river segment that is NOT occupied by any animals.
You can think of the river as a line of length L from position 0 to position L. There are N
animals, each animal occupies a continuous river segment from position s to t. Note that the segments
occupied by animals may be overlapping. Output 0 if the whole river is occupied by animals.
Input specification:
 The first line contains one integer L (1 <= L <= 109) that represents the length of the river.
 The second line contains an integer N (1 <= N <= 100000) that represents the number of
animals.
 Following are N lines. Each line contains two integers s and t (0 <= s < t <= L) that represent
the river segment occupied by one animal.
Output specification: The length of longest continuous river segment that is not occupied by any
animals.
Sample Input 1
5
2
02
35
Sample Input 2
10
2
25
47
Sample Input 3
10
2
05
5 10
Sample Output 1
1
Sample Output 2
3
Sample Output 3
0
Explanation: For Sample 1, as 0-2 and 3-5 are occupied, the only remaining part is 2-3.
For Sample 2, the remaining parts are 0-2 and 7-10.
Problem J5: No friends in the same group.
Time Limit: 1 second
Memory Limit: 256MB
Alice is the teacher of a class of N students. She is going to divide the students into 2 groups. She
has the following requirements:
 Alice knows that some pair of students are very good friend and they must not be assigned to
the same group.
 Alice also wants to minimize the difference between the sizes of the two groups.
Let’s write a program to help Alice to do the grouping, in which the program should:
 Report -1 if it is not possible to do the grouping.
 Report the group assigned to each student that fulfill the requirements.
4
Input specification:
 The first line contains two integers N and M, where N denotes the number of students, and M
denotes the number of known relationship between students.
 Following are M lines, and each line contains two integers S and T, which means student S
and T are very good friend and they must not be assigned to the same group. Each relationship
will only be provided once.
Test cases:
 In our test cases, we have 2 <= N <= 1000 and 1 <= M <= 100000.
 In 30% of the test cases, N <= 16.
 In another 20% of the test cases, the number of possible ways to group students <= 10000.
Output specification:
 If there is no way to do the grouping, output -1.
 Otherwise, output a string containing N characters representing the group assigned to each of
the N students. If the i-th student is assigned to group 1, then the i-th character in the string is
‘1’. Otherwise, the i-th character in the string is ‘2’.
 The string should represent the grouping with minimum difference between the sizes of the
two groups. (That is, the difference between the number of 1’s and the number of 2’s in the
output should be minimized.) If there is a tie, output the lexicographically smallest solution.
Sample Input 1
43
12
34
13
Sample Input 2
33
12
13
23
Sample Input 3
64
12
13
46
45
Sample Output 1
1221
Sample Output 2
-1
Sample Output 3
122211
Explanation:
In Sample 1, since (1, 2), (3, 4) and (1, 3) cannot be assigned into the same group, to make the
difference minimized, we want 1 and 4 be in the same group, and 2 and 3 be in the other. Among the
outputs, 1221 is lexicographically smallest. That is, 1 and 4 are in group 1, and 2 and 3 are in group 2.
In Sample 2, there is no way to do the grouping. The reason is that 1 cannot be in the same group of 2
or 3. But 2 and 3 cannot be in the same group, either.
In Sample 3, 1 cannot be with 2 and 3, and 4 cannot be with 5 and 6. The minimized difference is 0,
by group (1, 5, 6) and (2, 3, 4) together.
****** End of Junior Set ******
5
****** SENIOR Question Set ******
Problem S1: Finding number of pairs
Time Limit: 1 second
Memory Limit: 256MB
Given a sequence of n integers A[1], A[2], ..., A[n] and a positive integer M, count the number of
pairs (i, j) that satisfy the following two conditions:

i < j, and

A[i] + A[j] <= M.
Input specification:
 The first line contains integer n ( 2 <= n <= 105 ) and M ( 0 <= M <= 109 ), separated by a
space.
 The second line contains n integers, which denotes A[1], A[2], ... , A[n] (0 <= A[i] <= 109).
 In 50% of the test cases, n <= 1000.
Output specification:
 The output contains an integer, which denotes the number of pairs that satisfies the two
conditions.
 If the output is smaller than 109+7, please keep it as is. Otherwise, output the number mod
109+7.
Sample Input 1
56
12345
Sample Input 2
5 12
36828
Sample Output 1
6
Sample Output 2
7
Explanation: In Sample 1, among the pairs, (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4) satisfy the
conditions. In Sample 2, (1, 2), (1, 3), (1, 4), (1, 5), (2, 4), (3, 4), (4, 5) satisfy the conditions.
6
Problem S2: Matching problem
Time Limit: 1 second
Memory Limit: 256MB
Given two positive integers N and M, and two strings S and T of lowercase letters, we generate
two strings A and B so that they have the following characteristics:
 A and B have equal string length;
 A is generated by concatenating S for N times;
 B is generated by concatenating T for M times.
It is regarded as a match if the i-th character in A is the same as the i-th character in B. Given N, M, S,
T, please write a program to return the number of matches among A and B.
Input specification:
The first line of the input contains 2 integers, representing N and M, separated by a space.
The second and the third line contain string S and T, respectively.
It is guaranteed that A and B have equal string length.



In 20% of the test cases, the length of A <= 105.
In 40% of the test cases, the length of S <= 10 and the length of T <= 10.
In 100% of the test cases, N, M <= 109, the length of S <= 106, the length of T <= 106.
Output specification: Print the number of matches among A and B.
Sample Input 1
35
ababa
aba
Sample Input 2
30 20
abbb
bbaabb
Sample Output 1
8
Sample Output 2
70
Explanation: In the first sample, A = ababaababaababa, B = abaabaabaabaaba. Hence, Ai = Bi when i
= 1,2,3,6,10,13,14,15.
7
Problem S3: Symmetric Cross
Time Limit: 3 seconds
Memory Limit: 256MB
You are given an R × C matrix M. Each of its elements is either 0 or 1.
A cross of size k is a subregion of M, centering at one of its elements, containing the center
and k elements in each of the four orthogonal directions (i.e., horizontal and vertical directions).
A cross is called “symmetric”' if it does not change after being rotated by 90 degrees. Please
find the cross with the largest size in M. It is guaranteed that there is exactly one such cross.
Input specification:
The first line contains two integers R and C, the number of rows and columns of M.
The next R lines, each contain C numbers separated by one space, representing M.


In 50% of the test cases, 3 <= R, C <= 500.
In 100% of the test cases, 3 <= R, C <= 2000.
Output specification: The output contains a single line with three integers: the size of the largest
symmetric cross in M, and the row and column number of its center.
Sample Input 1
55
00100
00000
10101
00000
00100
Sample Input 2
55
00100
01001
10100
00101
00100
Sample Output 1
233
Sample Output 2
122
8
Problem S4: Maze – When to meet?
Time Limit: 1 second
Memory Limit: 256MB
A maze consists of N rooms and N one-way corridors. From the i-th room there is exactly one
one-way corridor to the k[i]-th room.
An adventurer and a minotaur are in the maze. The adventurer will move from a room to the
next room in one minute, while the minotaur will do the same in two minutes. The maze is
constructed so that as long as the adventurer and the minotaur start moving at the same moment, they
will eventually meet in the same room.
The adventurer and the minotaur choose two different rooms in the maze at the very
beginning. After that, they will keep moving from one room to another room via the corridors. You
are asked the maximum possible time for the adventurer and the minotaur to meet exactly in a room.
Input specification:
The first line contains an integer N, the number of rooms.
The second line contains N numbers, of which the i-th number is k[i].


In 50% of the test cases, 1 <= N <= 5000;
In 100% of the test cases, 1 <= N <= 100000.
Output specification: The maximum possible time for the adventurer and the minotaur to meet in a
room.
Sample Input 1
6
234561
Sample Input 2
6
234534
Sample Output 1
10
Sample Output 2
8
Explanation: In the second sample, if the adventurer starts at room 6 and the minotaur starts at room
1, they need 8 minutes to meet at the same room (i.e., room 5).
9
Problem S5: Can I pass the courses?
Time Limit: 1 second
Memory Limit: 256MB
There are many students in the University of Hong Kong. Some of them are hardworking, some are
clever and some are both hardworking and clever. We use two real numbers to describe a student.
HWP (Hard Working Point) describes how hardworking she is, and CP (Clever Point) describes how
clever she is.
There are many courses in the University of Hong Kong. For some of the courses, like
physics, student must work very hard and do a lot of exercise to pass the course. So students with high
HWP can pass it easily. Of course, if a student is clever, it also helps a little. For some other courses,
like artificial intelligence, CP helps a lot and HWP helps a little. So we can also use two real numbers
x and y to describe a course. A student can pass the course if and only if
HWP × x + CP × y > 1.
There is a course management system, teachers can create new courses or cancel courses, and
students can know if she can pass all the courses that exist right now.
Input specification:
The first line of the input contains one integer M, representing the number of operations.
Each of the following M lines contains some numbers describing an operation.
 If the first number is 0, it means a teacher creates a new course. Then the following three
numbers in this line are x, y, id of the course. (0.001 < x, y < 1000, 0 <= id <= 100000, and id
is an integer)
 If the first number is 1, it means a teacher cancels an old course. Then the following one
number in this line represents the id of the course to cancel. It is guaranteed that this course
exist before deleting.
 If the first number is 2, it means a student wants to know if she can pass all the courses that
exists right now, the following two numbers in this line are HWP and CP of the student.
(0.001 < HWP, CP < 1000)
To avoid precision problems, for any student and any course, |HWP × x + CP × y – 1| > 10-8.



For 20% of the test cases, M <= 1000.
For other 40% of the test cases, there is no cancel operation.
For 100% of the test cases, M <= 100000.
Output specification: For each student, if she can pass all the courses, print a line with single integer
1. Otherwise, print a line with a single integer 0.
10
Sample Input 1
8
0121
0 5 0.2 2
2 0.1 2
2 0.1 3
0 0.13 0.59 3
0 2.44 0.78 4
231
213
Sample Input 2
10
0121
0 5 0.2 2
2 0.1 2
2 0.1 3
12
2 0.1 2
0 0.13 0.59 3
0 2.44 0.78 4
231
213
Sample Output 1
0
1
0
1
Sample Output 2
0
1
1
0
1
****** End of Senior Set ******
11
```
Related documents