Problem solving with programming: Valera and Plates - Code forces (Round #216) problem
Valera is a lazy student. He has m clean bowls and k clean plates.
Valera has made an eating plan for the next n days. As Valera is lazy, he will eat exactly one dish per day. At that, in order to eat a dish, he needs exactly one clean plate or bowl. We know that Valera can cook only two types of dishes. He can eat dishes of the first type from bowls and dishes of the second type from either bowls or plates.
When Valera finishes eating, he leaves a dirty plate/bowl behind. His life philosophy doesn't let him eat from dirty kitchenware. So sometimes he needs to wash his plate/bowl before eating. Find the minimum number of times Valera will need to wash a plate/bowl, if he acts optimally.
We first count how many times he makes type-1 dish and type-2 dish. He eats type-1 dishes only in bowls.
We first calculate how many cleanings to eat first dish. Let us denote cb as the number of clean bowls and cp as the number of clean plates. Let type1 be the number of first dish type2 be the number of second dish in his plan.
If type1 > cb, We need (type1-cb) cleanings and we are left with 0 clean bowls. Otherwise we need no cleanings , we are left with (cb-type1) bowls.
We add the clean bowls from the previous step to number of clean plates.
cbp = cb + cp.
If type2 > cbp then we need typ2-cbp cleanings otherwise zero.
Valera is a lazy student. He has m clean bowls and k clean plates.
Valera has made an eating plan for the next n days. As Valera is lazy, he will eat exactly one dish per day. At that, in order to eat a dish, he needs exactly one clean plate or bowl. We know that Valera can cook only two types of dishes. He can eat dishes of the first type from bowls and dishes of the second type from either bowls or plates.
When Valera finishes eating, he leaves a dirty plate/bowl behind. His life philosophy doesn't let him eat from dirty kitchenware. So sometimes he needs to wash his plate/bowl before eating. Find the minimum number of times Valera will need to wash a plate/bowl, if he acts optimally.
We first count how many times he makes type-1 dish and type-2 dish. He eats type-1 dishes only in bowls.
We first calculate how many cleanings to eat first dish. Let us denote cb as the number of clean bowls and cp as the number of clean plates. Let type1 be the number of first dish type2 be the number of second dish in his plan.
If type1 > cb, We need (type1-cb) cleanings and we are left with 0 clean bowls. Otherwise we need no cleanings , we are left with (cb-type1) bowls.
We add the clean bowls from the previous step to number of clean plates.
cbp = cb + cp.
If type2 > cbp then we need typ2-cbp cleanings otherwise zero.
Read full article from Problem solving with programming: Valera and Plates - Code forces (Round #216) problemint days;cin >> days;int cb, cp;//# of clean bowls and platescin >> cb >> cp;int i;int type1 = 0, type2 = 0;//count of typ1 and type2 dishesfor( i = 0; i < days; i++ ){int type;cin >> type;if( type == 1 )type1++;elsetype2++;}int nCleans = 0;if( type1 > cb){nCleans = type1 - cb;cb = 0;}else{cb = cb - type1;}int cbp = cb + cp;if( type2 > cbp ){nCleans += type2 - cbp;}cout << nCleans;