




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





October 2024 Time Allowed: 2 Hours
We can measure the popularity of a person by how many friends a person has. We assume that ”friend” is a symmetric relation. If A is a friend of B, then B is a friend of A.
Given a list of k people, with id numbered from 0, 1,.. ., k− 1 , we can represent n pairs of friendship among these people using two lists of numbers F and G. Each list contains n integers ranging from 0 to k − 1 , with possible repetitions. Let F = ⟨f 0 , f 1 ,... , fn− 1 ⟩ and G = ⟨g 0 , g 1 ,... , gn− 1 ⟩, then fi is a friend of gi (and vice versa) for 0 ≤ i < n. A pair of friends is listed only once in F and G, and they can be listed in any order.
Write a program friend that reads the following from the standard input:
Print, to the standard output, the ID of the person who is the most popular (i.e., with the most number of friends). We break ties by preferring the person with a smaller id.
0 1
3 2
4 0 2 1 3
Figure 1 Figure 2
Consider the input below that corresponds to Figure 1 above.
4 4 0 3 3 1 3 1 2 2
In this input, there are four people and four pairs of friends among them. The pairs of friends are (0, 3), (3, 1), (3, 2), and (1, 2). The most popular person is person 3.
Another example is shown in Figure 2 and corresponds to the input below:
5 8 4 4 4 0 0 0 2 1 1 2 0 1 3 2 1 3
Here, there are five people and eight pairs of friends among them. Person 0 and Person 1 have the most friends (four each). We break ties by choosing Person 0.
echu21@pe101:~$./friend 4 4 0 3 3 1 3 1 2 2 3
echu21@pe101:~$./friend 5 8 4 4 4 0 0 0 2 1 1 2 0 1 3 2 1 3 0
Help Mr Nodle schedule his consultation hours by merging a given list of intervals.
An interval is a pair of integers [a, b] where a is the start time and b is the end time. An interval [a, b] is contained in another interval [c, d] if c ≥ a and b ≤ d. In this case, merging [a, b] and [c, d] gives the interval [c, d]. Two intervals [a, b] and [c, d] overlap if a ≤ c ≤ b or c ≤ a ≤ d. In this case, merging [a, b] and [c, d] gives the interval [min(a, c), max(b, d)].
For example, merging [1, 3] and [2, 4] gives [1, 4]. Merging [2, 3] and [1, 6] gives [1, 6]. Interval [1, 2] and [2, 3] are also overlapping, and merging them gives [1, 3]. On the other hand, the intervals [1, 3] and [4, 5] are disjoint. Merging [1, 3] and [4, 5] has no effect and gives [1, 3] and [4, 5].,
Write a program interval that reads, from the standard input, a positive integer n followed by 2 n non-negative integers representing n intervals. The numbers are start and end times. The i-th interval will be the 2 i-th number and (2i + 1)-th number in the sequence. You are guaranteed that the intervals are sorted in increasing order by their end time.
The program merges as many intervals in the input as it can, and prints to the standard output the merged intervals, one per line, in increasing order of the end time. When printing an interval, the start and end times should be separated by a comma and a space, and the intervals should be enclosed in square brackets. The function print_interval is provided to help you print an interval.
The input 3 0 5 3 6 4 6 represents intervals [0, 5], [3, 6], and [4, 6]. The three intervals can be merged as [0, 6]. So Mr. Nodle starts his consultation at time 0 and ends at 6.
The input 2 2 1000000 1000000 1000001 represents intervals [2, 1000000] and [1000000, 1000001]. The first interval ends at the same time the second interval starts. The two intervals are merged as [2, 1000001]. Mr. Nodle is gonna hold a really long consultation hour.
The input 4 2 4 5 8 2 8 9 11 represents intervals [2, 4], [5, 8], [2, 8], and [9, 11]. The first three intervals are merged into [2, 8]. The last interval [9, 11] is disjoint from [2, 8]. The intervals after merging are [2, 8] and [9, 11]. Mr. Nodel gets a break between 8 and 9.
2 marks are allocated to efficiency for this question.
echu21@pe101:~$./interval 3 0 5 3 6 4 6 [0, 6]
echu21@pe101:~$./interval 2 2 1000000 1000000 1000001 [2, 1000001]
echu21@pe101:~$./interval 4 2 4 5 8 2 8 9 11 [2, 8] [9, 11]
This page is intentionally left blank.