0% found this document useful (0 votes)
17 views

Exercicios AOCPC 2022

Uploaded by

Gabriel Aricleny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Exercicios AOCPC 2022

Uploaded by

Gabriel Aricleny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

International Collegiate Programming Contest

The 2022 Angolan Collegiate Programming Contest


Angola
October 2022

The International Collegiate Programming Contest


Sponsored by ICPC Foundation

The 2022 Angolan Collegiate


Programming Contest
(Contest Problems)

Angola
October 2022
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem A. Tickets
Input file: standard input
Output file: standard output
Balloon Color: Black

Jack has been working so hard and was able to save X dollars, His dream is to go see the champions
league final match but he doesn’t know if he will be able to buy a ticket or not.
He knows that the ticket cost M dollars, Can you tell Jack if he can afford to buy the ticket or not?

Input
The first line of the input contains one integer t (1 ≤ t ≤ 105 ) — the number of test cases. Then t test
cases follow.
Each test case contains 2 integers M and X (1 ≤ M, X ≤ 103 ) the price of the ticket and the money Jack
has saved.

Output
For each test case, print "YES"if he can buy the ticket otherwise print "NO".

Example
standard input standard output
3 NO
3 2 YES
2 2 YES
3 4

Page 1 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem B. Choose Wisely


Input file: standard input
Output file: standard output
Balloon Color: Maroon

You are given n segments, segment i is defined by Li , Ri . Segments can be overlapped.


You are also given an array A of m integers.
You are asked to choose a subset of the segments in a way that the total number of elements of A contained
in at least one segment of the chosen subset is maximum.
If there is more than one subset that leads you the same maximum number of elements, you are asked to
choose the one with the minimum size among them.
Print the size of the subset, then in the following line print the indices of the segments you chose.

Input
The first line contains one integer n (1 ≤ n ≤ 105 ), the number of segments.
n lines follow, the ith line contain two integers Li , Ri (1 ≤ Li ≤ Ri ≤ 1012 ).
Next line contains an integer m (1 ≤ m ≤ 105 ), the size of array A.
Last line contains m integers the ith of them has the value (1 ≤ Ai ≤ 1012 ).

Output
Print in the first line the minimum size of the subset that contains the maximum number of elements of
the array A within it.
In the next line print the indices of the mentioned subset.

Examples
standard input standard output
2 1
11 26 1
19 34
8
21 22 1 16 4 19 17 38
2 1
14 26 1
15 16
6
20 16 18 2 40 33

Page 2 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem C. Cheating Punishment


Input file: standard input
Output file: standard output
Balloon Color: Yellow

After yesterday contest and because of Ahmed cheating, Kareem decided to punish him by giving him
this problem.
Ahmed has a string S and a number K and he was asked to make this string P alindrome in at most K
step.
At each step he can change any character to any (F orward or Backward character in the Lexicographical
Alphabet).
The meaning of F orward or Backward character is like we say that b is a F orward character of a and
vise versa, a is a Backward character of b and so on.

The cost of this change is equal to CurCharIdx ∗ chSoF ar

• CurCharIdx : is the index of the Current Character.

• chSoF ar : is the number of changes on the Current Character so far.

He can do this changes while (K > 0).


Can you know if Ahmed can solve Kareem Challenge or not.
If he could found any valid answer print Y ES and the remaining K and the P alindrome string, Otherwise
print N O, −1.
If there are multiple solutions you should print the one that is make K larger as possible and
If there are still multiple solutions you should print the smallest lexicographical palindrome
one.

Input
Only one line contains string S where (1 ≤ |S| ≤ 105 ) (it may contains lower or upper case characters),
and an integer K (1 ≤ K ≤ 1012 ).

Output
Print Y ES, K and any valid P alindrome string if there is a solution, Otherwise print N O, −1 each of
them on different line.

Page 3 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Example
standard input standard output
Kareem 100 NO
-1

