Problems 21 - Codeforces
Problems 21 - Codeforces
Contest 21 (Icathia)
Problem Description
Sam is part of the organizing team arranging the university's career fair and has a list of companies and their respective arrival times and durations.
Due to university-wide budget cuts, there is only one stage/dais available on the entire campus so only one event can occur at a time. Given each
company's arrival time and the duration for which they will stay, determine the maximum number of promotional events that can be hosted during the
career fair.
For example, there are 𝑛 = 5 companies that will arrive at times 𝑎𝑟𝑟𝑖𝑣𝑎𝑙 = [1, 3, 3, 5, 7] and will stay for 𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛 = [2, 2, 1, 2, 1] . The first
company arrives at a time of 1 and stays for 2 hours. At the time 3 , two companies arrive, but only 1 can stay for either 1 or 2 hours. The next
companies arrive at times 5 and 7 and do not conflict with each other. In total, there can be a maximum of 4 promotional event.
Constraints
1 ≤ 𝑛 ≤ 5 ∗ 104
1 ≤ 𝑎𝑟𝑟𝑖𝑣𝑎𝑙[𝑖] ≤ 1000
1 ≤ 𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛[𝑖] ≤ 1000
Both 𝑎𝑟𝑟𝑖𝑣𝑎𝑙 array and 𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛 array will have equal number of elements .
https://codeforces.com/gym/529860/problems Page 1 of 10
Problems - Codeforces 13/06/24, 9:23 PM
Input
The first line contains an integer 𝑛 , denoting the number of elements in 𝑎𝑟𝑟𝑖𝑣𝑎𝑙 .
Each line 𝑖 of the 𝑛 subsequent lines (where 0 ≤ 𝑖 < 𝑛 ) contains an integer that describes 𝑎𝑟𝑟𝑖𝑣𝑎𝑙[𝑖] .
The next line again contains the integer 𝑛 , denoting the number of elements in 𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛 .
Each line 𝑖 of the 𝑛 subsequent lines (where 0 ≤ 𝑖 < 𝑛 ) contains an integer that describes 𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛[𝑖] .
Output
Print the maximum number of promotional events that can be hosted.
input
3
1
3
5
3
2
2
2
output
3
input
1
1
1
5
output
1
https://codeforces.com/gym/529860/problems Page 2 of 10
Problems - Codeforces 13/06/24, 9:23 PM
For the first case, all three events can be hosted as each of the companies arrive only after the previous one's duration has ended.
For the second case, only one company is present and its event can be hosted with no conflict.
Problem Description
Sam is part of the organizing team arranging the university's career fair and has a list of companies and their respective arrival times, durations and the
associated 𝑝𝑟𝑜𝑓𝑖𝑡 to the college. Due to university-wide budget cuts, there is only one stage/dais available on the entire campus so only one event
can occur at a time. Given this information, determine the maximum amount of profit that the college can earn by hosting the promotional events.
For example, there are 𝑛 = 5 companies that will arrive at times 𝑎𝑟𝑟𝑖𝑣𝑎𝑙 = [1, 3, 3, 5, 7] and will stay for 𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛 = [2, 2, 1, 2, 1] , The profit
associated is 𝑝𝑟𝑜𝑓𝑖𝑡 = [2, 3, 4, 2, 2] . The first company arrives at a time of 1 and stays for 2 hours. At the time 3 , two companies arrive, but only 1
can stay for either 1 or 2 hours. Of course, we'd like to host company 3, as it gives us a better profit. The next companies arrive at times 5 and 7 and
do not conflict with each other. In total, there can be a maximum of 4 promotional event, with the total profit as (2 + 4 + 2 + 2) = 10
Constraints
1 ≤ 𝑛 ≤ 5 ∗ 104
1 ≤ 𝑎𝑟𝑟𝑖𝑣𝑎𝑙[𝑖] ≤ 109
1 ≤ 𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛[𝑖] ≤ 109
2 ≤ 𝑎𝑟𝑟𝑖𝑣𝑎𝑙[𝑖] + 𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛[𝑖] ≤ 109
1 ≤ 𝑝𝑟𝑜𝑓𝑖𝑡[𝑖] ≤ 104
1.UniversityCareerFair
Samispartoftheorganizingteamarrangingtheuniversity'scareerfairandhaslistofcompaniesandtheirrespectivearrival
timesanddurations.Duetouniversity-widebudgetcuts,thereisonlyonestage/daisavailableontheentirecampussoonly
oneeventcanoccuratatime.Giveneachcompany'sarrivaltimeandthedurationtheywillstay,determinethemaximum
numberofpromotionaleventsthatcanbehostedduringthecareerfair.
Forexample,therearen=5companiesthatwillarriveattimesarrival=[1,3,3,5,7)andwillstayforduration=[2,2,1,2,1].
Thefirstcompanyarrivesattime1andstaysfor2hours.Attime3,twocompaniesarrive,butonly1canstayforeither1or
2hours.Thenextcompaniesarriveattimes5and7anddonotconflictwitheachother.Intotal,therecanbeamaximumof
4promotionalevents.
FunctionDescription
CompletethefunctionmaxEventsintheeditorbelow.Itmustreturnanintegerthatrepresentsthemaximumnumberof
promotionaleventsthatcanbehosted.
maxEventshasthefollowingparameters):
arrival[arrival/O],.arrival[n-1]]:anarrayofintegerswhereithelementisthearrivaltimeoftheithcompany.
duration[duration[0],..durationn-1]]:anarrayofintegerswhereithelementisthedurationthattheithcompany'sstayat
thecareerfair.
Constraints
•150≤50
•1≤arrival[i]≤1000
•1≤duration/i]≤1000
•Both'arrival'arrayand'duration'arraywillhaveequalnumberofelements
https://codeforces.com/gym/529860/problems Page 3 of 10
Problems - Codeforces 13/06/24, 9:23 PM
Input
The first line contains an integer 𝑛 , denoting the number of elements in 𝑎𝑟𝑟𝑖𝑣𝑎𝑙 .
Each line 𝑖 of the 𝑛 subsequent lines (where 0 ≤ 𝑖 < 𝑛 ) contains an integer that describes 𝑎𝑟𝑟𝑖𝑣𝑎𝑙[𝑖] .
The next line again contains the integer 𝑛 , denoting the number of elements in 𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛 .
Each line 𝑖 of the 𝑛 subsequent lines (where 0 ≤ 𝑖 < 𝑛 ) contains an integer that describes 𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛[𝑖] .
The next line again contains the integer 𝑛 , denoting the number of elements in 𝑝𝑟𝑜𝑓𝑖𝑡 .
Each line 𝑖 of the 𝑛 subsequent lines (where 0 ≤ 𝑖 < 𝑛 ) contains an integer that describes 𝑝𝑟𝑜𝑓𝑖𝑡[𝑖] .
Output
Print the maximum profit that you can earn while hosting the promotional events in your college.
input
5
1
3
3
5
7
5
2
2
1
2
1
5
2
3
4
2
2
https://codeforces.com/gym/529860/problems Page 4 of 10
Problems - Codeforces 13/06/24, 9:23 PM
output
10
Problem Description
You want to build yourself a house. The building company you hired can only build houses with sides from their specific set 𝑠 . That means they can
build you a square house or a rectangular one but if and only if its length and width belong to the set 𝑠 .
This month, they have a special promotion: they will paint the ceiling of a new house for free...but only if its area is not more than 𝑎 . You want them to
do it for free but you also want to be sure that the house will be comfortable and not too small. How many possible house configurations can you
create to have the ceiling painted for free given the side lengths offered?
There is a method to how the company decided what lengths of sides to produce. To determine 𝑛 lengths of wall segments to offer, they start with a
seed value 𝑠0 , some variables 𝑘 , 𝑏 and 𝑚 , and use the following equation to determine all other side lengths 𝑠[𝑖]:
Constraints
1 ≤ 𝑛 ≤ 2 ∗ 107
1 ≤ 𝑠0 ≤ 103
1 ≤ 𝑘, 𝑏, 𝑚 ≤ 109
1 ≤ 𝑎 ≤ 1018
https://codeforces.com/gym/529860/problems Page 5 of 10
Problems - Codeforces 13/06/24, 9:23 PM
https://codeforces.com/gym/529860/problems Page 6 of 10
Problems - Codeforces 13/06/24, 9:23 PM
Input
The first line contains 𝑛 denoting the number of wall lengths offered.
The second line contains an integer 𝑠0 denoting the length of the shortest wall .
The next three lines contains integer 𝑘, 𝑏, 𝑚 denoting three arbitrary integers .
https://codeforces.com/gym/529860/problems Page 7 of 10
Problems - Codeforces 13/06/24, 9:23 PM
The last line contains a long integer 𝑎 denoting the largest area that will be painted for free .
Output
Print the number of variants that allow you to use the promotion .
input
3
1
1
1
2
4
output
6
input
3
1
2
1
2
4
output
3
𝑛 = 3, 𝑠[0] = 𝑠0 = 1, 𝑘 = 1, 𝑏 = 1, 𝑚 = 2 and 𝑎 = 4 .
That means that
1 ∗ 1 , (𝑎𝑟𝑒𝑎 = 1 ≤ 4 ) is good.
1 ∗ 2 , (𝑎𝑟𝑒𝑎 = 2 ≤ 4 ) is good.
1 ∗ 4 , (𝑎𝑟𝑒𝑎 = 4 ≤ 4 ) is good.
2 ∗ 1 , (𝑎𝑟𝑒𝑎 = 2 ≤ 4 ) is good.
2 ∗ 2 , (𝑎𝑟𝑒𝑎 = 4 ≤ 4 ) is good.
2 ∗ 4 , (𝑎𝑟𝑒𝑎 = 8 > 4 ) is bad.
4 ∗ 1 , (𝑎𝑟𝑒𝑎 = 4 ≤ 4 ) is good.
4 ∗ 2 , (𝑎𝑟𝑒𝑎 = 8 > 4 ) is bad.
4 ∗ 4 , (𝑎𝑟𝑒𝑎 = 16 > 4) is bad.
6 of the variants are good and 3 are bad.
D. Shared Interest
1 second, 256 megabytes
Given a graph of friends who have different interests, determine which groups of friends have the most interests in common. Then use a little math to
determine a value to return.
The graph will be represented as a series of nodes numbered consecutively from 1 to friends_nodes. Friendships have evolved based on interests
which will be represented as weights in the graph. Any members who share the same interest are said to be connected by that interest. Once you have
determined the node pairs with the maximum number of shared interests, return the maximal product of the node pairs' labels.
For example, you are given a graph with friend_nodes = 4 and friend_edges = 5:
https://codeforces.com/gym/529860/problems Page 8 of 10
Problems - Codeforces 13/06/24, 9:23 PM
𝑊 𝑒𝑖𝑔ℎ𝑡(𝐼𝑛𝑡𝑒𝑟𝑒𝑠𝑡) 𝐶𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛𝑠
1 2, 3
2 1, 2
3 1, 2, 3
4 2, 4
In this case we see that the pairs (1,2) [have interests 2 and 3] and pair (2, 3) [have interests 1 and 3] each share two interests which is maximal. We
take the products of the node numbers: 1 x 2 = 2 and 2 x 3 = 6. The maximal value 6 is returned.
Constraints
2 ≤ friends_nodes ≤ 100
(𝑓𝑟𝑖𝑒𝑛𝑑𝑠_𝑛𝑜𝑑𝑒𝑠 × (𝑓𝑟𝑖𝑒𝑛𝑑𝑠_𝑛𝑜𝑑𝑒𝑠 − 1))
1 ≤ friends_edges ≤ 𝑚𝑖𝑛(200, 2
)
1 ≤ friends_weight[i] ≤ 100
1 ≤ friends_from[i], friends_to[i] ≤ 𝑓𝑟𝑖𝑒𝑛𝑑𝑠_𝑛𝑜𝑑𝑒𝑠
1 ≤ friends_weight[i] ≤ 𝑓𝑟𝑖𝑒𝑛𝑑𝑠_𝑒𝑑𝑔𝑒𝑠
𝑓𝑟𝑖𝑒𝑛𝑑𝑠_𝑓𝑟𝑜𝑚[𝑖] ≠ 𝑓𝑟𝑖𝑒𝑛𝑑𝑠_𝑡𝑜[𝑖]
Each pair of friends can be connected by zero or more interests.
Input
The first line contains two integers 𝑛 and 𝑚 denoting the number of vertices and edges respectively.
The next 𝑛 lines contains three integers 𝑣1 , 𝑣2 and 𝑤 denoting two vertices and weight of edge between them respectively.
Output
A single integer denoting the maximal product of the node pairs' labels.
input
4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 5
output
6
input
10 6
1 2 2
7 3 2
5 6 2
10 8 2
6 9 2
2 3 2
output
80
https://codeforces.com/gym/529860/problems Page 9 of 10
Problems - Codeforces 13/06/24, 9:23 PM
The pairs connected by the maximal number of interests are (1, 2) and (2, 3) . Their respective products are 1 × 2 = 2 and 2 × 3 = 6 . We then return
the largest of these values as our answer, which is 6.
https://codeforces.com/gym/529860/problems Page 10 of 10