Watch more DVDs (enhanced version) (multidimensional DP)

Source: Internet
Author: User

[Problem description]

I joined kindergarten many times and registered today. You can only relax tonight (it will be very busy later ). Her uncle decided to buy him some animated DVDs to watch at night. But grandpa stipulated that they could only finish reading the video within a certain period of time. (Because my uncle is still engaged in noip, he cannot accompany me to watch more dishes too early, but I am sleepy for a long time every day, so I can only watch the dishes in a certain period of time ). List more tables and ask her uncle to buy her n DVD discs. Most of them are cartoons that love to watch more videos (gospel warriors, robot cats, Naruto, cherry balls ......). The N discs are numbered (1, 2, 3 ...... N ). I have scored mi (mi> 0) for each disc. The higher the score, the more I like it. Each disc has a playback time TI. A lot of people want to see the highest total score of the disc in the time set by Grandpa tonight. (You must read the desired dish, that is to say, you cannot only watch half of it ). Obviously, it is unnecessary for uncle to Buy N sheets of discs. If he wants to buy them, he will be OK. This will save money. In addition, as long as he sees a few images, his uncle will surely read them.

However, there is a strange problem, where only M (M <n) disks are purchased for customers, and there will be no more or less disks. This can make a lot of uncles embarrassed. How can I buy only M of N disks and finish reading them at the specified time, and the total value is the highest?

You can help Uncle a lot.

[Input description] (watchdvd. In)

The input file contains three lines.

The first line: two positive integers separated by spaces, N, M, L (indicating the number of disks that Uncle buys for uncle, and the number of disks that the store wants to buy for uncle, respectively, period specified by Grandpa ).

From the second row to the nth row, there are two numbers in each row: T, M, and the information of the DVD in the list is provided.

[Output description] (watchdvd. out)

Output a single row

Indicates the total number of discs that you can watch tonight.

If the M discs sold by the store to my uncle cannot be read at the time specified by grandpa, the output is 0;

[Input example]

3 2 10

11 100

1 2

9 1

[Output example]

3

[Data Scope]

20% of Data n <= 20; L <= 2000;

100% of Data n <= 100 L <= 2000; m <n

[Time limit]

1 s

[Submit link]

Http://www.rqnoj.cn/

[Problem Analysis]

This question is an extended content I found when I was learning about my backpack problem. So I wrote a question. This question is more than the original question: that is to say, not only does the backpack have a limited weight, the number of connections is also limited. In fact, it can also be said to add other conditions in the question "Find, find, and find gf.

A lot of people think this way: It's not easy. In the DP process, record the number of packages backed by the solution. The solution is as long as the number is M.

In fact, this idea is wrong. The optimal solution is different because of M. Different M has different optimal solutions, that is, a better solution without limit m may be the optimal solution after M.

Correct Solution: add one dimension to the State based on the solution of the original knapsack problem, indicating different optimal solutions for different M.

The design status OPT [I, j] indicates the optimal solution for limiting I items when the backpack load is J.

State transition equation:

OPT [I, j] = max (OPT [I-1, J-W [I] + V [I])

Time Complexity:

Phase O (n) * state o (LM) + transfer cost O (1) = O (NML)


# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; int N, M, L; int d [101] [2]; int DP [101] [1001]; int main () {While (scanf ("% d", & N, & M, & L )! = EOF) {int I, j, k; for (I = 1; I <= N; I ++) scanf ("% d ", & D [I] [0], & D [I] [1]); for (I = 0; I <= m; I ++) for (j = 0; j <= L; j ++) DP [I] [J] =-99999999; // note that the initialization is negative infinity for (j = 0; j <= L; j ++) DP [0] [J] = 0; for (I = 1; I <= N; I ++) for (j = m; j> = 1; J --) // note that the value must be from m-> 1; otherwise, the error is returned! For (k = L; k> = d [I] [0]; k --) DP [J] [k] = max (DP [J-1] [k-d [I] [0] + d [I] [1], DP [J] [k]); If (DP [m] [l] <0) DP [m] [l] = 0; printf ("% d \ n ", DP [m] [l]);} return 0 ;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.