Note
The index of the first character is equal to 1 not 0.
Due to ASCII code then we will treat capital letters as smaller in the (Lexicographical Alphabet) than
the smaller letters.

Page 4 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem D. Gcd
Input file: standard input
Output file: standard output
Balloon Color: Gold

Mr. El-Guindy is a math teacher. He was teaching his students about the divisors of positive integers. He
taught them that each number has a list of divisors, and 1 is the divisor of each element. So, he motivated
the definition of the “Greatest Common Divisor” of two positive integers a, b denoted by gcd(a, b), which
is found by looking at the list of divisors of a and the list of divisors of b and finding the greatest divisor
that is in both lists.
To make them practice finding the greatest common divisor, he wanted to play a game that involved such
practice. He gave the students a list of N positive integers A1 , A2 , . . . , An , and he challenged the students
to convert this list into 1s by doing the following operation a number of times (possibly 0):
Choose any two adjacent positive integers in the list, and replace one of them with their greatest common
divisor. More formally, choose an index 1 ≤ i ≤ N − 1 and replace either Ai or Ai+1 with gcd(Ai , Ai+1 )
(but not both).
Some students were able to do it fast and got bored, so to give them an exciting challenge, he asked the
students to find the minimum number of operations to accomplish this task. The students got stumped,
even the smart ones, can you help them out?

Input
The first line of input contains T (1 ≤ T ≤ 5 · 105 ) the number of test cases. The description of each test
case follows.
The first line contains N (1 ≤ N ≤ 105 ), the number of positive integers in the list.
The second line contains the list A1 , A2 , . . . , AN (1 ≤ Ai ≤ 1018 ).
The sum of N over all test cases doesn’t exceed 5 · 105 .

Output
For each test case, output one line, the minimum number of operations to convert the list into 1s. If it is
impossible to convert the list, print −1.

Example
standard input standard output
3 4
5 -1
1 2 3 4 5 -1
3
2 4 8
1
2

Page 5 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem E. Reaching City


Input file: standard input
Output file: standard output
Balloon Color: Pink

Zezo has a brother who loves traveling, and he just recently graduated his father decided to give him his
car to travel to any city he want. there is N City representing the power of city ith and you have M Liter
of gasoline and K magic Powers.
You start your journey from city 0 and move to the next city by possibly using X liter of petrol or using
Special power.
The car has its own system for consuming gasoline as follows.

• If the current city power is greater than or equal to the next city’s power, car do not need a gasoline
or magic power.

• If the current city’s power is less than the next city’s power, you can either use one magic power or
(Ai - Ai+1 ) gasoline.

Input
The first line of input contains three integers N , K , M ,(1 ≤ N ≤ 105 ) and (0 ≤ K ≤ N ) and
(1 ≤ M ≤ 109 ) the number of cities , K magic powers and M liter of gasoline.
The second line contains N integers A1 , A2 , ....., AN (1 ≤ Ai ≤ 109 ) where Ai is the total Power of city at
the ith .

Output
output the furthest city index (1-indexed) you can reach if you use the given M gasoline and K magic
Powers optimally.

Example
standard input standard output
7 1 5 5
4 2 7 6 9 14 12

Page 6 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem F. Game
Input file: standard input
Output file: standard output
Balloon Color: Purple

Zezo has 3 brothers two of them are playing a game, there are N Cards on a table, and a number Ai is
written on the ith card.
Momen and Ahmed play alternately to take one card and Momen goes first, Zezo is the judge but he
forgets the final result.
Can you help him to remember the final result? don’t worry It’s a simple game the game ends when cards
there are no cards left and the score of each player is the sum of the numbers written on the cards they
have.
Momen and Ahmed are playing optimal to maximize their scores, Can you help Zezo to know the final
result?

Input
The first line of input contains one integer,(1 ≤ N ≤ 105 ) the number of cards.
The second line contains N integers A1 , A2 , ....., AN (1 ≤ Ai ≤ 109 ) .

