chauhanshivam079's blog

By chauhanshivam079, history, 2 years ago, In English

Given n and 2 arrays a[i] denoting the time taken to eat ith grape and and b[i] denoting the power gained by eating the ith grape repectively. The total time to eat is T minutes. You can eat one grape irrespective of how many minutes it needs in usual time during the Tth minute of consumption. Find the max power that can be obtained in T minutes. N<=3000 T<=3000 1<A[i],b[i]<=3000

sample tc: n=2 T=60 a[i]={10,100} b[i]={10,100} output : 110 explanation: We can eat the 1 grape in first 10 miniutes.Then in the next 10 minute he can eat the second grape . power=10+100=110.

tc:2 n=2 T=60 a[i]={100,100} b[i]={100,100} output : 100 Explanation: Only one grape can be consumed.After that T minutes get over.. plz explain how to solve this question..I think dp would be used here...but i am not sure..

  • Vote: I like it
  • -5
  • Vote: I do not like it

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

It's looks like 0-1 Knapsack problem:

https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/

I am not sure, since I don't understand the problem (in particular — first example). Would it help if you provide link to actual problem?

  • »
    »
    2 years ago, # ^ |
    Rev. 2   Vote: I like it +3 Vote: I do not like it

    Explanation of the Question:- If at the end let's say 5 min left and time reqd to consume the Apple is 100min then we can take that Apple nd this can be done once only.

    • »
      »
      »
      2 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      I have several ideas, but I can't test them because you didn't provide link to the problem :D

      One possible solution is to pick grape with highest power and eat it at the end. Now you want to choose from other grapes maximum power in time T-1. You can do that with 0-1 knapsack.

      I also was thinking about greedy approach. Sort grapes by value = power / time. Then take grapes with the greatest value until you reach or exceed time T.

      • »
        »
        »
        »
        2 years ago, # ^ |
        Rev. 3   Vote: I like it 0 Vote: I do not like it

        The one which is giving maximum profit can have a min time reqd so I don't think this would give correct answer.

        I don't think greedy would work here as there might be a possibility where better option would be if we take the grape which is having maximum time nd also giving us maximum power in last minute.

        No, this problem is not on codeforces

        • »
          »
          »
          »
          »
          2 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Can we both agree that taking grape with maximum power at the end of the time (this one grape that exceed time) is correct choice? Then all you need to do is to calculate what is maximum power you can get with other grapes in time T-1. And this problem can be solved by using 0-1 knapsack.

          • »
            »
            »
            »
            »
            »
            2 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            I don't think this would work as there might be a case where we don't need to take the grape in last minute

»
2 years ago, # |
  Vote: I like it +3 Vote: I do not like it

You could try a dp[i][j][k], where k is either 0 or 1. dp[i][j][0] is just a regular knapsack, while dp[i][j][1] doesn't take into account the time limit, so if you have X minutes left and a grape with required time T, T > X, you can still use it.