Wooden Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10423    Accepted Submission(s): 4287

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
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
 
Source
 
Recommend
We have carefully selected several similar problems for you:  1050 1052 1045 1053 1789 

 
  水题。
  先对木材长度 l 排序,在对 w 依次处理,将第一个递增序列筛选出来,筛选出来的元素赋值为-1,再将第二个递增序列筛选出来,同样标记-1 ,直到w序列全部为-1,代表筛选完成。输出记录下的递增序列的个数就是结果。
  还可以用贪心做,有时间再做一遍。
  代码:
 #include <iostream>

 using namespace std;
struct stick{
int l,w;
}s[];
int main()
{
int T;
cin>>T;
while(T--){
int n;
cin>>n;
for(int i=;i<=n;i++)
cin>>s[i].l>>s[i].w;
//对l排序
for(int i=;i<=n-;i++)
for(int j=;j<=n-i;j++){
if(s[j].l>s[j+].l){
int t;
t=s[j].l;s[j].l=s[j+].l;s[j+].l=t;
t=s[j].w;s[j].w=s[j+].w;s[j+].w=t;
}
}
int sum=; //递增序列的个数
//对w序列进行筛选
while(){
int i;
for(i=;i<=n;i++){ //如果全部为-1,则退出循环
if(s[i].w!=-)
break;
}
if(i>n) break;
sum++;
int num=;
for(i=;i<=n;i++){
if(num== && s[i].w!=-){
num=s[i].w;
s[i].w=-;
}
else if(s[i].w!=- && s[i].w>=num){
num=s[i].w;
s[i].w=-;
}
}
}
cout<<sum<<endl; }
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1051:Wooden Sticks(水题,贪心)的更多相关文章

  1. HDU 1051 Wooden Sticks (贪心)

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  2. HDU 1051 Wooden Sticks 贪心||DP

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. HDU - 1051 Wooden Sticks 贪心 动态规划

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)    ...

  4. HDU 1051 Wooden Sticks 贪心题解

    本题一看就知道是最长不减序列了,一想就以为是使用dp攻克了. 只是那是个错误的思路. 我就动了半天没动出来.然后看了看别人是能够使用dp的,只是那个比較难证明其正确性,而其速度也不快.故此并非非常好的 ...

  5. HDU 1051 Wooden Sticks 造木棍【贪心】

    题目链接>>> 转载于:https://www.cnblogs.com/Action-/archive/2012/07/03/2574800.html  题目大意: 给n根木棍的长度 ...

  6. hdu 1051 wooden sticks (贪心+巧妙转化)

    #include <iostream>#include<stdio.h>#include<cmath>#include<algorithm>using ...

  7. HDU 1051 Wooden Sticks

    题意: 有 n 根木棒,长度和质量都已经知道,需要一个机器一根一根地处理这些木棒. 该机器在加工过程中需要一定的准备时间,是用于清洗机器,调整工具和模板的. 机器需要的准备时间如下: 1.第一根需要1 ...

  8. HDU 1051 Wooden Sticks【LIS】

    题意:给出n个木头的重量wi,长度li,如果满足w[i+1]>=w[i]且l[i+1]>=l[i],则不用耗费另外的加工时间,问至少需要多长时间加工完这些木头. 第一次做这一题目也没有做出 ...

  9. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. RxJava异步请求加载状态控制

    在我看来,RxJava最大的特点就是异步,无论你是解析复杂的数据或是IO操作,我们都可以利用它内置的线程池进行线程间的调度,简单的使用 subscribeOn(Schedulers.io()).doO ...

  2. 运行maven pom.xml文件后编译环境变为jdk1.5

    idea中运行pom.xml文件后,将编译环境变成了1.5,造成一系列的编译问题很是不方便. 以下是解决方法: 在"pom.xml"里加入如下代码: <properties& ...

  3. 织梦dedecms修改include和plus重命名提高安全性防漏洞注入挂马

    织梦dedecms是新手站长使用得比较多的一个建站开源程序,正因如此,也是被被入侵挂马比较多的程序.下面就来跟大家说一下怎么重新命名dedecms的include文件夹以及plus文件夹来提高网站的安 ...

  4. CentOS忘记root密码解决办法

    如果是忘记普通的用户密码,那还好说,用root登录命令行界面,修改即可. 但如果是root的话,那就需要这样修改了.   记住,这几篇文章说的都是对的,只是我复杂了,实际只需要将光标移到最后" ...

  5. PadLeft函数

    string num=12 num.PadLeft(4, '0'); //结果为为 '0012' 看字符串长度是否满足4位,不满足则在字符串左边以"0"补足

  6. zookeeper(四):核心原理(Watcher、事件和状态)

    zookeeper主要是为了统一分布式系统中各个节点的工作状态,在资源冲突的情况下协调提供节点资源抢占,提供给每个节点了解整个集群所处状态的途径.这一切的实现都依赖于zookeeper中的事件监听和通 ...

  7. atitit.验证码识别step2------剪贴板ClipBoard copy image图像 attilax总结

    atitit.验证码识别step2------剪贴板ClipBoard copy image图像 attilax总结 剪贴板(ClipBoard)是内存中的一块区域,是Windows内置的一个非常有用 ...

  8. atitit.软件设计模式大的总结attialx总结

    atitit.软件设计模式大的总结attialx总结 1. 设计模式的历史3 2. 设计模式的数量(253个)3 3. 设计模式的结构4 3.1. 应用场景and条件Context4 3.2. Pro ...

  9. android逆向分析之smali语法

    一 .smali数据类型 1.Dalvik字节码 Davlik字节码中,寄存器都是32位的,能够支持任何类型,64位类型(Long/Double)用2个连续的寄存器表示: Dalvik字节码有两种类型 ...

  10. [PIC32--IDE]Microchip PIC32开发环境的搭建

    问题描写叙述 PIC32是Microchip推出的32 bit MCU,其内核是MIPS架构的,MIPS也是属于RISC指令集的,好像ARM的指令集还在RISC指令集中做了一些扩充,而MIPS应当是更 ...