woodcut

http://www.lintcode.com/en/problem/wood-cut/#


二分答案,贪心验证,具有单调性


class Solution {
public:
    /**
     *@param L: Given n pieces of wood with length L[i]
     *@param k: An integer
     *return: The maximum length of the small pieces.
     */
    int woodCut(vector<int> L, int k){
        if(L.empty()) return 0;
        LL low=0, high=*max_element(L.begin(), L.end());
        while(low<high){
            if(low+1==high){
                LL ans=0;
                for(auto e: L){
                    ans+=e/high;
                }
                if(ans>=k) return high;
                else return low;
            }
            LL mid=(low+high)/2;
            LL ans=0;
            for(auto e: L){
                ans+=e/mid;
            }
            if(ans>=k)  low=mid;
            else high=mid-1;
        }
        return low;
    }
}S;

introsort C++ STL 没有最坏情况

timsort msort isort 综合

dual pivot qsort JDK7 arrays.sort 里面采用 任然有最坏n^2情况

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C# 二维优化下料算法的代码示例: ```csharp using System; using System.Collections.Generic; namespace WoodCutting { class Program { static void Main(string[] args) { List<WoodPiece> woodPieces = new List<WoodPiece>(); woodPieces.Add(new WoodPiece(4, 8)); woodPieces.Add(new WoodPiece(5, 7)); woodPieces.Add(new WoodPiece(6, 6)); woodPieces.Add(new WoodPiece(8, 4)); woodPieces.Add(new WoodPiece(10, 3)); List<WoodCut> woodCuts = OptimizeWoodCutting(woodPieces, 10, 20); Console.WriteLine("Optimized Wood Cuts:"); foreach (WoodCut cut in woodCuts) { Console.WriteLine("Wood Piece: {0}x{1}", cut.Width, cut.Height); Console.WriteLine("Cut Location: ({0}, {1})", cut.X, cut.Y); } } static List<WoodCut> OptimizeWoodCutting(List<WoodPiece> woodPieces, int boardWidth, int boardHeight) { List<WoodCut> woodCuts = new List<WoodCut>(); int x = 0; int y = 0; foreach (WoodPiece piece in woodPieces) { if (x + piece.Width <= boardWidth && y + piece.Height <= boardHeight) { woodCuts.Add(new WoodCut(piece.Width, piece.Height, x, y)); x += piece.Width; } else { x = 0; y += piece.Height; if (y > boardHeight) { throw new Exception("Board is not big enough to fit all wood pieces."); } woodCuts.Add(new WoodCut(piece.Width, piece.Height, x, y)); x += piece.Width; } } return woodCuts; } } class WoodPiece { public int Width { get; set; } public int Height { get; set; } public WoodPiece(int width, int height) { Width = width; Height = height; } } class WoodCut { public int Width { get; set; } public int Height { get; set; } public int X { get; set; } public int Y { get; set; } public WoodCut(int width, int height, int x, int y) { Width = width; Height = height; X = x; Y = y; } } } ``` 在这个例子中,我们有一个 `WoodPiece` 类来表示木材,一个 `WoodCut` 类来表示切割的木材块,以及一个名为 `OptimizeWoodCutting` 的函数来执行切割操作。在这个函数中,我们将每个木材块与木板的尺寸进行比较,并将其放置在可用空间内。如果木材块的宽度大于木板的宽度,则将其放置在下一行的开头。 请注意,此代码示例仅适用于基本的二维优化下料算法。实际应用中,您可能需要考虑更多因素,例如木材的形状和大小、切割顺序以及更高级的算法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值