• SDUT ACM 2408 Pick apples 贪心+完全背包 Anti


     

    Pick apples

    Time Limit: 1000MS Memory limit: 165536K

    题目描述

    Once ago, there is a mystery yard which only produces three kinds of apples. The number of each kind is infinite. A girl carrying a big bag comes into the yard. She is so surprised because she has never seen so many apples before. Each kind of apple has a size and a price to be sold. Now the little girl wants to gain more profits, but she does not know how. So she asks you for help, and tell she the most profits she can gain.

    输入

    In the first line there is an integer T (T <= 50), indicates the number of test cases.
    In each case, there are four lines. In the first three lines, there are two integers S and P in each line, which indicates the size (1 <= S<= 100) and the price (1 <= P <= 10000) of this kind of apple.

    In the fourth line there is an integer V,(1 <= V <= 100,000,000)indicates the volume of the girl's bag.

    输出

    For each case, first output the case number then follow the most profits she can gain.

    示例输入

    1
    1 1
    2 1
    3 1
    6
    

    示例输出

    Case 1: 6

    来源

    2012年"浪潮杯"山东省第三届ACM大学生程序设计竞赛
     
     
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 long long dp[1110];
     5 int main()
     6 {
     7     int t, sum;
     8     long long s[5], p[5]; //这里必须用long long,不知道原因。。。
     9     double val, max;
    10     scanf("%d", &t);
    11     for(int k = 1; k <= t; k++)
    12     {
    13         int x = 0;
    14         double val, max = 0;
    15         memset(dp, 0, sizeof(dp));
    16         for(int i = 0; i < 3; i++)
    17         {
    18             scanf("%lld %lld", &s[i], &p[i]);
    19             val = 1.0 * p[i] / s[i];
    20             if(val > max)
    21             {
    22                 max = val;
    23                 x = i; //记录性价比最高的苹果的下标
    24             }
    25         }
    26         scanf("%d", &sum); //背包总容量
    27         if(sum <= 1000) //1000以下用完全背包,超出就贪心加背包
    28         {
    29             for(int i = 0; i < 3; i++)
    30                 for(int v = s[i]; v <= sum; v++)
    31                     dp[v] = std::max(dp[v], dp[v-s[i]]+p[i]);
    32             printf("Case %d: %lld\n", k, dp[sum]);
    33         }
    34         else
    35         {
    36             int lem = sum - 1000; //用于贪心的背包容量
    37             int n = lem / s[x]; //能装的性价比最高的苹果的数量
    38             long long w = n * p[x]; //贪心所得的利润
    39             int cur = sum - n * s[x]; //剩余的背包容量,剩余的用完全背包
    40             for(int i = 0; i < 3; i++)
    41                 for(int v = s[i]; v <= cur; v++)
    42                     dp[v] = std::max(dp[v], dp[v-s[i]]+p[i]);
    43             printf("Case %d: %lld\n", k, dp[cur] + w);
    44         }
    45     }
    46     return 0;
    47 }
  • 相关阅读:
    manjaro开机出现grub 解决办法
    goquery 解析不了noscript
    同步服务器时间
    phpStorm中使用xdebug工具调试docker容器中的程序
    Goutte 获取http response
    在微信浏览器里使用js或jquery实现页面重新刷新
    Node Sass does not yet support your current environment
    微信支付服务商模式
    PHP获取月末时间
    JavaScript DOM 对象
  • 原文地址:https://www.cnblogs.com/wolfred7464/p/3032902.html
Copyright © 2020-2023  润新知