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 ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .

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 2n 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 贪心基础上加个id递增的条件
 #include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
#include<math.h>
using namespace std; struct node
{
int l,w;
}a[]; bool vis[]; bool cmp(node b,node c)
{
if(b.l==c.l)
return b.w<c.w;
else
return b.l<c.l;
} int main()
{ int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d%d",&a[i].w,&a[i].l);
sort(a,a+n,cmp);
int res=;
memset(vis,false,sizeof(vis));
int tmpl,tmpw;
for(int i=;i<n;i++)
{
if(!vis[i])
{
vis[i]=true;
res++;
tmpl=a[i].l;
tmpw=a[i].w;
for(int j=i+;j<n;j++)
{
if(a[j].l>=tmpl&&a[j].w>=tmpw&&!vis[j])
{
vis[j]=true;
tmpl=a[j].l;
tmpw=a[j].w;
}
}
}
}
printf("%d\n",res);
}
return ;
}
												

POJ 1065 Wooden Sticks (贪心)的更多相关文章

  1. POJ 1065 Wooden Sticks#贪心+qsort用法

    (- ̄▽ ̄)-* 这道题用到了cstdlib库的qsort()函数: 用法链接:http://www.cnblogs.com/syxchina/archive/2010/07/29/2197382.h ...

  2. POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心

    参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...

  3. HDU ACM 1051/ POJ 1065 Wooden Sticks

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

  4. POJ 1065 Wooden Sticks

    Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16262 Accepted: 6748 Descri ...

  5. poj -1065 Wooden Sticks (贪心or dp)

    http://poj.org/problem?id=1065 题意比较简单,有n跟木棍,事先知道每根木棍的长度和宽度,这些木棍需要送去加工,第一根木棍需要一分钟的生产时间,如果当前木棍的长度跟宽度 都 ...

  6. poj 1065 Wooden Sticks 【贪心 新思维】

    题目地址:http://poj.org/problem?id=1065 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 ...

  7. POJ 1065 Wooden Sticks【贪心】

    题意: 有一些木棍,每个有长度和重量,要求把这些木棍排成若干两个属性值均不下降的序列.问至少要分为多少个序列.且要保证排出来的子序列数最少. 思路: ( 9 , 4 ) ,( 2 , 5 ) ,( 1 ...

  8. POJ - 1065 Wooden Sticks(贪心+dp+最长递减子序列+Dilworth定理)

    题意:给定n个木棍的l和w,第一个木棍需要1min安装时间,若木棍(l’,w’)满足l' >= l, w' >= w,则不需要花费额外的安装时间,否则需要花费1min安装时间,求安装n个木 ...

  9. POJ 1065 Wooden Sticks(zoj 1025) 最长单调子序列

    POJ :http://poj.org/problem?id=1065 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId= ...

随机推荐

  1. (效率低下)77. Combinations C++回溯法 组合

    https://leetcode.com/problems/combinations/ 沿用78题的思路 class Solution { public: void backTrack(vector& ...

  2. 提高Bash使用效率的方法

    环境:centos6.5 1.移动 Ctrl + a :移到命令行首Ctrl + e :移到命令行尾 Ctrl + xx:在命令行首和光标之间移动 左右键移动字符 Ctrl+左右键移动单词(不记快捷键 ...

  3. Docker Nginx 配置多个子域名

    参考:nginx server_name实用:配置多个子域名 在腾讯购置了域名服务,想直接配置二级域名映射到指定端口,发现腾讯不支持端口映射的方式. 想了一下,域名默认解析80端口,只能通过nginx ...

  4. python中bottle模块的使用

    1.简介 2.示例 2.1一个简单的bottle接口 # -*- coding: utf-8 -*- from bottle import route, request, run import jso ...

  5. 用vue构建多页面应用

    最近一直在研究使用vue做出来一些东西,但都是SPA的单页面应用,但实际工作中,单页面并不一定符合业务需求,所以这篇我就来说说怎么开发多页面的Vue应用,以及在这个过程会遇到的问题. 准备工作 在本地 ...

  6. Windows下安装 Memcache

    1.下载Memcached Windows服务端程序.(memcached >= 1.4.5 版本安装32 32位系统 1.4.5版本:http://static.runoob.com/down ...

  7. spring自定义xml标签&自定义注解

    public class YafBeanDefinitionParser implements BeanDefinitionParser { private BeanDefinitionRegistr ...

  8. commonJS 和 ES6 模块化的不同

    commonjs 导出 module.exports={ add:function(){ console.log('add测试') } } 导入 var add=require('./add.js') ...

  9. chrome hosts

    # Go Hosts# 2017-05-02 02:13:04# Localhost (DO NOT REMOVE)127.0.0.1    localhost::1    localhost ip6 ...

  10. vs2015 出现Lc.exe 已退出,代码为-1的问题,如何解决

    今天在代码运行时,出现lc.exe已退出,代码为-1 的问题