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

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 大意:
  

描述

C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于

第i个处理的木棒,那么将不会耗费时间,否则需要消耗一个单位的时间。因为急着去约会,C小加想在最短的时间内把木棒处理完,你能告诉他应该怎样做吗?

输入

第一行是一个整数T,表示输入数据一共有T组。

每组测试数据的第一行是一个整数N(1<=N<=5000),表示有N个木棒。接下来的一行分别输入N个木棒的L,W(0 < L ,W <= 10000),用一个空格隔开,分别表示

木棒的长度和质量。

输出

处理这些木棒的最短时间。

先将木头按长度从小到大排列,然后假设从第一个木头开始,查询第2~n个木头,把不用时间的全部标记,再找到下一个未标记的木头为开始,时间加一,再将这块木头后面不用时间的全部标记,以此类推。

 #include<cstdio>
#include<algorithm>
using namespace std;
struct stu
{
int l,w;
}st[];
bool cmp(stu a,stu b)
{
if(a.l != b.l) //将木头按长度从小到大排列,长度一样按质量从小到大排列
return a.l<b.l;
else
return a.w<b.w;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j,d[];
int n;
scanf("%d",&n);
for(i = ; i < n ; i++)
{
scanf("%d %d",&st[i].l,&st[i].w);
d[i]=; //标记查询过的木头,0为未标记
}
int ans=;
sort(st,st+n,cmp);
for(i = ; i < n ; i++)
{
if(!d[i])
{
d[i]=;
ans++; //ans表示时间
int w=st[i].w;
for(j = i+ ; j < n ; j++) //查询i以后的木头
{
if(!d[j] && st[j].w >= w) //因为木头的长度是从小到大排列,所以这只判断重量
{
d[j]=; //标记在i后处理不用时间的木头
w=st[j].w;
} }
}
}
printf("%d\n",ans);
}
}

杭电 1051 Wooden Sticks的更多相关文章

  1. HDOJ 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 (贪心)

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

  3. HDOJ.1051 Wooden Sticks (贪心)

    Wooden Sticks 点我挑战题目 题意分析 给出T组数据,每组数据有n对数,分别代表每个木棍的长度l和重量w.第一个木棍加工需要1min的准备准备时间,对于刚刚经加工过的木棍,如果接下来的木棍 ...

  4. 1051 Wooden Sticks

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

  5. HDU 1051 Wooden Sticks 贪心||DP

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

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

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

  7. hdu 1051:Wooden Sticks(水题,贪心)

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

  8. HDOJ 1051. Wooden Sticks

    题目 There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The ...

  9. HDU 1051 Wooden Sticks

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

随机推荐

  1. h5旋转效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 编译运行第一个Java程序——通过示例学习Java编程3

    作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=13 在本教程中,我们将了解如何编写.编译和运行Ja ...

  3. drupal6提示 Compilation failed: disallowed Unicode code point (>= 0xd800 && <= 0xdfff) at offset 9 on line 615

    解决办法:将sites\all\modules\ctools\includes\cleanstring.inc文件中的61行改成62行这样子即可,如下图

  4. javascript 流程控制及函数

    回顾 基本语法 在html的使用 <script></script> 注释 ///* */ 指令结束符 ;换行 输出 console.log()document.write() ...

  5. 初识react中的状态量

    react组件中的两类状态数据:props,state,官网API给出的使用规范,多读几遍,受益匪浅: 结论: 1. 对应任何可变的数据,理应只有一个单一“ 数据源 ” 2. 如果多个组件均需要这些数 ...

  6. Mysql如何为表字段添加索引???

    1.添加PRIMARY KEY(主键索引): ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2.添加UNIQUE(唯一索引) : ALTE ...

  7. MFC U盘检测

    WM_DEVICECHANGE消息 查阅MSDN得知: The framework calls this member function to notify an application or dev ...

  8. ScriptManager对象的属性

    --<本文属于摘抄> 属性 说明 EnablePageMethods 指定在ASPX页面上定义的公共静态方法是否可以从客户端脚本中作为Web服务方法调用 EnablePartialRend ...

  9. MVC视图特性

    在主界面的视图中可以使用viewdata,引用主界面的分布视图界面也可以调用主界面的分部视图,但是分部视图不可以定义viewdata并使用 例子如下: // // GET: /Home/ public ...

  10. MIPS简单入门

    What ‘s the MIPS? 汇编语言 汇编语言是一类语言的总称,因处理器不同,而对应的不同机器的指令集也不同,产生了很多种汇编语言. 目前最流行的是ARM,MIPS,x86.ARM用于大量的移 ...