Python – two solutions in codeforces.com

Yesterday, I have decided to make an account in codeforeces.com and to check how the system works. Pretty much quite well, in 90 minutes I have managed to solve 2 of the 5 problems:

codeForces
I have decided to stop, because for the 3 next problems I needed better understanding in data structures. Furthermore, I do not think that I would have managed to do them in just 30 minutes, which were left.

Problem 1

Thus, let’s start with the first problem. The whole description is here. These are the sample tests:


input

output

input

output


Thus, what I did to solve it? Pretty much, once you read the solution and cast to integer where needed, you have half of the solution. The next is just to check whether you can assign how many first degrees can you assign, after assigning the minimal second and third degrees. If you have reached your limit, then you simply assign more second degrees and more third degrees. As easy as that, but this is just the first problem:

Problem 2

The second problem is more fun. Its description is here. The idea is to distribute as much tea as possible to a group of people, having in mind that the girls should have half of the boys tea. The tea in the cups of each girl should be equal to each other and the same goes for the boys tea. The number of boys is the same as the number of girls.

Until now the problem seems trivial – you simply divide the whole amount by 1.5 times the number of boys and girls and you give them water. As simple as that! But this is not the case. The owner of the party has different cup sizes. Thus, this makes the problem a little more difficult 🙂

What I did? I have sorted the cups. Let’s imagine, that we have 6 cups and they have the following size  in millilitres – 150, 200, 200, 400,500,1000.

The smallest cup is 150 and it goes to a girl. We have 3 girls and 3 boys. The smallest boy’s cup is 400. These are selected, because with them every boy and every girl can have the same amount of tea. The point is that 150*2 is smaller than 400, which means that the boys should not have more than 300 ml of tea each.

Assuming, that we have unlimited value of tea, our answer should be 150*number_of_girls * 3. Anyhow, this is not the case in the task – our value is limited to the one in the teapot. Thus, I have taken it into account at the beginning, assuming that the max_tea_per_girl = total_teapot/(3*total_boys). At a later stage I reduce the maximum with the constrains given from the smallest_boy_cup and smallest_boy_cup.

Pretty much that is all! 🙂

Here comes the code, it would be probably easier if you run it once or twice to get the whole idea:

 

Thank you and enjoy the code! 😀

About

VBA Developer

Tagged with: ,