POJ 1065 Wooden Sticks(贪心,)

POJ 1065-Wooden Sticks 贪心问题 <br />题目来源: http://acm.pku.edu.cn/JudgeOnline/problem?id=1065<br /> <br /> <br />解题报告: <br /> <br />本题采用贪心算法, 先从一堆sticks中找到长度最小的stick, 对长度相等的, 找到weight最小的,  找到后, 设长度为l, 重量为w, 然后, 从剩下的sticks中找到长度、重量都大于等于l和w的sticks中长度及重量最小的stick,再依次找下去,如果不存在,则就直接找长度、重量最小的sti 阅读详情

 

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 ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .

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 2n 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都大于等于前一根,那就不用花时间。简单来说,就是让尽量多木棒能        接在其他木棒后面,贪心题。

解题思路:

        sort排序自定义以l或者w递增排序,然后按正序判断后面是否有木棒可以免费接,因为我们一开始已经以l(w)作排序,所以直接以w(l)比较,如果可以接上,更新刚接上的木棒的l(w),因为接上这根木棒,后面可能还有木棒能继续接。每一次判断前都要判断是否已经接过了(1 用标记数组标记是否用过 2 数据清0,判断数据是否为0)

重点: 1:标记判断

           2:排序对比,尾部更新。

下面附上代码:

 

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
struct mubang
{
    int l;
    int w;
}mb[5005];
int cmp(mubang a,mubang b)
{
    if(a.l<b.l)
        return 1;
    if(a.l==b.l&&a.w<b.w)
        return 1;
    return 0;
}
int main()
{
     int n,i,j,cas,ok[5005],ans=0,wei;
     cin>>cas;
     while(cas--)
     {
         ans=0;
         memset(ok,0,sizeof(ok));
         cin>>n;
         for(i=0;i<n;i++)
             cin>>mb[i].l>>mb[i].w;
             sort(mb,mb+n,cmp);
         for(i=0;i<n;i++)
         {
            if(ok[i]==1)
                continue;
                ans++;
            wei=mb[i].w;
            for(j=i+1;j<n;j++)
            {
                if(ok[j]==1)
                    continue;
                if(mb[j].w>=wei)
                {
                    ok[j]=1;
                    wei=mb[j].w;
                }
            }
         }
         cout<<ans<<endl;
     }
    return 0;
}

 

 

 

 

 

正交试验minitab 1.创建正交试验 2.根据自己的实验选择水平数和因子数,并选定一个正交表 3.得到正交表 4.填写实验结果 5.进行正交分析 点击结果,点击分析 6.得到分析结果 7.方差分析 选择响应变量与因子,点击确定 得到方差分析的结果 ... 阅读详情

相关推荐

小案例:利用图床自动化批量上传图片并获取图片链接

用python爬虫写了一个小脚本,用来自动上传图片到图床,然后返回链接,我们只需直接复制链接就可以。(需要安装Requests库) 因为不同图床需要提交的POST表单不一致,所以在这里我用ImgURL图床做示范,如果大家有需求,可自行修改代码。使用脚本的时候,我们只需把需要上传的图片和这个脚本放在同一个文件夹中,然后运行脚本,稍作等待,看到提示时就可以在脚本运行文件夹中发现一个名为“图片链接地址.txt”的文件,里面就是图片链接啦! 四、图片链接地址.txt(单张图片):...

采菊东篱下,Python满乾坤! 4409

ACM题解——贪心专题——木头加工

ACM题解——贪心之木头加工 题目描述 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

Biangbangbing的博客 644

SRA数据库

简介 序列读取存档(sequence Read Archive,SRA)数据是可通过多个云提供商和NCBI服务器获得的,它是最大的可公开获得的高通量测序数据存储库。 SRA接受来自生活各个部门以及宏基因组学和环境调查的数据。 SRA存储原始测序数据和比对信息,以提高可重复性并通过数据分析促进新发现。 NCBI网站储存二代测序原始数据的数据库 SRA官网 从Entrez搜索结果中下载SRA序列 例如查找SRA Entrez中BALB / c小鼠淋巴结组织的RNA-Seq记录 获取搜索结果 SRA se

qq_44520665的博客 5907

POJ - 1065 Wooden Sticks

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 som

Lakersman的博客 662

Dynamics 365 On-Premise Server v9.0部署体验

期盼已久的Dynamics 365 On-Premise的版本升级终于在11月初姗姗来迟。 11月2日起已经可以在官方渠道下载正式版的Dynamics 365 Server v9.0安装文件,版本号9.0.2,随手附上中文版安装文件下载地址。 想必已经有不少小伙伴开始尝试各种升级、安装了吧。 Dynamics 365 On-Premise的安装过程,度娘一下已经有很多了,这里就不赘述了,只是...

