sdut2408 Pick apples (greedy + backpack) Shandong Province third ACM provincial race

Source: Internet
Author: User

This article originates from: http://blog.csdn.net/svitter/


Test instructions: Three kinds of apples, each have corresponding size,value, give you a backpack space, to maximize the value.

The key to this topic is the very large backpack space

Based on indicates the size (1 <= S<= 100) We can consider an apple with the highest price-performance ratio in space beyond 1000000 (100^3). Why is 100^3?

To know that if the backpack is just filling up, and the value of the apple filling space is greater than the value of Apple, then choose to fill the space to make the most value of the apple, rather than the most cost-effective apple-the price of high-performance Apple may be due to the lack of space, the space is not enough to achieve maximum value. 100^3 is larger than the maximum size of any 3 apples, so choose 100^3 this number common multiple.


Another: The initial value of f[1000001] is assigned to-1, in order to find the backpack-= too much water = = completely did not consider the last f[bagsize] may be 1 of the case, and then wrote a find is not-1 of the maximum bag value, water again-= when the assignment-1, The most important thing is that space is used the most.

Simple example:

Bagsize, a[1] 59/30, a[2].size 58/31 a[3].size 57/32;

Post code:

#include <iostream> #include <stdio.h> #include <string.h>using namespace std; #define LLN Long Long    intstruct apple{int Value;    int Size; double cost;};    Apple A[3];lln f[1001000];void Ace () {int T, I, J, no, most;    int bagsize, tempbag;    Double cost;    Lln restnum, ans, restvalue;    int Size;    scanf ("%d", &t);        for (no = 1; no <= t; no++) {memset (f, 0, sizeof (f));        F[0] = 0; most = 0;        Cost = 0; for (i = 0; i < 3; i++) {scanf ("%d%d", &a[i]. Size, &a[i].            Value); A[i]. Cost = (double) a[i]. Value)/a[i].            Size; if (A[i].                Cost > Cost)//find the most {most = i; Cost = A[i].            cost;        }} scanf ("%d", &bagsize);        Restnum = 0;        Restvalue = 0;            More than 1000 of the time, the most cost-effective apple fill in if (Bagsize > 1000000) {tempbag = BagSize-1000000; Restnum = Tempbag/a[most]. Size;            Bagsize-= Restnum * A[most].            Size; Restvalue = Restnum * A[most].        Value; }//The remaining apples use backpack for (i = 0; i < 3; i++) {Size = Bagsize-a[i].            Size; for (j = 0; J <= Size; j + +) {f[j + a[i]. Size] = max (F[j + a[i]. Size], F[j] + a[i].            Value);        }} ans = Restvalue + f[bagsize];    printf ("Case%d:%lld\n", no, ans);    }}int Main () {Ace (); return 0;}


sdut2408 Pick apples (greedy + backpack) Shandong Province third ACM provincial race

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.