题解 | #购物单#

购物单

https://www.nowcoder.com/practice/f9c6f980eeec43ef85be20755ddbeaf4

#include <iostream>
#include <vector>
#include<cmath>
using namespace std;
#define max(N1,N2) N1>N2?N1:N2
struct Thing {
  public:
    int price;
    int target;
    int Value;
    int sub1 = -1;
    int sub2 = -1;
};

int main() {
    int money, n;
    cin >> money >> n;
    int len = n;
    vector<Thing> things;
    for (int i = 0; i < len; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        Thing thing;
        thing.price = a;
        thing.target = c;
        thing.Value = a * b;
        things.push_back(thing);
    }
    for(int i = 0; i < len; i++)
    {
        if(things[i].target!=0)
        {
            if(things[things[i].target-1].sub1==-1)
                things[things[i].target-1].sub1 = i;
            else
                things[things[i].target-1].sub2 = i;

        }
    }
    vector<vector<int>> f;//f[i][j]初始化
    for (int i = 0; i < n + 1; i++) {
        vector<int> ins(money + 1, 0);
        f.push_back(ins);
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= money; j++) {
            f[i][j] = f[i - 1][j];
            if (things[i - 1].target != 0) 
            {
                continue;
            }
            if(j >= things[i - 1].price) 
            {
                f[i][j] = max(f[i][j],f[i - 1][j - things[i - 1].price] + things[i - 1].Value);
            }
            if(things[i - 1].sub1!=-1 &&j >= things[things[i - 1].sub1].price + things[i - 1].price) 
            {
                f[i][j] = max(f[i][j],f[i - 1][j - things[i - 1].price - things[things[i - 1].sub1].price] + things[i- 1].Value + things[things[i - 1].sub1].Value);
            }
            if(things[i - 1].sub2!=-1 &&j >= things[things[i - 1].sub2].price + things[i - 1].price) 
            {
                f[i][j] = max(f[i][j],f[i - 1][j - things[i - 1].price - things[things[i - 1].sub2].price] + things[i- 1].Value + things[things[i - 1].sub2].Value);
                }
            if(things[i-1].sub2!=-1 &&j>=things[things[i - 1].sub2].price + things[i - 1].price+things[things[i - 1].sub1].price)
            {
                f[i][j] = max(f[i][j],f[i - 1][j - things[i - 1].price - things[things[i - 1].sub2].price-things[things[i - 1].sub1].price] + things[i- 1].Value + things[things[i - 1].sub2].Value+things[things[i - 1].sub1].Value);
            }
        }

    }
    cout<<f[n][money];








}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

09-16 17:32
门头沟学院 Java
顺顺超爱学:1.熟悉Java编程语言,熟悉集合,多线程,IO,反射等核心知识,了解线程池,ThreadLocal等进阶知识; 2.熟悉Mysql数据库,熟练使用sql,熟悉索引,存储引擎,事务原理,MVCC,锁机制,了解sql优化; 3.熟悉Redis缓存,了解常见的数据类型,了解缓存常见问题及其解决方案,了解使用Redis实现的分布式锁方案; 4.熟悉Javaweb开发框架,熟悉spring,springmvc,mybatis等,了解IOC,AOP等; 5.熟悉微服务开发框架,熟悉SpringBoot,SpringCloud,包括Nacos,OpenFeign,Gateway等核心组件; 6.熟悉Rabbitmq消息队列,熟练使用消息模型,了解架构,消息可靠性,死信队列,延迟消息等;
点赞 评论 收藏
分享
09-22 09:42
门头沟学院 Java
牛客37185681...:马德,我感觉这是我面过最恶心的公司,一面是两个女hr,说什么实习前几个月属于试用期,试用期过了才能转成正式实习生,我***笑了,问待遇就是不说,问能不能接受全栈,沙币公司
如果可以选,你最想去哪家...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务