zoukankan      html  css  js  c++  java
  • SDUT 2408 Pick apples

    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大学生程序设计竞赛

      

      一开始以为是完全背包问题,但是背包的体积V太大,直接背包果断不过。 

      正解应该是 大范围贪心,小范围背包。

      直接谈心不能保证充分利用背包的体积,从而不能保证找到最优解。

      但是背包找到的最优解也肯定是尽可能将性价比高的物品放进去。

      这样答题思路就出来了,先分出一部分空间 V1 来用来贪心,剩余部分 V2 = V - V1用来背包,贪心即选择性价比最高的物品放进去。

      如果贪心后 V1 有剩余的空间 , 设剩余空间为V3,则应将V3回收用来背包,即 V2 += V3。

      这样得出的结构就是正确答案了。

      但是这里还牵扯到一个问题,那就是V1和V2的划分问题。

      这里有两种方法,一种就是直接分出较大的一部分空间来用来背包,显然此部分要在时间和空间上都能承受。

      还有一种就是找给出的这三个体积的最小公倍数,至于为什么这样能行,我也不知道了,坐等大神来证明 o(∩_∩)o 

      下面是在SDUT OJ上 AC 的代码

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cmath>
     6 
     7 using namespace std;
     8 
     9 long long int gcd(long long int a,long long int b)//辗转相处求最大公约数,再用此数求最小共倍数
    10 {
    11     long long int c = a%b;
    12     if(c)
    13     {
    14         return gcd(b,c);
    15     }
    16     return b;
    17 }
    18 
    19 long long int bag[1000010];
    20 
    21 struct N
    22 {
    23     long long int p,w;
    24     double pc;
    25 } a[5];
    26 
    27 bool cmp(N a1,N a2)//按性价比降序排序 为大范围贪心做准备
    28 {
    29     if(a1.pc > a2.pc)
    30         return true;
    31     return false;
    32 }
    33 
    34 long long int Max;
    35 
    36 int main()
    37 {
    38     long long int t,js = 1;
    39     long long int v,i,j;
    40     cin>>t;
    41     while(t--)
    42     {
    43         for(i = 0; i < 3; i++)
    44         {
    45             cin>>a[i].w>>a[i].p;
    46             a[i].pc = a[i].p*1.0/a[i].w;
    47         }
    48         cin>>v;
    49 
    50         sort(a,a+3,cmp);
    51 
    52 
    53 
    54         long long int g1 = gcd(a[0].w,a[1].w);
    55         g1 = a[0].w/g1*a[1].w;
    56         long long int g = gcd(a[2].w,g1);
    57         g = a[2].w/g*g1;//寻找这个三个数的最小共倍数;
    58 
    59         g += 100;//一开始没有这一步在SDUST OJ上AC了  但是在SDUT上 死活A不了 
    60 
    61         if(g <= v)
    62         {
    63             long long int temp_v = v-g;
    64             Max = (long long int)temp_v / a[0].w * a[0].p;
    65             temp_v %= a[0].w;
    66             g += temp_v;
    67         }
    68         else
    69         {
    70             Max = 0;
    71             g = v;
    72         }
    73 
    74         memset(bag,0,(g+2)*sizeof(long long int));
    75 
    76         long long int Max2 = 0;
    77 
    78         for(i = 0;i < 3; i++)//中规中矩的完全背包
    79             for(j = a[i].w;j <= g; j++)
    80             {
    81                 bag[j] = max(bag[j],bag[j-a[i].w]+a[i].p);
    82                 if(bag[j] > Max2)
    83                     Max2 = bag[j];
    84             }
    85 
    86         cout<<"Case "<<js++<<": "<<Max+Max2<<endl;
    87     }
    88     return 0;
    89 }
  • 相关阅读:
    安装adobe,路径My Pictures或卷无效。请重新输入。
    PrintDocument打印、预览、打印机设置和打印属性的方法(较完整) .
    C# 生成CODE128条码
    SQL2005 安装时 “性能监视器计数器要求(错误)” 解决方案
    Siebel escript学习笔记
    siebel 界面搭建
    Siebel Tools 开发笔记
    Siebel Tools配置
    Oracle:environment variable "PATH" does not exceed the recommended length
    IOS开发入门实例
  • 原文地址:https://www.cnblogs.com/zmx354/p/3092438.html
Copyright © 2011-2022 走看看