• ***C


    C - I love sneakers!
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store.

    There are several brands of sneakers that Iserlohn wants to collect, such as Air Jordan and Nike Pro. And each brand has released various products. For the reason that Iserlohn is definitely a sneaker-mania, he desires to buy at least one product for each brand.
    Although the fixed price of each product has been labeled, Iserlohn sets values for each of them based on his own tendency. With handsome but limited money, he wants to maximize the total value of the shoes he is going to buy. Obviously, as a collector, he won’t buy the same product twice.
    Now, Iserlohn needs you to help him find the best solution of his problem, which means to maximize the total value of the products he can buy.
     

    Input

    Input contains multiple test cases. Each test case begins with three integers 1<=N<=100 representing the total number of products, 1 <= M<= 10000 the money Iserlohn gets, and 1<=K<=10 representing the sneaker brands. The following N lines each represents a product with three positive integers 1<=a<=k, b and c, 0<=b,c<100000, meaning the brand’s number it belongs, the labeled price, and the value of this product. Process to End Of File.
     

    Output

    For each test case, print an integer which is the maximum total value of the sneakers that Iserlohn purchases. Print "Impossible" if Iserlohn's demands can’t be satisfied.
     

    Sample Input

    5 10000 3
    1 4 6
    2 5 7
    3 4 99
    1 55 77
    2 44 66
     

    Sample Output

    255
     题意:一个人去买运动鞋,他手里拥有的钱为m,运动鞋有k种品牌,每种品牌都可能有多双运动鞋,并且对于每种品牌,他都至少要买一双。给出n双运动鞋,运动鞋的品牌号,价格,价值分别为a,b,c。问他用那些钱去买运动鞋,能获得的最大价值是多少。
    思路:

    1. 要求每一组之中至少有一个被选中,和之前的最多有一个稍有区别,需要把题目再次细分。

    2. dp[j][w] 表示选定 j 个品牌并且花费w的最大价值,dp[j]w] 为正数表示状态存在,为负数表示状态不存在。

    3. dp[j][w] = max(dp[j][w], dp[j][w - s[i].b] +s[i].c);  第j类品牌有选择并且要选择第i件物品。(不选择第i件物品是相等的,所以略过转移方程)

       dp[j][w] = max(dp[j][w], dp[j - 1][w- s[i].b] +s[i].c);  第j类品牌前面没有选择并且要选择第i件物品。

    4. 由于状态是从 0 到 i 的且 dp[0][w] = 0,其他为 -INF。所以只有当第一类品牌的状态存在时,才能推导出来第二类品牌的存在状态,以此类推。

    5. 题目中有 2 个陷阱,一是可能会存在某品牌数量为 0 的情况,另外会存在费用或价格为 0 的情况,所以状态转移方程不能调换。

    AC代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define INF 0x7fffffff
     6 
     7 
     8 using namespace std;
     9 struct ks{
    10     int id;
    11     int b;
    12     int c;
    13 };
    14 ks s[103]={0};
    15 int dp[22][10005]={0};
    16 int number[13]={0};
    17 bool cmp(ks a1,ks a2){
    18     if(a1.id==a2.id)
    19         return a1.b<a2.b;
    20     else
    21         return a1.id<a2.id;
    22 }
    23 
    24 int main()
    25 {
    26     freopen("1.txt","r",stdin);
    27     int n,money,k;
    28     int a;
    29     int i,j;
    30     while(cin>>n>>money>>k){
    31         for(i=0;i<n;i++){
    32         cin>>s[i].id>>s[i].b>>s[i].c;//录入信息
    33     }
    34     sort(s,s+n,cmp);//按产品号和价格的升序进行排列
    35     for(i=0;i<k;i++){
    36         for(j=0;j<=money;j++)
    37             dp[i][j]=-INF;
    38     }
    39     dp[0][0]=0;//重置为0,为了第一重循环时的使用
    40     int w;
    41     j=0;
    42     for(i=0;i<n;i++){
    43         if(i&&s[i-1].id!=s[i].id)
    44             j++;//记录鞋的品牌种数
    45         for(w=money;w>=s[i].b;w--)//一定要从后向前推否则会取重的
    46             {//技巧性的实现了至少购买1双鞋子
    47             if(dp[j][w-s[i].b]!=-INF&&dp[j][w-s[i].b]+s[i].c>dp[j][w])//第j种鞋子已经买过了,并且买下第i双鞋子;
    48                 dp[j][w]=dp[j][w-s[i].b]+s[i].c;
    49             if(j&&dp[j-1][w-s[i].b]!=-INF&&dp[j-1][w-s[i].b]+s[i].c>dp[j][w])//第j种鞋子没有买过,并且买下第i双鞋子;
    50                 dp[j][w]=dp[j-1][w-s[i].b]+s[i].c;
    51         }
    52     }
    53     int ans=-1;
    54     for(i=money;i>=0;i--){
    55         if(ans<dp[j][i])
    56             ans=dp[j][i];
    57     }
    58     if(ans==-1||j<k-1)
    59         cout<<"Impossible"<<endl;
    60     else
    61         cout<<ans<<endl;
    62     }
    63     return 0;
    64 }
    View Code
  • 相关阅读:
    Easy-Mock模拟get接口和post接口实例
    本地部署Easy Mock
    用matplotlib和pandas绘制股票MACD指标图,并验证化交易策略
    向大家介绍我的新书:《基于股票大数据分析的Python入门实战》
    在windows环境里,用Docker搭建Redis开发环境(新书第一个章节)
    谈谈最近副业挣钱的感受,一要形成体系,二要找平台加持
    进大厂也就这回事,工作后2到3年进大厂操作指南
    最近录制视频挣钱的经过和体会
    以外派的身份进大厂,或许条程序员升级的途径
    面试中更多会考核相关技能的项目经验——再论程序员该如何准备面试
  • 原文地址:https://www.cnblogs.com/zhangchengbing/p/3354238.html
Copyright © 2020-2023  润新知