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. Rxjava2.0 链式请求异常处理

    使用Rxjava2.0的过程中,难免会遇到链式请求,而链式请求一般都是第一个抛异常,那么后面的请求都是不会走的.现在来讨论一下链式请求的一种异常处理方法.例如: 一个登录-->通过登录返回的to ...

  2. Android 文件存储 和 权限管理

    转载请标明出处: :http://blog.csdn.net/huaiyiheyuan/article/details/52473984 android SD卡主要有两种存储方式 Internal . ...

  3. win64 QT(VS2013) 配置 CGAL+libQGLViewer

    网上讲的都不是太全 我来说说把~ 首先感谢这位大神: chenwk891:http://blog.csdn.net/chenwk891/article/details/42171495#reply 还 ...

  4. Android Exception 11(baidumapsdk(15405): Authentication Error errorcode: 102 uid)

    08-12 16:06:12.622: E/baidumapsdk(15405): Authentication Error errorcode: 102 uid: null appid -1 msg ...

  5. Linux su命令参数及用法详解--Linux切换用户命令

    建议大家切换用户的时候 使用  su -  root  这样,否则可能发现某些命令执行不了 关于su .su - 及 sudo的区别 请往下看 1.命令作用 su的作用是变更为其它使用者的身份,超级用 ...

  6. eclipse项目 乱码

  7. 前端JS脚本将网页表格导出为Excel

    话不多说,上代码! <!DOCTYPE> <html> <head> <title>Excel Test</title> </head ...

  8. C# OO(初级思想)

    继承,多态,封装 在C#中,为了能够合理描述自然界的规律,面向对象的编程引入了继承的概念,是面向对象编程中最重要的概念之一,定义了如何根据现有的类创建新类的过程. 继承:一个类派生出来的子类具有这个类 ...

  9. 几种TCP连接中出现RST的情况(转载)

    TCP RST 网络 linux 目录[-] 1 端口未打开 2 请求超时 3 提前关闭 4 在一个已关闭的socket上收到数据 总结 参考文献: 应该没有人会质疑,现在是一个网络时代了.应该不少程 ...

  10. HDOJ 5418 Victor and World 状压DP

    水状压DP Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java ...