Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Farmer John's cows have quite the sweet tooth, and they especially enjoy eating candy canes! FJ has N
 total cows, each with a certain initial height and he wants to feed them M
 candy canes, each also of varying height (1≤N,M≤2⋅10^5
).

FJ plans to feed the candy canes one by one to the cows, in the order they are given in the input. To feed a candy cane to his cows, he will hang the candy cane so that initially the candy cane is just touching the ground. The cows will then line up one by one, in the order given by the input, and go up to the candy cane, each eating up to their height (since they cannot reach any higher). The candy cane stays suspended in place where it is initially set up and is not lowered to the ground, even after cows eat the bottom of the candy cane. It is possible a cow may eat nothing during her turn, if the base of the candy cane is already above that cow's height. After every cow has had their turn, the cows grow in height by how many units of candy cane they ate, and Farmer John hangs the next candy cane and the cows repeat the process again (with cow 1 again being the first to start eating the next candy cane).

INPUT FORMAT (pipe stdin):
The first line contains N
 and M
.
The next line contains the initial heights of the N
 cows, each in the range [1,10^9]
.

The next line contains the heights of the M
 candy canes, each in the range [1,10^9]
.

OUTPUT FORMAT (pipe stdout):
The final heights of each of the N
 cows on separate lines.
Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

SAMPLE INPUT:
3 2
3 2 5
6 1
SAMPLE OUTPUT:
7
2
7
The first candy cane is 6
 units tall.

The first cow eats the portion of the first candy cane up to height 3
, after which the remaining portion of the first candy cane occupies heights [3,6]
.
The second cow is not tall enough to eat any of the remaining portion of the first candy cane.
The third cow eats two additional units of the first candy cane. The remaining portion of the first candy cane, occupying heights [5,6]
, is not eaten.
Next, each cow grows by the amount she ate, so the heights of the cows become [3+3,2+0,5+2]=[6,2,7]
.

The second candy cane is 1
 unit tall, and the first cow eats all of it.

SCORING:
Inputs 2-10: N,M≤10^3
Inputs 11-14: No additional constraints.


What I have tried:

current code:
n, m = map(int, input().split())
heightcow = list(map(int, input().split()))
finalcow = heightcow.copy()
heightcane = list(map(int, input().split()))

for j in range(m):
    statcane = [[False] * (heightcane[j] + 1) for _ in range(n)]
    for i in range(n):
        min_height = min(heightcow[i] + 1, len(statcane[i]))
        for k in range(min_height):
            if statcane[i][k] == True:
                statcane[i][k] = False
                finalcow[i] += 1

for num in finalcow:
    print(num)
Posted
Updated 17-Dec-23 15:25pm
v2
Comments
Richard MacCutchan 18-Dec-23 3:24am    
You need to get together with the other people who are doing Farner John puzzles. Maybe you can start a new Facebook group for it.
Dave Kreskowiak 18-Dec-23 9:45am    
+5

1 solution

I stopped reading at "PLEASE HELP NOW!". Such demands are exceptionally rude.

The only thing I will say is for performance problems, you do NOT want to put loops inside of loops, which are again, inside another loop.

That's all I'm going to say.
 
Share this answer
 
Comments
v87 17-Dec-23 21:26pm    
hey , Im sorry if I came across as rude. I didnt mean to. Thanks for the advice. Do you perhaps have a code solution that I could use?
Dave Kreskowiak 17-Dec-23 23:24pm    
Nobody here is going to do your homework for you.
Richard MacCutchan 18-Dec-23 3:24am    
This is the third new member working on these ridiculous questions.
Dave Kreskowiak 18-Dec-23 9:42am    
Could be the same person over and over again.
Richard MacCutchan 18-Dec-23 9:46am    
Nothing would surprise me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900