• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > 贪心算法 WOODEN STICKS 实例代码

贪心算法 WOODEN STICKS 实例代码

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了贪心算法实例,c语言贪心算法实例,java贪心算法实例,贪心算法,贪心算法的基本思想等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

Problem Description
There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:
(a) The setup time for the first wooden stick is 1 minute. (b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l<=l' and w<=w'. Otherwise, it will need 1 minute for setup.
You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 2 minutes since there is a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).

Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1<=n<=5000, that represents the number of wooden sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2, ..., ln, wn, each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.

Output
The output should contain the minimum setup time in minutes, one per line.

Sample Input
3 5 4 9 5 2 2 1 3 5 1 4 3 2 2 1 1 2 2 3 1 3 2 2 3 1

Sample Output
2 1 3

题意:

我们要处理一些木棍,第一根的时间是1分钟,另外的,在长度为l,重为w的木棍后面的那根的长度为l', 重量w',只要l <=l' 并且w <= w',就不需要时间,否则需要1分钟,求如何安排处理木棍的顺序,才能使花的时间最少。

思路:

贪心算法。先把这些木棍按长度和重量都从小到大的顺序排列。cl和cw是第一根的长度和重量,依次比较后面的是不是比当前的cl,cw大,是的话把标志flag设为1,并跟新cl,cw。比较完后,再从前往后扫描,找到第一个标志位为0的,作为是第二批的最小的一根,计数器加一。把它的长度和重量作为当前的cl,cw,再循环往后比较。直到所有的都处理了。

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C 语言插入排序算法及实例代码
  • C语言选择排序算法及实例代码
  • C语言中使用快速排序算法对元素排序的实例详解
  • C语言的冒泡排序和快速排序算法使用实例
  • 贪心算法的C语言实现与运用详解
  • C语言的数字游戏算法效率问题探讨实例
  • c语言中使用BF-KMP算法实例
  • 贪心算法 WOODEN STICKS 实例代码

相关文章

  • 2017-05-28C++获得文件状态信息的方法
  • 2017-05-28C++模板二段名字查找方法
  • 2018-08-06VS2010 C++程序调用C#库
  • 2017-05-28哈夫曼的c语言实现代码
  • 2017-05-28优先队列(priority_queue)的C语言实现代码
  • 2017-05-28c语言socket多线程编程限制客户端连接数
  • 2017-05-28C语言中多维数组的内存分配和释放(malloc与free)的方法
  • 2017-05-28C++实现简单的扫雷游戏(控制台版)
  • 2017-05-28C++中char*转换为LPCWSTR的解决方案
  • 2017-05-28Cocos2d-x学习笔记之CCScene、CCLayer、CCSprite的默认坐标和默认锚点实验

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • c++如何分割字符串示例代码
    • 基于C++实现的各种内部排序算法汇总
    • C语言中的内联函数(inline)与宏定义(#define)详细解析
    • 使用C语言递归与非递归实现字符串反转函数char *reverse(char *str)的方法
    • 希尔排序算法的C语言实现示例
    • 详解C语言中index()函数和rindex()函数的用法
    • C++实现打印两个有序链表公共部分的方法
    • 浅谈返回函数内部new分配的内存的引用
    • C/C++程序编译流程详解
    • C++多线程编程简单实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有