Docsity

Parapaa Que mas puedo decir, Resúmenes de Sistemas Multiagentes

Toggle sidebar
bg1
BSc Software, Data and Technology 2024
JetBrains Scholarship Test
Programming Tasks
Problem 1
You are a professional trainer of Nsquirrels. After a long period of training, they have become
remarkably intelligent – they even have their own currency to buy their favorite nuts. You know
how many nuts each squirrel has (the i-th squirrel has Ainuts). You’ve noticed that the overall
unhappiness of the squirrels is influenced by the Max(A1, A2, A 3, ..., An), so you want to minimize
this number. You can perform one operation: take a nut from the i−th squirrel and give it to the
j−th squirrel, which will cost you Biunits of currency. You can perform this action as many times
as you want, as long as you can afford it. Initially, you have Kunits of currency. Calculate and print
the minimum unhappiness level you can achieve.
Input Format
The first line contains two integers – Nand K(1⩽N⩽2∗105,0⩽K⩽1018).
The second line contains Nintegers Ai(0⩽Ai⩽106).
The third line contains Nintegers Bi(0⩽Bi⩽106).
Output Format
Output a single integer – the answer to the problem.
Solution
The solution for this problem uses a binary search algorithm. Firstly, let’s prove that it would work.
Let’s say that we can minimize Max(A1, A2, ..., An)into m, then we know that for every m′> m we
can minimize it to m′(m′is a worse answer than m). Now problems sounds like that: find such m, so
that we can make Max(A1, A2, ..., An) = mand it is not possible to make Max(A1, A2, ..., A n) = m−1.
All we need is to make a checker for m. For that let’s denote F=AM 1−AM 2, which is the difference
of AM1-how many nuts we can give to other squirrels, AM2 – how many nuts we need to give to
other squirrels, SU M – how much money we spend on nuts. For the check function, for every Ai> m
we spend SU M +=(Ai−m)·Bimoney, then we should allocate those Ai−mnuts, for that we
have F, it changes F−=Ai−m, for every m≥Aiwe change F,F+ = m−Ai, if at the end of
the check Fis a negative number or we spent more money than K(SU M > K) it means we cannot
make Max(A1, A2, ..., A n) = mand we make msmaller, other-ways we can and we try to check for
mbigger than that.
Code in C++:
#inc lud e<b i t s / s t d c++.h>
using namespace s t d ;
const int N = 2 e 5 + 1 ;
int a [ N] , b [N] , n ;
long long k ;
int ma in ( )
{
Problem 1 continued on the next page… 1 / 11
pf3
pf4
pf5
pf8
pf9
pfa

Vista previa parcial del texto

¡Descarga Parapaa Que mas puedo decir y más Resúmenes en PDF de Sistemas Multiagentes solo en Docsity!

JetBrains Scholarship Test

Programming Tasks

Problem 1

You are a professional trainer of N squirrels. After a long period of training, they have become remarkably intelligent – they even have their own currency to buy their favorite nuts. You know how many nuts each squirrel has (the i-th squirrel has Ai nuts). You’ve noticed that the overall unhappiness of the squirrels is influenced by the M ax(A 1 , A 2 , A 3 , ..., An), so you want to minimize this number. You can perform one operation: take a nut from the i − th squirrel and give it to the j − th squirrel, which will cost you Bi units of currency. You can perform this action as many times as you want, as long as you can afford it. Initially, you have K units of currency. Calculate and print the minimum unhappiness level you can achieve.

Input Format The first line contains two integers – N and K( 1 ⩽ N ⩽ 2 ∗ 105 , 0 ⩽ K ⩽ 1018 ). The second line contains N integers Ai( 0 ⩽ Ai ⩽ 106 ).

The third line contains N integers Bi( 0 ⩽ Bi ⩽ 106 ).

Output Format Output a single integer – the answer to the problem.

Solution The solution for this problem uses a binary search algorithm. Firstly, let’s prove that it would work. Let’s say that we can minimize M ax(A 1 , A 2 , ..., An) into m, then we know that for every m′^ > m we can minimize it to m′(m′^ is a worse answer than m). Now problems sounds like that: find such m, so that we can make M ax(A 1 , A 2 , ..., An) = m and it is not possible to make M ax(A 1 , A 2 , ..., An) = m− 1. All we need is to make a checker for m. For that let’s denote F = AM 1 −AM 2 , which is the difference of AM1-how many nuts we can give to other squirrels, AM2 – how many nuts we need to give to other squirrels, SU M – how much money we spend on nuts. For the check function, for every Ai > m we spend SU M + = (Ai − m) · Bi money, then we should allocate those Ai − m nuts, for that we have F , it changes F − = Ai − m, for every m ≥ Ai we change F ,F + = m − Ai, if at the end of the check F is a negative number or we spent more money than K(SU M > K) it means we cannot make M ax(A 1 , A 2 , ..., An) = m and we make m smaller, other-ways we can and we try to check for m bigger than that.

Code in C++:

#include <b i t s / s t d c ++.h>

using namespace s t d ;

const int N = 2 e5 + 1 ;

int a [N] , b [N] , n ;

long long k ;

int main ( ) {

Problem 1 continued on the next page… 1 / 11

c i n >> n >> k ; for ( int i = 1 ; i <= n;++ i ) c i n >> a [ i ] ; for ( int i = 1 ; i <= n;++ i ) c i n >> b [ i ] ; int l = 1 , r = 1 0 0 0 0 0 0 ; int ans = 0 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; long long f = 0 ; long long sum = 0 ; for ( int i = 1 ; i <= n;++ i ) { i f ( a [ i ] > mid ) { sum += 1 l l ∗ ( a [ i ] − mid ) ∗ b [ i ] ; f −= 1 l l ∗ a [ i ] − 1 l l ∗ mid ; } e l s e { f += 1 l l ∗ mid − 1 l l ∗ a [ i ] ; } } i f ( f >= 0 && sum <= k ) { ans = mid ; r = mid − 1 ; } e l s e l = mid + 1 ; } c o u t << ans ; }

