HDU_01 Backpack Series

Source: Internet
Author: User

hdu_2602 Bone CollectorTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 49986 Accepted Submission (s): 20965

Problem Descriptionmany years ago, in Teddy ' s hometown there is a man who was called "Bone Collector". Collect varies of bones, such as dog ' s, cow ' s, also he went to the grave ...
The bone collector had a big bag with a volume of V, and along he trip of collecting there is a lot of bones, obviously , different bone have different value and different volume, now given the each bone's value along his trips, can you CALCU Late out the maximum of the total value the bone collector can get?


Inputthe first line contain a integer T, the number of cases.
Followed by T cases, each case three lines, the first line contain both integer n, V, (N <=, v <=) repr Esenting the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume for each bone.
Outputone integer per line representing the maximum of the total value (this number would be is less than 231).
Sample Input
15 101 2 3 4 55 4 3 2 1

Sample Output
14
Analysis: Bare 01 backpack. title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2602Code Listing:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int MAXN = + 5;int t;int N, v;int value[maxn];int volume[maxn];int dp[maxn];void input () {scanf ("%d%d", &am P n, &v); for (int i = 0; i < N; ++i) {cin >> value[i];} for (int i = 0; i < N; ++i) {cin >> volume[i];}} void Solve () {memset (DP, 0, sizeof (DP)), for (int i = 0; i < N; ++i) {for (int j = V; J >= Volume[i];--j) {Dp[j] = max (Dp[j-volume[i]] + value[i], dp[j]);}} cout << Dp[v] << Endl;} int main () {cin >> t;for (int T = 0; t < T; ++t) {input (); Solve ();} return 0;}

hdu_2546 Rice CardTime limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 22098 Accepted Submission (s): 7730

Problem description UESTC part of the canteen meal card has a very strange design, that is, before purchasing to determine the balance. If the remaining amount on the card is greater than or equal to 5 yuan before the purchase of an item, the purchase must be successful (even if the balance is negative on the card after purchase), it cannot be purchased (even if the amount is sufficient). So we all want to try to keep the balance on the card to the minimum.
One day, the canteen has n vegetables for sale, each vegetable can be purchased once. If you know the price of each vegetable and the balance on the card, ask at least how much of the balance on the card.

Input multiple sets of data. For each group of data:
The first action is an integer n, which indicates the number of dishes. n<=1000.
The second line consists of n positive integers representing the price per vegetable. The price does not exceed 50.
The third line includes a positive integer m, which represents the balance on the card. m<=1000.

N=0 indicates the end of the data.

Output for each set of inputs, outputs a line that contains an integer that represents the smallest possible balance on the card.
Sample Input
1505101 2 3 2 1 1 2 3 2 1500

Sample Output
-4532
Analysis: 01 backpack. involves greedy strategies. First take out 5 yuan to buy the most expensive dishes, then the next backpack capacity of m-5, the number of items n-1, 01 backpack just. Topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=2546 Code listing:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int MAXN = + 5;int N, m;int value[maxn];int dp[maxn];void input () {for (int i = 0; i < n; ++i) {cin >& Gt Value[i];} Cin >> M;} void Solve () {if (M < 5) {cout << m << Endl;return;} M-= 5;sort (value, Value + N), memset (DP, 0, sizeof (DP)), for (int i = 0; i < n-1; ++i) {for (int j = m; J >= Value[i] ; --J) {Dp[j] = max (Dp[j-value[i]] + value[i], dp[j]);}} cout << m + 5-dp[m]-value[n-1] << Endl;} int main () {while (CIN >> n && n! = 0) {input (); Solve ();} return 0;}

hdu_2955 robberiesTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 20096 Accepted Submission (s): 7443

Problem DescriptionThe Aspiring Roy the robber have seen a lot of American movies, and knows so the bad guys usually gets Caught in the end, often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, before retiring to a comfortable Job at a university.


For a few months now, Roy had been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.


His mother, Ola, had decided upon a tolerable probability of getting caught. She feels that he's safe enough if the banks he robs together give a probability less than this.
Inputthe first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to is below, and an Integer N, the number of banks he has plans for. Then follow N lines, where line J gives an integer Mj and a floating point number Pj.
Bank J contains Mj millions, and the probability of getting caught from robbing it's Pj.
Outputfor each test case, output a line with the maximum number of millions he can expect to get while the probability of Getting caught is less than the limit set.

Notes and Constraints
0 < T <= 100
0.0 <= P <= 1.0
0 < N <= 100
0 < Mj <= 100
0.0 <= Pj <= 1.0
A Bank goes bankrupt if it is robbed, and your may assume that all probabilities be independent as the police have very lo W funds.
Sample Input
30.04 31 0.022 0.033 0.050.06 32 0.032 0.033 0.050.10 31 0.032 0.023 0.05

