Hi there,
below there is a problem with its solution,
can someone explain what actually that code does and is it a some kind of theorem ?? i really dont get how it comes up with a solution
Problem
Input Format : -> int Array
[N ,T ,S ,U]
Output Format : -> int
Max Earning
where ,
N - no.of items to be purchased
T - no.of types of items (i.e) if T = 5, then types_of_items = range(1,6)
S - no.of items in a group
U - minimum no.of unique types in any group
Example:
Input 1 :
[8, 4, 3, 2]
Output 1:
20
Explanation:
This composition (1+1+2+2+3+3+4+4) can make a maximum of 20 if 8 items are purchased
((2+4+2)+(1+3)+(4+3+1))
2 distinct types (1 & 3)
2 distinct types (2 & 4)
3 distinct types (1, 3 & 4)
((1+1+2)+(2+3+3)+(4+4))
2 distinct types (1 & 2)
2 distinct types (2 & 3)
1 distinct type (4) [acceptable because in a group of 3 there must be 2 distinct item types]
you might be wondering in composition (4,3)+(4,3)+(4,2)+(3,2)
Wouldn't (4,3)+(4,3)+(4,2)+(3,2) = 25 be the max earning for [8,4,3,2] ?
ans - because there should not be any group of length S such that unique items in that composition < U
above composition is not valid because
len(set([4,4,4])) < U #where U = 2 given in input
Input 2 :
[3, 1, 4, 3]
Output:
-1
Explanation :
Among 3 items purchased, for any group of four (4) items in a set, there must be a minimum of three (3) unique item types but there are only two (2) item types in this offer. The offer is not purchasable
Solution
N,T,S,U = map(int,input().split()) # N T S U input
if T<U:
print(-1) # if types < unique set then groups cant form
exit()
if U==1: # if distinct elements = 1 then earning = types*produucts
print(N*T)
else:
# T = T1 types of products
# T-1 to T-n : T2
# T-n-1 : T3
maxEarning = -1
T2max = int((S-1)/(U-1))
for T2 in range(T2max,0,-1):
T1 = S + (T2-1) - T2*(U-1)
n = int((N-T1)/T2)
T3 = (N-T1)%T2
if n>T-1 or (n == (T-1) and T3>0): # when types of products not enough break
break
Earning = T*T1 + (T-1+T-n)*n/2*T2 + T3*(T-n-1)
if Earning <= maxEarning:
break
maxEarning = Earning
print(maxEarning)