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

题意:求同时满足木棒的长宽都构成不下降子序列的最少组数。

思路:1、将木棒按照长(或宽)进行升序排序。
   2、建立递推数组dp[],表示操作当前木棒时所需要的设定次数。
   3、dp[]的递推公式:  若前面存在一个木棒的宽度大于当前木棒的宽度的时,当前木棒所需要进行的设置数一定会大于这个木棒所需要的设置数。
           当前木棒的dp[]=max{dp[前面宽度大于当前木棒宽度的木棒所对应的下标]}+1
   4、遍历数组dp[],最大的dp即为题目所求最少需要的设定次数.
注意:排序完成后的木棒默认只设定一次(即假设排序完成后的序列为一个不下降序列)dp[]值初始化为1
   排序时在长度相同的情况下应优先按照宽度再次进行排序。


 #include<cstdio>
#include<algorithm>
using namespace std;
int dp[];
struct Node{
int h,w;
}p[];
bool cmp( Node a, Node b){
if( a.h!=b.h )
return a.h<b.h ? true:false;
return a.w<b.w ? true:false;
}
int main()
{
int t,n;
int maxn; scanf("%d",&t);
while( t-- ){
maxn=;
scanf("%d",&n);
for( int i=; i<n; i++)
scanf("%d%d",&p[i].h,&p[i].w);
sort( p, p+n, cmp);
for( int j=; j<n; j++)
dp[j]=;
for( int i=; i<n; i++){
for( int j=; j<i; j++){
if( p[j].w>p[i].w ){
dp[i]=max(dp[i],dp[j]+);
}
}
}
for( int i=; i<n; i++)
if( dp[i]>maxn )
maxn=dp[i];
printf("%d\n",maxn);
} return ;
}

感谢SLH的耐心讲解

DP_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. POJ 2653 Pick-up sticks (线段相交)

    题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...

  3. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  4. POJ 2653 Pick-up sticks【线段相交】

    题意:n根木棍随意摆放在一个平面上,问放在最上面的木棍是哪些. 思路:线段相交,因为题目说最多有1000根在最上面.所以从后往前处理,直到木棍没了或者最上面的木棍的总数大于1000. #include ...

  5. POJ1065Wooden Sticks[DP LIS]

    Wooden Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21902   Accepted: 9353 De ...

  6. 【POJ 2653】Pick-up sticks 判断线段相交

    一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...

  7. CF451A Game With Sticks 水题

    Codeforces Round #258 (Div. 2) Game With Sticks A. Game With Sticks time limit per test 1 second mem ...

  8. POJ 2452 Sticks Problem

    RMQ+二分....枚举 i  ,找比 i 小的第一个元素,再找之间的第一个最大元素.....                   Sticks Problem Time Limit: 6000MS ...

  9. The 2015 China Collegiate Programming Contest D.Pick The Sticks hdu 5543

    Pick The Sticks Time Limit: 15000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

随机推荐

  1. LOJ6436. 「PKUSC2018」神仙的游戏 [NTT]

    传送门 思路 首先通过各种手玩/找规律/严谨证明,发现当\(n-i\)为border当且仅当对于任意\(k\in[0,i)\),模\(i\)余\(k\)的位置没有同时出现0和1. 换句话说,拿出任意一 ...

  2. open suse tumbleweed安装记录

    zypper install imagewriter cmake blender fontforge gimp digikam inkscape  kontact pitivi smplayer si ...

  3. 记录下我用Jenkins打包碰到的坑

    使用Andorid Studio 打包都是正常的,但是使用Jenkins自动打包一直报错,尝试过网上的各种方案,依然都不行,报错如下. FAILURE: Build failed with an ex ...

  4. idea 2018注册码(激活码)

    最近做一个项目,用idea 社区版的   但是缺少了好多功能 无奈只能用专业版的,但是需要注册激活  下面是我的注册方法 1.打开了idea  会提示让激活  选择Licensse server 2. ...

  5. JS模拟Touch事件

    var ele = document.getElementsByClassName('target_node_class')[0] //may have x and y properties in s ...

  6. VS Code文本编辑快捷操作(2)

    1.  光标移动         移动光标最常用的就是方向键,但是方向键每次只能把光标移动一个位置,可以说是一种相对低效的方式.下面介绍针对单词.行.代码块.整个文档等多种光标移动方式.   1.1 ...

  7. C之堆栈

    栈* 自动申请,自动释放* 大小固定,内存空间连续* 从栈上分配的内存叫静态内存 堆* 程序员自己申请* new/malloc* 大小取决于虚拟内存的大小,内存空间不连续* java中自动回收,C中需 ...

  8. HttpServletRequest&HttpServletResponse对象

    HttpServletRequest&HttpServletResponse对象不是由我们来创建的,而是由tomcat服务器创建,那么我们就可以直接来使用这两个 对象 A: HttpServl ...

  9. php判断为空就插入,判断不为空就更新

    if ($_GET['tplname']!==null) { if ($userinfo[0] == ''){$exec="INSERT INTO cblej_company_pc_temp ...

  10. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_12-webpack研究-webpack安装

    npm默认安装配置的路径配置在nodejs的node_modules目录 j加上 -g 就是全局安装 后面只写webpack默认安装的是最新版本 指定版本号 视频中建议指定版本号进行安装