Sample Output
246
  Test instructions: Rob Rob Bank of the amount of J is MJ, the probability of being caught is PJ. When the probability of being caught is less than P, it is believed that the robber can escape. So the problem is, in the case of escaping, the robber can rob the maximum amount of money. Analysis: 01 backpack. A glance is bare 01 backpack, but the array subscript is floating point number, so you have to find a way to convert. We treat the sum of the bank as a backpack capacity, the probability of not being caught as value, then the transfer equation is: dp[j] = max (dp[j-p[i] * (1-m[i]), Dp[j]). So the result is obvious, find the largest capacity v,dp[v] > 1-p, then this V is the result. Topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=2955 Code listing:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace STD; #define EPS 0.000000001const int maxn = + 5;int t;double p;int n;int p[maxn];d ouble M[MAXN];d ouble DP[MAXN * MAXN] void input () {cin >> P >> n;for (int i = 0; i < N; ++i) {cin >> p[i] >> m[i];}} void Solve () {if (P-0 < EPS) {cout << "0" << Endl;return;} int v = 0;for (int i = 0; i < N; ++i) {V + = P[i];}  Dp[0] = 1;for (int i = 1; I <= V; ++i) Dp[i] = 0;for (int i = 0; i < N; ++i) {for (int j = V; J >= P[i];--j) {dp[j] = Max (Dp[j-p[i]] * (1-m[i]), dp[j]);}} for (int i = V; I >= 0; i) {if (Dp[i]-(1-p) > EPS) {cout << i << Endl;return;}}} int main () {cin >> t;for (int T = 0; t < T; ++t) {input (); Solve ();} return 0;}

hdu_1203 I need A offer!Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 24189 Accepted Submission (s): 9728

Problem descriptionspeakless very early want to go abroad, now he has finished all the required examinations, prepared all the materials to prepare, so, they need to apply for school. To apply for any university abroad, you have to pay a certain amount of application fees, which is very alarming. Speakless didn't have much money, only a total of n million dollars. He will choose a number of M schools (certainly within his financial range). Each school has a different application fee of a (million dollars), and speakless estimates the likelihood of his getting a offer from this school B. Whether or not there is an offer between different schools will not affect each other. "I need a offer," he shouted. Help the poor man, help him calculate the maximum probability that he can receive at least one offer. (if Speakless chooses more than one school, get an offer from any school).

Input has several sets of data, and the first row of each group of data has two positive integers n,m (0<=n<=10000,0<=m<=10000)
In the following M-line, each row has two data ai (integer), and Bi (real) represents the application fee for the I-school and the probability of getting an offer.
The last entry has two 0.

Output each set of data corresponds to an export, indicating the maximum probability that speakless may get at least one offer. Expressed as a percentage, accurate to one decimal place.

Sample Input
10 34 0.14 0.25 0.30 0

Sample Output
44.0%HintYou should use printf ("percent") to print a '% '.

Analysis: 01 backpack. This problem is similar to hdu_2955, ask for the possibility of getting at least one job maximum probability, then can consider the opposite, get the minimum probability of work. Then it is easy to understand that the transfer equation: dp[j] = min (Dp[j-a[i]] * (1-b[i]), Dp[j]). The result is naturally 1-dp[n]. Note the output percentages here and remember to convert.
Topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=1203 Code listing:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int MAXN = 10000 + 5;int N, m;int a[maxn];d ouble B[MAXN];d ouble dp[maxn];void input () {for (int i = 0; I <m; +) +i) {scanf ("%d%lf", &a[i], &b[i]);}} void Solve () {dp[0] = 1;for (int i = 1; I <= n; ++i) Dp[i] = 1;for (int i = 0; i < m; ++i) {for (int j = n; J >= A[i ]; --J) {Dp[j] = min (Dp[j-a[i]] * (1-b[i]), dp[j]);//cout << Dp[j] << "";} cout << Endl;} printf ("%.1lf%%\n", (1-dp[n]) * 100);} int main () {while (scanf ("%d%d", &n, &m)! = EOF && n+m) {input (); Solve ();} return 0;}

hdu_1171 Big Event in HDUTime limit:10000/5000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 35145 Accepted Submission (s): 12188

Problem Descriptionnowadays, we all know that computer College are the biggest department in HDU. But, maybe you don ' t know that computer College had ever been split into computer College and software College in 2002.
The splitting is absolutely a big event in hdu! At the same time, it's a trouble thing too. All facilities must go halves. First, all facilities is assessed, and the facilities is thought to being same if they has the same value. It is assumed this there is N (0<n<1000) kinds of facilities (different value, different kinds).

Inputinput contains multiple test cases. Each test case is starts with a number n (0 < n <= – The total number of different facilities). The next N lines contain an integer V (0<v<=50--value of facility) and an integer M (0<m<=100--corresponding Number of the facilities) each. You can assume this all V is different.
A test case starting with a negative an integer terminates input and this test case was not the be processed.

Outputfor, print one line containing, integers A and B which denote the value of computer College and Softwar e College'll get respectively. A and B should be as equal as possible. At the same-time, you should guarantee, which A is not less than B.

Sample Input
210 120 1310 1 20 230 1-1

Sample Output
20 1040 40
Test instructions: Divide all the computers into a A, B part, so that the sum of the two parts is as close as possible, and guarantee the amount a > B. Analysis: 01 backpack. Split the total price, the smaller part as the capacity of B-pack, then the B 01 backpack can get the maximum value of B, a value is out.
Topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Code listing:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int MAXN = + 5;const int maxm = + 5;int n;int v[maxn];int m[maxn];int VALUE[MAXN * Maxm];int DP[MAXN * Maxm];void input () {for (int i = 0; i < N; ++i) {scanf ("%d%d", &v[i], &m[i]);}} void Solve () {int sumv = 0, VA = 0;for (int i = 0; i < N; ++i) {SUMV + = v[i] * m[i];for (int j = 0; J < M[i]; ++j) {VA lue[va++] = V[i];}} int MINV = Sumv/2;memset (DP, 0, sizeof (DP)), for (int i = 0; i < VA; ++i) {for (int j = MINV; J >= Value[i];--j) {DP [j] = Max (Dp[j-value[i]] + value[i], dp[j]);}} printf ("%d%d\n", SUMV-DP[MINV], DP[MINV]);} int main () {while (scanf ("%d", &n)! = EOF && N >= 0) {input (); Solve ();} return 0;}





Hdu_01 Backpack Series

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.