dimanche 6 juin 2021

Optimize Python Code to read various inputs as time limit exceeded

Below is my python code I want to be able to optimize my code for different inputs and pass the time limit error I keep getting for my current code. Some sample inputs are for 3 cases n there would be a line for each case space separated as so.

I've tried creating different ways of getting the inputs, even for large value of inputs as well but I can't pass the time limit error for different test cases. If someone could show me where on my code, it's slowing this process down that would be very helpful

 Sample tests:
 3
 3 2 1 1
 2 1 1 1
 5 2 1 1

n = int(input())
if 1<= n <= 100000:
counter = 0
while counter < n:
    i = 1
    a = list(map(int, input().split(' ')))
    D = a[0]
    d = a[1]
    P = a[2]
    Q = a[3]
    if (1 <= d <= D <= 1000000) and d <= D <= 100:
        if 1 <= P and Q <= 1000000:
            days = 1
            while i <= D:
                if i % d == 0:
                    if days == 1:
                        Production = P + Q
                        Total_money = d*P
                        days+=1
                    elif days > 1:
                        Total_money+= Production*d
                        days+= 1
                elif i%d == 1 and i == D:
                    if days <= 2:
                        Total_money+= Production
                    else:
                        Total_money+= Production + Q
                i+= 1
            counter+= 1
            print(Total_money)

Aucun commentaire:

Enregistrer un commentaire