In celebration of the 4th of July, Aldo goes to an American buffet. In this buffet, there are different types of dishes. The -th plate fills a percentage of your stomach and gives you a percentage of happiness.
Aldo can eat a plate at the most once, because he wants variety, and he wants to achieve 100% happiness without going over 100% full. Help Aldo determine the maximum number of dishes he can eat while satisfying his restrictions.
Notes:
It is valid to exceed 100% happiness, but not 100% fullness. It is guaranteed that Aldo can always reach 100% happiness.
Input : The first line contains an integer, the number of dishes. The following lines contain 2 integers each, and the percentages of fullness and happiness that they cause, respectively.
Example input :
- 3
- 60 50
- 30 50
- 50 60
- Answer : 2
- 2
- 100 20
- 100 100
Answer : 1
Please, i need at least a hint. I tried to solve it solo but i can't.
Maintain a 3-D dp. $$$dp[i][j][k]$$$ denotes that given dishes 1 to i, what is the maximum number of dishes that can be eaten so that happiness $$$\ge j$$$ and fullness=k. Base case is $$$dp[0][j][k]=0$$$. So, if the happiness and fullness of $$$i^{th}$$$ dish are $$$x_i$$$ and $$$y_i$$$, then $$$dp[i][j][k]=max(dp[i-1][j-x_i][k-y_i]+1,dp[i-1][j][k])$$$ for $$$k \ge y_i$$$. For $$$k<y_i$$$, $$$dp[i][j][k]=dp[i-1][j][k]$$$. Here $$$dp[i][j][k]$$$ is 0 if $$$j<0$$$. Complexity is O(100*100*NUM_DISHES).
And the answer is ( a is the number of dishes) dp[a][a][a] ?
It is $$$max(d[a][100][0], dp[a][100][1], \dots, dp[a][100][100])$$$.
I am actually not sure whether this approach works. Here is my simple solution, Since we dont need to have exactly 100% fullness, maintain a 1d table of size 100. dp[i] holds maximum happiness I can get if I have i% stomach full. Also maintain index array which holds the index of the element used to obtain this happiness. Then for all i, remove the index used to obtain ith element and then sort the remaining elements in ascending order based on the % fullness. Take the maximum number you can and then check if the cumulative happiness >= 100. If yes, take the max of this for all i from 1 to 100. Therotically this wont go more than O(100 * N)
Can you please provide a c++ code ? ( not need to be exactly but i can understand a code better ) .
incorrect too.. :(
Can you send the input for which this is incorrect ? Im too bored to find the case.
i can't understand why we have dp[i][j-xi][k-yi] if xi is the hapiness
If you have dishes 1 to i and you want to achieve happiness $$$\ge$$$ j with fullness equal to k, there are 2 cases: 1.You eat dish i. Now you have dishes 1 to i-1 and you want happiness $$$\ge j-x_i$$$ with fullness $$$k-y_i$$$. This is $$$dp[i-1][j-x_i][k-y_i]$$$. 2. You do not eat dish i. Then same happiness and fullness are needed with dishes 1 to i-1. That is dp[i-1][j][k].
You said that so if k — yi = fullness so why we search for dp[a][100][0..100] ? if we need 100 of fulness ?
You want happiness to be >=100 while fullness maybe any number from 0 to 100. So, the optimal answer will be the maximum taken over all possible fullness.
And we need to iterate like this ? for(int i = 0;i<a;i++) { for(int j = 0;j<100;j++) { for(int k = 0;k<100;k++) { dp[i][j][k] ?
yeah
could you please send a c++ code i can't implement it solo.
I assumed number of dishes<=100. Not sure if the code works though.
That solution is incorrect :(
I messed up in the base case :/. Try now. I edited the code.
maybe some corner cases ?i come with a solution to generate every possible susbet but it wiil work only for n <= 20 , the constrains are n <= 50 tho.
Can you try to run it 1 last time?
any other ideas ?
The idea is still the same, but we'll need to establish the dp in the base case of j<0 also(since I'm using recursion there, it's not really the base case and is the reason for TLE.) We'll need to precompute the values to be returned there. But as I said earlier, it was my last attempt. :'-(
Hello Bryma,
please do not seek help with ONLY problem statements.
Provide a link to the problem, so people can make sure it is not from a live contest, before they help you. Thanks.
Ok but it's a spanish problem so you need to translate it — https://omegaup.com/arena/replica-cpio-2020/practice/#problems/4-de-julio
I got accepted.
It is only dp[x][y][z] // x : cur_plate, y : fullness, z = happiness.
Firstly, I did simply that and got MLE.
My AC code below.