前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LintCode 1917. 切割剩余金属

LintCode 1917. 切割剩余金属

作者头像
Michael阿明
发布2021-02-19 12:53:57
4610
发布2021-02-19 12:53:57
举报
文章被收录于专栏:Michael阿明学习之路

文章目录

1. 题目

描述

金属棒工厂的厂长拥有 n 根多余的金属棒。 当地的一个承包商提出,只要所有的棒材具有相同的长度(用 saleLength 表示棒材的长度),就将金属棒工厂的剩余棒材全部购买。 厂长可以通过将每根棒材切割零次或多次来增加可销售的棒材数量,但是每次切割都会产生一定的成本(用 costPerCut 表示每次切割的成本)。 等所有的切割完成以后,多余的棒材将被丢弃,没有利润。

金属棒工厂的厂长获得的销售总利润计算公式如下:

代码语言:javascript
复制
totalProfit = totalUniformRods * saleLength * salePrice - totalCuts * costPerCut

其中 totalUniformRods 是可销售的金属棒数量, salePrice 是承包商同意支付的每单位长度价格, totalCuts是需要切割棒材的次数。

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
样例 1
输入:
1
10
[30,59,110]
输出:
1913

https://www.lintcode.com/problem/cutting-metal-surplus/description

2. 解题

  • 暴力法
代码语言:javascript
复制
class Solution {
public:
    /**
     * @param costPerCut: integer cost to make a cut 
     * @param salePrice: integer per unit length sales price 
     * @param lengths: an array of integer rod lengths 
     * @return: The function must return an integer that denotes the maximum possible profit. 
     */
    int maxProfit(int costPerCut, int salePrice, vector<int> &lengths) {
        // write your code here
        int maxprofit = 0, profit = 0;
        for(int L = 1; L <= 10000; ++L)
        {
            profit = 0;
            for(auto l : lengths)
            {
                int n = l/L;
                int cut = 0;
                if(l%L == 0)
                    cut = n-1;
                else
                    cut = n;
                profit += n*salePrice*L - cut*costPerCut;
            }
            maxprofit = max(maxprofit, profit);
        }
        return maxprofit;
    }
};

101ms C++


本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/01/29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. 题目
  • 2. 解题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档