Roger的专栏 2139

SPSS新手教程—两步聚类之结果解读

在《详解SPSS两步聚类之参数设置》一文中,我们已经了解了两步聚类的优点、分析原理,以及参数设置的技巧。 在本节中,会对IBM SPSS Statistics聚类后的结果进行解读,其中会涉及到最终聚类的结果、聚类的质量、变量重要性、聚类特征的解读。 如图1所示,我们先来回顾下本例数据在参数设置面板中的详细设置。 图1:二阶聚类设置 一、数值结果 根据上述参数设置,得到如下的数值分析结果。 首先看到自动聚类结果,如图2所示为聚类的透视表结果,展示了不同聚类数目下的BIC、BIC变化量、BIC变

nekonekoboom的博客 3万+

杭电OJ——1051 Wooden Sticks

Wooden Sticks 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 o

李树花开,风中摇曳 8343

【Maxwell16.0】实战指南:从DXF导入到MotorCAD联合仿真的关键技巧

本文详细解析了Maxwell 16.0与MotorCAD进行联合仿真的核心流程与关键技巧。重点阐述了从DXF模型导入、几何修复、一极一槽模型剪裁,到坐标系对齐、MotorCAD参数匹配及结果验证的全过程,旨在帮助工程师高效打通联合仿真链路,规避常见错误,提升电机设计效率。

weixin_29062865的博客 715

贪心算法H题

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 sti.

qq_50960478的博客 499

用Python实现古诗词填字游戏(三)

古诗词做填字游戏的第三部分代码,也是最核心的部分。实现对诗文和关键字的选定,并确定文字的输出位置。

PosPro的专栏 6117

windows系统下,如何为comfyui如何安装triton

​ 有些加速插件、模型在安装的时候,比如说sageattention的安装,就需要提前安装triton这个包,不过这个包有点特殊,windows下略微不好装,经常会装失败。 🎠如何安装 之前也有人介绍过,要到github上找个wheel文件,找到对应的版本安装下就好了。 问题是:https://github.com/woct0rdho/triton-windows/releases 这个git地址上,已经找不到wheel文件了... ​

聚梦小课堂 4497

题解:Wooden Sticks(贪心)

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 ...

晚安( ̄o ̄) . z Z 955

BMW Trojan 样本分析

仅用于链接已经删却但是需要方便查阅的原文http://blog.csdn.net/zirconsdu/article/details/7997470 转载请注明出处http://blog.csdn.net/zirconsdu/article/details/8153813。 一些具体数据结构和具体路径隐去。   BMW木马样本分析 摘要 2011年底和某Hunter的谈话涉及到当时新检

zirconsdu的专栏 3911

POJ 1065 Wooden Sticks 【贪心】

题目链接 题意 给n个整数对,定义数对间的大于关系是(w1,l1)≤(w2,l2)↔w1≤w2andl1≤l2,求用这些数对最少能组成几组非递减序列 分析 LIS的变形,但考虑到原来给的这些数对并没有顺序,可以随便选择,因此没有必要用DP求LIS。可以倒序排序(先按第一个数排序,再按第二个数排序)后,贪心,即从左到右遍历,选择当前还没有被选择的数对中的最小...

babao9492的博客 126

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

poj 1065 Wooden Sticks(贪心算法)

题目:来自http://poj.org/problem?id=1065 描述:有一些木棍,每个有长度和重量,要求把这些木棍排成若干两个属性值均不下降的序列。问至少要分为多少个序列。且要保证排出来的子序列数最少。 例如:     ( 9 , 4 ) ,( 2 , 5 ) ,( 1 , 2 ) ,( 5 , 3 ),( 4 , 1 )可以排成这样                     ( 4

继续微笑lsj 2301

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

poj 1065 Wooden Sticks (贪心)

题目:http://poj.org/problem?id=1065 思路:先按照L排序转换成一维,在对W贪心即可。 #include #include using namespace std; const int MAX=5001; struct wooden{ int l,w,flag; }wd[MAX]; bool Cmp(wooden x,wooden y){ if(x.l!=y.l

R_xiaozhu_Q的专栏 1184

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

poj 1065 Wooden Sticks(diworth定理)

