shivn's blog

By shivn, history, 13 months ago, In English

There are n tickets numbered from 1 to n. Each ticket has a price given by the array price[] and the number of points that you will get after purchasing the ticket is given in another array points[]. You have x amount of money. You need to find out the maximum number of points you can get.

Constraints are 1<=n<=36 1<=price[i]<=1e9 for all i from 1 to n 1<=points[i]<=1e9 for all i from 1 to n and x<=1e9

I am not able to come up with the optimised approach to the problem. Can someone help me to solve this!!

  • Vote: I like it
  • +4
  • Vote: I do not like it

»
13 months ago, # |
  Vote: I like it +14 Vote: I do not like it

Just Looking at constraints and problem statement gives me feel of Meet in the middle Algorithm.

  • »
    »
    13 months ago, # ^ |
      Vote: I like it +9 Vote: I do not like it

    Yeah it is. Find all subsets for first half and second half. Now for each subset in the first half let's say it has cost c, you need to find the maximum points you can get in the second half under with cost <=x-c. This can be done with stl sets or something

    • »
      »
      »
      13 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks!

    • »
      »
      »
      13 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Isn't that knapsack dp?

      • »
        »
        »
        »
        13 months ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        Check constraints.

        • »
          »
          »
          »
          »
          13 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Oh yea. Mb, didn't notice it said 10^9. I guess maybe you could use coordinate compression. (Idk if you'll take my suggestion as I'm only a newbie... :/)

          • »
            »
            »
            »
            »
            »
            13 months ago, # ^ |
              Vote: I like it +3 Vote: I do not like it

            Coordinate compression doesn't work. Worst case you'll find 10^9 distinct elements anyway

            • »
              »
              »
              »
              »
              »
              »
              13 months ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              But n <= 36 right?

              • »
                »
                »
                »
                »
                »
                »
                »
                13 months ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                let the price array be $$$1,2,4,8,16,\cdots,2^{n-1}$$$. How many different subset sums do you see here?

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  13 months ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  Fair point....

          • »
            »
            »
            »
            »
            »
            13 months ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            In coordinate compression you will need to consider all the number of points that can score which are <=x. So it won't work here.

            • »
              »
              »
              »
              »
              »
              »
              13 months ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              Knapsack doesn't require considering all points though, Don't you just need to do a coord-compression and then do the mitm knapsack dp?