e l s e : # Reverse t h e o r d e r o f o d d _ p o s i t i o n s , f o l l o w e d by e v e n _ p o s i t i o n s ans = o d d _ p o s i t i o n s [ : : − 1 ] + e v e n _ p o s i t i o n s

print ( ans )

Problem 3

Sergei works as an electrician in a city company. One day, he contemplated the possibility of an overvoltage event.

His network is set up linearly, where the i-th substation is xi kilometers away from the power station. Adjacent substations are interconnected, and the edge poles are connected to power generators.

In the case of an overvoltage at a substation (due, for example, to a fault), it would cause an overvoltage at all substations within a 1-kilometer range. Moreover, it creates a chain effect: the farthest substation would trigger an overvoltage at all substations within a 2-kilometer radius, and so on.

Therefore, Sergei is considering how many substations could be affected if only one substation were to fault. Please help Sergei to find the answer.

Input Format The first line contains the natural number n ( 1 ≤ n ≤ 104 ) – the number of substations in the city.

The next n lines contain the coordinate xi ( 1 ≤ xi ≤ 109 ) – the distance between the i-th substation and the power generator. Note that these coordinates could be the same.

Output Format Print a single number – the maximum number of substations that could fault if there is only one initial fault.

Solution Let’s consider all substations are sorted by their coordinates. This allows us to simulate the process of a fault of one substation.

Moreover, let’s firstly consider the task, when we need to calculate how many substations will be affected if we start from the i-th substation.

Observation: all substations that are right of the starting substation could be considered indepen- dently from the substations that are left of the starting substation. This is true because the distance between the starting substation and any substation that is left of the starting is less than the distance to any right substation.

Then, we need to simulate the process, described in the task. Make sure that you accurately simulate the stopping condition and the starting condition: it’s easier to start from the nearest right/left substation.