题意:有n条木棒,给出它们每条的l和w,用一台机器对它们进行加工,如果机器正加工的木条,与在它之前加工的木块有关系:l <= l'和w <= w',则机器不用准备时间,否则需准备1分钟。问加工完全部木棒,机器最少需要准备多久。 思路:每次以某一点为起点,向后贪心地走。(虽然是求小链覆盖,但是不用上网络流) 或者利用diworth定理,最长反链 = 最小链覆盖(本...

ehdhg13455的博客 346

POJ 1065 Wooden Sticks 贪心

题目链接:http://poj.org/problem?id=1065 题目大意:需加工n个木棍,每个木棍有长度L和重量W两个参数,机器处理第一个木棍 用时1分钟,如果当前处理的木棍为L和W,之后处理的木棍L' 和W' 满足:L 题目解析:贪心策略,将木棍按照长度L从小到大排序,然后通过枚举每个木棍的重量,判断W有多少个上升的序列,即为最终所求。时间复杂度为O(n*n)。

Christine的专栏 1008

POJ 1065 Wooden Sticks 解题报告-用动态规划方法解决(LIS变式)

POJ 1065 Wooden Sticks 解题报告-用动态规划方法解决(LIS变式)DescriptionThere 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 mach

Jeza SS 748

Wooden Sticks(贪心)

Wooden Sticks. win the wooden spoon:成为末名。 题目地址:http://poj.org/problem?id=1065 There is a pile of n wooden sticks. a woodworking machine:木工机器。 setup time:启动时间。 my codes: #include<iostr...

banteng7670的博客 117

[POJ1065] Wooden Sticks(贪心)

题目:http://poj.org/problem?id=1065 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于 第i个处理的木棒,那么将不会耗费时间,否则需要消耗一个单位的时间。因为急着去约会,C小加想在最短的时间内把木棒处理完,你能告诉他应该怎样做吗? 输入 第一行是一个整数T,表示输入数据一共有T组。 每组测试数据的第一行是一个整数N(1<=N<=5000),表示..

REWINE 863

POJ1065 Wooden Sticks(贪心||DP)

题意: 一些有长度l和重量w的木棍要装在机器上,如果前一个木棍长度和重量均小于等于当前木棍,装上机器不耗时,否则时间+1,问全装上最少时间。 要点: 可以用贪心或DP,贪心就是先按长度从小到大排好,再根据重量,用一个d数组标记是否用过,从一根木棍出发,往后找重量大于等于它的木棍,将它的d标记为1,再以这个新木棍为起点再往后找,遍历一遍后再从d为0的木棍出发,看能遍历几次就是最小时间。

SeasonJoe的博客 492

POJ 1065 Wooden Sticks

思路: 排序后依次进行贪心选择; 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 struct node { 5 int l, w; 6 bool isUsed; 7 }nodes[5000];//木棍数组 8 bool cmp(const node&...

qq_43119424的博客 204

poj 1065 Wooden Sticks

哈尔滨理工大学软件工程专业08-7李万鹏原创作品,转载请标明出处http://blog.csdn.net/woshixingaaa/archive/2010/05/14/5592992.aspx http://acm.pku.edu.cn/JudgeOnline/problem?id=1065 类型: 贪心 题意: 有一个机器,它处理木块时,如果满足正在被处

kernel hacker 修炼之道——李万鹏 1596

POJ1065 Wooden Sticks ACM解题报告(暴力贪心)

这题就是那个3636的兄弟啊,这题数据量还小,暴力直接16MS,不需要二分了(ps.每次我二分都要思考半天) 这题贪心的方法与3636不同,是两个量都升序。因为这题只要大于等于当前的即可不+1; 这题主要是给出贪心的小证明吧。升序排列,第一个木棍为一组,然后和他第一个参数相等木棍都与他一组(因为升序排列而且可以取=) 如果你说这个不是最优解,可以将第一个参数相等的所有木棍看成集合p1,p2,

Miracle_ma的专栏 1535

poj1065-Wooden Sticks

题目 有很多小木棍需要机器处理。每个小木棍有重量和长度两个属性。不断把小木棍放入机器中,如果小木棍\(a\)放完后放入小木棍\(b\),那么如果\(a.weight<=b.weight\ and\ a.length<=b.length\),机器就可以以0的时间处理完这根木棍,否则需要花1的时间初始化。最开始也需要一次初始化。给出所有小木棍的信息,求一共需要多久处理完所有的小木...

dengtan9179的博客 223
上一篇: hdu 1028 Ignatius and the Princess III(打表,后期进阶补->找规律和dp)
下一篇: HDU - 2037 今年暑假不AC(贪心,最长连接)
Zero丶圈圈
博客等级 码龄9年 6粉丝 46原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值