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. CDN新应用和客户

    目前的CDN配置服务主要应用于证券.金融保险.ISP.ICP.网上交易.门户网站.大中型公司.网络教学等领域.另外在行业专网.互联网中都可以用到,甚至可以对局域网进行网络优化.利用CDN,这些网站无需 ...

  2. UIImagePickerController 视频录制操作,视频大小,时间长度

    一:使用 iOS 系统 UIImagePickerController 获取视频大小 获取视频长度 - (void)viewDidLoad { [super viewDidLoad]; // Do a ...

  3. [Swift A] - 实战-豆瓣电台总结

    最近在学Swift,也是刚刚开始.这里对自己最近所学做个简单的总结:视频和代码都在下面 http://pan.baidu.com/s/1sjHd5qX 1.String和NSString的不同 Swi ...

  4. 准备你的Adempiere开发环境(1)- 编译

    1. 安装JDK 1.6. 2. 安装Eclipse IDE for Java EE Developers. 3. 导入adempire-360lts: 4. 关闭adempiere-360lts的B ...

  5. 批量删除git分支

    本篇文章由:http://xinpure.com/bulk-delete-git-branching/ 批量删除git分支 使用 git 时候,经常会发现,不知不觉就创建了大量的分支.那么,麻烦事就来 ...

  6. SQL数据表插入随机数(转)

    declare @T TABLE (id int identity(1,1),[Name] nvarchar(20), Randnum int) insert @T ([Name]) select ' ...

  7. LNMP架构二

    Nginx默认虚拟主机 1.首先修改nginx.conf文件,删除server及下面的,在http最后添加include vhost/*.conf; (指定虚拟主机目录,并读取以.conf结尾的文件) ...

  8. win10 清理winsxs文件夹

    dism /online /cleanup-image /startcomponentcleanup /resetbase

  9. MYSQL备份与恢复精华篇

    数据备份原理 数据备份属于数据容灾保护中的内容,所有的数据备份系统设计都基于这五个元素,备份源.备份目标.传输网络.备份引擎和备份策略.用户按照需要制定备份策略,使用定时任务执行备份脚本,使用备份引擎 ...

  10. 点滴积累【C#】---错误日志记录到txt文本里。

    效果: 描述:将系统中的错误信息,try catch到日志里面. 代码: [后端代码] using System; using System.Collections.Generic; using Sy ...