Output
Print Momen’s score and Ahmed score when both players take the optimal strategy to maximize their
scores.

Example
standard input standard output
4 38 20
20 18 2 18

Page 7 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem G. Highway
Input file: standard input
Output file: standard output
Balloon Color: Green

Ehab is driving on a highway, he is driving at a speed of N Kmh but you know that this highway has
radars that fine people who drive slower than X Kmh or faster than Y Kmh. Can you tell Ehab if he is
driving too slow or too fast or if his speed is good for the road?

Input
The input consists of three integers N , X, and Y which describe Ehab’s speed, and the X and Y boundaries
described above (1 ≤ N, X, Y ≤ 100), (X ≤ Y ).

Output
If Ehab is driving too slow, print "too slow". If Ehab is driving too fast, print "too fast". Otherwise, print
"just right".

Examples
standard input standard output
75 50 80 just right
90 30 60 too fast
30 40 90 too slow
50 50 80 just right
80 50 80 just right

Page 8 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem H. LCM or greater


Input file: standard input
Output file: standard output
Balloon Color: Orange

Abdou and Ali are colleagues. Each week one of them challenges the other and gives him a math problem.
This week it is Abdou’s turn. Abdou and Ali have learned about GCD (greatest common divisor) and
LCM (lowest common multiple) in their math class.
Abdou thinks that it’s an easy task to calculate the GCD and LCM between two positive integers. He
wants to give Ali a harder task, in this task Ali has to find the LCM of two integers a and b that is in a
given range from low to high (inclusive), i.e. (low <= LCM <= high).
Ali hasn’t studied this topic yet. Can you help Ali to get the answer for each query?

Input
The first line contains two integers a and b, the two integers as described above (1 <= a, b <= 104 ).
The second line contains one integer n, the number of queries (1 <= n <= 105 ).
Then n lines follow, each line contains one query consisting of two integers, low and high
(1 <= low <= high <= 109 ).

Output
Print n lines. The ith of them should contain the result of the ith query in the input. If there is no common
multiple in the given range for any query, you should print −1 as a result for this query.
Note: If there is more than one answer print the smallest.

Examples
standard input standard output
9 27 27
3 -1
27 30 54
10 20
29 60
1 9 18
2 9
10 20
1 15
100 1 100
5 -1
1 300 100
6 18 -1
100 222 200
120 199
101 200

Page 9 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem I. Maximum Length


Input file: standard input
Output file: standard output
Balloon Color: Red

You are given an array A of N integers. You can do the following two types of operations any (possibly
zero) number of times:

• Pick two indices i and j (1 ≤ i, j ≤ N, i 6= j). Change Aj := Aj + Ai and remove the ith element
from the array.

• Pick an index i (1 ≤ i ≤ N ). Split Ai into two integers X and Y such that X+Y =Ai . Remove the
ith element from the array and append elements X and Y to the array.

Find the maximum number of Maximum length of the array after performing any number of operations
of the above two types.

Input
The first line of the input contains an integer t (1 ≤ t ≤ 100) — the number of test cases in the test.
The first line of each test case contains the single number N (1 ≤ N ≤ 105 ) — the size of the array.
The second line of the case contains N integers Ai (−109 ≤ Ai ≤ 109 ) — the elemnts of the array.

Output
For each test case, output the maximum number of Maximum length of the array after performing any
number of operations of the above two types.

Example
standard input standard output
2 2
2 4
1 1
3
2 -1 1

Page 10 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem J. Playing with grids


Input file: standard input
Output file: standard output
Balloon Color: White

One day Hamza was playing with some grids. When he got bored, he decided to invent a new game with
these grids, the game is as follows, you are given an n × n grid and you are asked to sort it in a special
way. You are asked to sort the i-th row of the grid starting from the i-th item. Then after sorting all rows,
rotate the entire grid by 90 degree (clockwise). Help hamza determine what the grid will look like after
playing the game.