int affected_num ( int s t a r t , int d i r e c t i o n ) { int r a d i u s = 1 ; int prev = s t a r t ; while ( true ) { int next = prev ; // Get t h e f u r t h e s t a f f e c t e d s u b s t a t i o n i n d e x w i t h o u t e x c e e d i n g // t h e c u r r e n t r a d i u s while ( next + d i r e c t i o n >= 0 && next + d i r e c t i o n < N && abs ( s u b s t a t i o n [ next + d i r e c t i o n ] − s u b s t a t i o n [ prev ] ) <= r a d i u s ) { next += d i r e c t i o n ; }

// We d i d n ’ t f i n d a new h a y b a l e , so t h e c h a i n e x p l o s i o n i s o v e r i f ( next == prev ) { break ; }

Problem 3 continued on the next page… 5 / 11

s o r t ( s u b s t a t i o n s. b e g i n ( ) , s u b s t a t i o n s. end ( ) ) ; int max_affected = 0 ; for ( int i = 0 ; i < N; i ++) { // Get t h e number o f a f f e c t e d s u b s t a t i o n s f o r t h e l e f t & r i g h t s i d e max_affected = max( max_affected , affected_num ( i , −1) + affected_num ( i , 1 ) + 1 ) ; } c o u t << max_affected << e n d l ; }

Math Tasks

Problem 4

Consider all positive integers N whose third part is a perfect cube and half is a fifth power of an integer.

  1. Find the smallest possible value of N. Answer: 3779136.
  2. Find the largest possible remainder of N when divided by 7. Answer: 4.

Solution: N must be divisible by 3 and by 2. Therefore, N = 3a^ · 2 b^ · L, where L is not divisible neither by 3, nor by 2.

N /3 is a perfect cube, so 3 | a − 1 , 3 | b, and L is a perfect cube.

N /2 is a perfect fifth power, so 5 | a, 5 | b − 1 , and L is a perfect fifth power.

Therefore, a = 10 + 15α, b = 6 + 15β, L = γ^15.

The smallest possible value of N is reached when α = 0, β = 0, γ = 1, so Nmin = 3^10 · 26. Note that Nmin ≡ 4 modulo 7.

Modulo 7 the general situation is the following: 315 ≡ 33 ≡ − 1 , 515 ≡ 53 ≡ − 1 , γ^15 ≡ 0 or γ^15 ≡ 1 or γ^15 ≡ − 1. So the remainder is always equal to 3, 4, or 0.

1 (chosen) rook each, which is impossible under given number of rooks.

  1. Alice and Bob got bored with the previous version of amusement, so they changed the rules. Now Bob first selects 15 rows and 5 columns, and then puts rooks in all of the cells on the intersection of these rows and columns (and only in those cells). Then Alice selects 3 rooks so that none of them are in the same row or in the same column. In how many ways can she do that? Answer:

3

Solution: First she selects the columns in which these rooks will be (

3

= 10 ways to do that). We’ll call these columns chosen.

Then she selects any rook from the first chosen column (15 ways to do that). After that she selects any rook from the second chosen column, but not in the same row as the first rook ( ways to do that). And so on. Each time the number of forbidden rows increases by 1.

Problem 6

For real numbers x and y denote G(x, y) = x^2 + y^2 − 16 x − 12 y.

  1. Let G(x, y) = 0. Find the maximum possible value of x + y. Answer: 14 + 10

Geometrical solution: Notice that G(x, y) = 0 is equivalent to (x − 8)^2 + (y − 6)^2 = 10^2 , i.e. we’re dealing with a circle. Now, x + y will reach it’s maximum in the point where the tangent equation is x+y = const. It’s easy to see that the coordinates of this point are

8 + √^102 , 6 + √^102

Algebraic solution: Denote x + y = c and substitute y = c − x in the equation x^2 + y^2 − 16 x − 12 y = 0. After simplifying it, we get x^2 − (c + 2)x +

c^2 2

− 6 c = 0. The discriminant D = −c^2 + 28 c+4 = 200−(c−14)^2 must be nonnegative, so possible values of c are

[

]

  1. Assume that − 4 ⩽ x ⩽ 20 , − 10 ⩽ y ⩽ 20 , both x and y uniformly distributed on their segments. Find the probability of G(x, y) > 0. Answer: 1 −

5 π 36

Solution: G(x, y) > 0 is equivalent to (x − 8)^2 + (y − 6)^2 > 102. So the point should be inside the rectangle [−4; 20] × [−10; 20], but outside the circle. Luckily, the circle is completely inside the rectangle. Therefore, the probability is P = 1 −

area of disk area of rectangle

102 π 24 · 30