zoukankan      html  css  js  c++  java
  • Pick apples(大范围贪心,小范围完全背包)

    Pick apples

    Time Limit: 1000MS Memory Limit: 165536KB

    Problem 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.

    Example Input

    1
    1 1
    2 1
    3 1
    6
    

    Example Output

    Case 1: 6

    Hint

     

    Author

    2012年"浪潮杯"山东省第三届ACM大学生程序设计竞赛
    /*
    * @Author: lyuc
    * @Date:   2017-04-27 16:06:19
    * @Last Modified by:   lyuc
    * @Last Modified time: 2017-04-27 19:27:24
    */
    
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #define LL long long
    #define maxn 1000000
    using namespace std;
    struct node{
        int s,p;
        bool operator <(const node & other) const {
            return p*1.0/s>other.p*1.0/other.s;
        }
    }fr[4];
    LL dp[1000005];
    int t;
    int v;
    LL res;
    int main(){
        // freopen("in.txt","r",stdin);
        scanf("%d",&t);
        for(int ca=1;ca<=t;ca++){
            printf("Case %d: ",ca);
            res=0;
            memset(dp,0,sizeof dp);
            for(int i=0;i<3;i++){
                scanf("%d%d",&fr[i].s,&fr[i].p);
            }
            scanf("%d",&v);
            if(v<=maxn){
                for(int i=0;i<3;i++){
                    for(int j=fr[i].s;j<=v;j++){
                        dp[j]=max(dp[j],dp[j-fr[i].s]+fr[i].p);
                    }
                }
                printf("%lld
    ",dp[v]);
            }else{
                sort(fr,fr+3);
                while(v>maxn){
                    res+=fr[0].p;
                    v-=fr[0].s;
                }
                for(int i=0;i<3;i++){
                    for(int j=fr[i].s;j<=v;j++){
                        dp[j]=max(dp[j],dp[j-fr[i].s]+fr[i].p);
                    }
                }
                printf("%lld
    ",res+dp[v]);
            }
        }    
        return 0;
    }
  • 相关阅读:
    【精品软件】小蔡电脑助手 V2.0
    C++ 数据结构与算法(二)线性表之单链表
    C++ 数据结构与算法:冒泡排序及改进算法
    C++ 数据结构与算法(四)线性表之循环链表
    const int *p、int*const p、和 int const *p 详解
    C++ 指针和引用的区别
    C++ 数据结构与算法(三)线性表之双向链表
    C++ 数据结构与算法(一)线性表之顺序表
    C++数据结构与算法:选择排序
    VC++ 获取文件夹大小
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6776151.html
Copyright © 2011-2022 走看看