Input
The first line contains one integer, t , the number of test cases.
The second line of each test case contains one integer n (1 ≤ n ≤ 500)
The next n lines each contain n numbers 1 ≤ ai ≤ 105 .


It is guaranteed that the sum of n over all testcases does not exeed 103 .

Output
You are required to sort the i-th row starting from the i-th item and output the sorted grid rotated by 90
degree.

Example
standard input standard output
1 11 8 5
3 2 9 7
5 8 7 3 10 8
8 9 10
11 2 3

Page 11 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem K. Dusk and the album


Input file: standard input
Output file: standard output
Balloon Color: Light Blue

The weeknd is releasing a new album next month. Dusk wants to buy the album as soon as it is released.
He knows two pieces of information about the album. The two pieces of information are of the following
type:

• The parity of the release day is x. (where x is even or odd).

• The release day is between day l and day r (inclusive).

Dusk is free only between day n and day m (inclusive). 1 ≤ n ≤ m ≤ 1018 .


What is the probability that the album will be released in the days that dusk is free on?

Input
The first line contains an integer t, the number of testcases.
The first line of each testcase contains a single string s (which is either "even"or "odd") which corresponds
to the first type of information.
The second line of the testcase contains two integers that correspond to the second type of information ,
l and r, 1 ≤ l ≤ r ≤ 1018 , the first day in the range of possible days that the album might come out in,
and the last day.
The third line of the testcase contains two integers, n and m, the first day that Dusk will be free on and
the last day.

Output
The probability that the album will be released in the days that Dusk will be free on. (to the nearest 9
decimal places).

Examples
standard input standard output
1 0.500000000
odd
6 9
8 10
1 1.000000000
even
5 9
4 8

Page 12 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem L. Reversing
Input file: standard input
Output file: standard output
Balloon Color: Dark Pink

Yoge is a lecturer in a computer science department, one day he made a quiz for testing the students.
he gave the students an array of N integers, so he wants to make some operations on array A.
The operation is at each element equal to Zero, you should reverse the order of all elements starting
from the beginning of the array to the element before it, so What is the final order of this array A?

Input
The first line contains an integer N (1 ≤ N ≤ 105 ) the number of elements in array A.
The second line contains N integers Ai (0 ≤ Ai ≤ 105 ) the elements of array A.

Output
You have to print the N numbers after performing all operations on it.

Examples
standard input standard output
7 4 0 1 2 0 5 6
1 2 0 4 0 5 6
3 3 2 1
3 2 1

Note
In the first test explanation is :
Initially array A = 1, 2, 0, 4, 0, 5, 6 .
First operation array A = 2, 1, 0, 4, 0, 5, 6 .
Second operation array A = 4, 0, 1, 2, 0, 5, 6 .
Final order of array A = 4, 0, 1, 2, 0, 5, 6 .

Page 13 of 14
The 2022 ICPC Angolan Collegiate Programming Contest
Angola, October, 2022

Problem M. The Old Mystery


Input file: standard input
Output file: standard output
Balloon Color: Dark Blue

While SpongeBob walking with Gary, they found a complex mystery that leads to the treasure of
the old king Yoge, Help SpongeBob to solve the mystery before the evil Plankton gets the treasure.
The mystery is to choose two different positions in Yoge’s Array and swap the elements at those
positions (The two swapped elements may have the same value), so you are asked to find the number of
different arrays that SpongeBob can obtain after he makes the swap, can you help SpongeBob?
Two arrays are considered different if in at least one position they had a two different values.

Input
The first line of the input contains a single integer N (1 ≤ N ≤ 105 ) the size of the Array A.
The second line contains N space-separate integers A1 , A2 , ..., AN (1 ≤ Ai ≤ 105 ) describing
the values of the Array A.

Output
Print the number of different Arrays that SpongeBob can obtain after he makes the swap.

Examples
standard input standard output
3 3
2 3 2
2 1
1 3
5 1
10 10 10 10 10

Page 14 of 14

You might also like