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);
}
}

最新文章

  1. GIS公交查询-flex/java
  2. [LeetCode]Lowest Common Ancestor of a Binary Search Tree
  3. TypeId和IidManager关系
  4. php 模拟表单提交
  5. 怎样在自己的网站上做自动生成当前url的二维码
  6. Path对象
  7. sql server 的约束的作用 于 理解 [转]
  8. vue 实现 换一换 功能
  9. R-kmeans聚类算法
  10. Spark Shuffle模块——Suffle Read过程分析
  11. vim的基本介绍
  12. UVA-11214 Guarding the Chessboard (迭代加深搜索)
  13. Cropperjs 插件知识点
  14. 【转载】如何在 Github 上发现优秀的开源项目?
  15. leetcode-162-寻找峰值
  16. double 和 im2double 的区别
  17. AspNetPager控件的最基本用法
  18. BZOJ 3732 Network Link-Cut-Tree (我是认真的!!
  19. [COGS 2064]爬山
  20. .NET破解之爱奇迪(一)

热门文章

  1. Qt 进程和线程之三:线程同步、可重入与线程安全
  2. AKOJ-2010-魔法石
  3. [HDU1595] find the longest of the shortest
  4. E - Addition and Subtraction Hard AtCoder - 2273 思维观察题
  5. 我的NopCommerce之旅(7): 依赖注入(IOC/DI)
  6. rsyslog+analyzer
  7. c++的const和static区别
  8. Windows64+Python27下配置matplotlib
  9. C++遍历文件及文件夹代码
  10. spring 中bean学习笔记