zoukankan      html  css  js  c++  java
  • Pick apples(完全背包+贪心)

    Description

    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.

    Input

    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.

    Output

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

    Sample

    Input

    1
    1 1
    2 1
    3 1
    6
    

    Output

    Case 1: 6

    Hint

     

    Source

    #include <stdio.h>
    #include <vector>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    using namespace std;
    /*小范围完全背包,大范围贪心*/
    long long dp[1100009];
    struct node
    {
        int a,b;
        double c;//体积/价格
    } a[4];
    bool cmp(node a,node b)
    {
        return a.c<b.c;
    }
    long long max1(long long a,long long b)
    {
        return a>b?a:b;
    }
    int main()
    {
        int t,case1=1;
        scanf("%d",&t);
        while(t--)
        {
            memset(dp,0,sizeof(dp));
            long long v;
            for(int i=1; i<=3; i++)
            {
                scanf("%d%d",&a[i].a,&a[i].b);
                a[i].c=a[i].a*1.0/a[i].b;//计算体积/钱
            }
            cin>>v;
            sort(a+1,a+4,cmp);//数组a是从1开始被输入的
            long long ans =0;
            if(v>1000000)
            {
                ans=(((v-1000000)/a[1].a)*a[1].b);//v-1000000是因为想用贪心的办法把那(v-1000000)个位置填满,免得超时(体积太大)
                v=1000000+((v-1000000)%a[1].a);//计算(v-1000000)后剩余的空间
            }
            for(int i=1; i<=3; i++)
                for(long long j=a[i].a; j<=v; j++)//用完全背包计算这个能装多少
                dp[j]=max1(dp[j],dp[j-a[i].a]+a[i].b);
            printf("Case %d: %lld
    ",case1++,ans+dp[v]);
        }
        return 0;
    }
  • 相关阅读:
    磁盘 inodes 不足 Free inodes is less than 20% on volume
    记录一次Nginx使用第三方模块fair导致的线上故障排错
    xml 特殊字符 导致的 solr 数据导入异常
    Jenkins 定时备份插件 ThinBackup
    Elasticsearch 节点磁盘使用率过高,导致ES集群索引无副本
    Elasticsearch定时删除索引第二版
    js for in 获得遍历数组索引和对象属性
    js函数作用域
    django 1.11.1 连接MySQL
    angular 4 和django 1.11.1 前后端交互 总结
  • 原文地址:https://www.cnblogs.com/RootVount/p/10651430.html
Copyright © 2011-2022 走看看