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

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 ( 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

Source

#include <iostream>

#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Node
{
    int l;
    int w;
    int id;
}p[5555];
bool cmp(Node x,Node y)
{
    if(x.l!=y.l)
    {
        return x.l<y.l;
    }
    else
    {
        return x.w<y.w;
    }
}
int main()
{
    int T;
    cin>>T;
while(T--)
{
    memset(p,0,sizeof(p));
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>p.l>>p.w;
        p.id=i;
    }
    sort(p,p+n,cmp);
    int dp[5555];
    for(int i=0;i<n;i++)
    {
        dp=1;
        for(int j=0;j<i;j++)
        {
            if(p.w<p[j].w)
            {
                if(dp<dp[j]+1)
                {
                    dp=dp[j]+1;
                }
            }
        }
    }
    int maxn=-1;
    for(int i=0;i<n;i++)
    {
//        cout<<p.id<<" ";
        maxn=max(maxn,dp);
    }
//    cout<<endl;
    cout<<maxn<<endl;
}
    return 0;
}

POJ 1065 Wooden Sticks的更多相关文章

  1. HDU ACM 1051/ POJ 1065 Wooden Sticks

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

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

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

  3. POJ 1065 Wooden Sticks (贪心)

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

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

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

  5. 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 ...

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

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

  7. POJ 1065 Wooden Sticks【贪心】

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

  8. POJ 1065 Wooden Sticks Greed,DP

    排序后贪心或根据第二关键字找最长下降子序列 #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...

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

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

随机推荐

  1. 蒙牛乳业六厂—第一家MES工厂

    在上海西门子工业自动化(SIAS)与蒙牛液态奶事业部以及蒙牛集团信息中心的共同努力下,经过项目组成员1年半时间的具体实施,中国乳品行业第一个真正意义上的生产执行系统MES,于2008年6月在蒙牛乳业集 ...

  2. Kafka入门学习(一)

    ====常用开源分布式消息系统 *集群:多台机器组成的系统叫集群. *ActiveMQ还是支持JMS的一种消息中间件. *阿里巴巴metaq,rocketmq都有kafka的影子. *kafka的动态 ...

  3. STL之迭代器

    容器支持的迭代器类型  STL Container  Type of Iterators Supported   vector  random access iterators 元素严格有序(类似数组 ...

  4. JForum二次开发(一)

    1.环境 myeclipse2014,jdk7,tomcat8,mysql5.6 2.下载源码地址 http://jforum.net/download.jsp 3.导入源码 新建web工程JForu ...

  5. 开始认真学计算机网络----computer network学习笔记(一)

    什么是计算机网络,就是连一堆计算机,计算机不单单指pc,还包括打印机啦,手机啦巴拉巴拉一堆 为什么要连,share data共享数据 数据? 文档,图片,视频,巴拉巴拉 网络有什么类型? LAN--- ...

  6. @Async java 异步方法

    在spring 3中,@Async注解能让某个方法快速变为异步执行,马上来先DEMO上手下. 假如在网站的用户注册后,需要发送邮件,然后用户得到邮件确认后才能继续其他工作: 假设发送是一个很耗费时间的 ...

  7. "Programming"和"Programming"是同一个"Programming"吗?

    什么意思? C语言没有专门的字符串类型,但是,它同样可以处理字符串.本文不是讨论字符串的使用,而是讨论C字符串之间的关系.如题,在C语言代码中,如果定义#define STR = "Prog ...

  8. hdu 2120 Ice_cream's world I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2120 Ice_cream's world I Description ice_cream's worl ...

  9. Java之蛋疼的file Protocol

    file Protocol Opens a file on a local or network drive. Syntax file:///sDrives[|sFile] Tokens sDrive ...

  10. PHP笔记-PHP中Web Service.

    这几天工作需要.net站点免登陆访问PHP的Wiki站点. PHP不熟,感觉很苦逼.任务下来了,必须搞定.准备用SSO,太麻烦了,要改写别人很多代码,这个是第三方CMS,封装的很厉害,不好改.最后我的 ...