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. 启动ActiveMQ时报端口被占用异常

    配置好ActiveMQ后,前几次都启动成功.有一天启动时发现启动不成功,查看报错日志发现出现如下提示: Failed to start Apache ActiveMQ (localhost, ID:* ...

  2. ES6 数值扩展

    1.Number.isNan 和 Number.isFinite Number.isNaN()用来检查一个值是否为NaN Number.isNaN(NaN) // true Number.isNaN( ...

  3. 细说linux IPC(一):基于socket的进程间通信(上)

        [版权声明:尊重原创.转载请保留出处:blog.csdn.net/shallnet 或 .../gentleliu,文章仅供学习交流,请勿用于商业用途]     在一个较大的project其中 ...

  4. windows域相关

    查看域角色: netdom query fsmo

  5. ACE中静态实例管理方式

    ACE中的很多类使用了单例模式,为了便于管理单例对象,ACE使用了一个组件——ACE_Framework_Component来专门管理. 我们以ACE_Reactor这个单例类的创建和释放为例. 1. ...

  6. Shell 基本运算符(转)

    Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 ...

  7. 阿里云 oss python3 样例

    阿里云的oss SDK又是不支持python3,头疼头疼. 本想改一改它的SDK,让它支持python2+python3,无奈里面大量的代码使用不带括号的print.工作量恐怖. 幸好oss的使用很e ...

  8. jBoss修改端口号

    http://blog.csdn.net/ghost_t/article/details/5708991 jBoss版本: jboss-5.1.0.GA jboss-6.0.0.Final     j ...

  9. jboss 的debug启动4法

    http://xo-tobacoo.iteye.com/blog/684946方式一: 使用myeclipse,全自动化,不再赘述 方式二: eclipse下使用server工具,部署后使用debug ...

  10. 后台dubug有值且sql也打印出来执行了但是前台就是查不到数据

    记录在sturts2里面 摔得最深的一次 public String queryJoinAccount(){ //用来存储分页的数据 pageMap=new HashMap<String, Ob ...