Poj 1065 Wooden Sticks【贪心+LIS?】

来源:互联网 发布:淘宝价格监控软件 编辑:程序博客网 时间:2024/04/29 15:25
Wooden SticksTime Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%lld & %llu
SubmitStatus

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

213


题意:

给出若干个小木棍的长度和宽度(不能调换长宽),需要用一个机器处理完所有木棍,如果后一个木棍的长度和宽度都分别大于上一个木棍,那么不浪费多于的时间,否则花费1分钟,问处理完毕所有的木棍,至少需要多少时间.

N*N 的dp 做法:

二级升序排列,对第二级元素求最小下降子序列的长度,理论解释如下(来源于大神)

其实题目的意思就是把所有元素分为最少的堆数,每堆有l<=l' and w<=w' 按l排序后(l相等则按w),问题转化为把所有元素分为最少的堆数,每堆有w<=w'(l<=l' 显然成立) 即已知一个数列,要求最少用多少个不下降序列完全覆盖
可以证明不下降序列完全覆盖数就是最长下降子列的长度(记为L): 显然覆盖数不能比L小,否则由抽屉原理,必然有下降子列中两元素(a < b)在同一不下降须列中(a <= b),这是不可能的 由覆盖数可以取得L,而序列的每个元素在不同堆中,然后每次将元素“贪心”地分在堆中,这个过程和dp地求最长下降子列很像,可以构造解,也可以反证如果不能分号,与下降子列长度为L矛盾。
于是先将数列按照l,w的顺序进行快排,然后在求出w序列中的最长递减序列的长度就可以了.
<span style="font-size:18px;">#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>using namespace std;struct node{   int len,weight;}nod[5005];int dp[5005];bool cmp(node a,node b){   if(a.len==b.len) return a.weight<b.weight;   else return a.len<b.len;}int main(){    int t,n;    scanf("%d",&t);    while(t--){        scanf("%d",&n);        for(int i=0;i<n;i++){            scanf("%d %d",&nod[i].len,&nod[i].weight);        }        sort(nod,nod+n,cmp);        memset(dp,0,sizeof(dp));        dp[0]=1;        int ans=0;        for(int i=0;i<n;i++){            dp[i]=1;            for(int j=0;j<n;j++){                if(nod[j].weight>nod[i].weight)                    dp[i]=max(dp[i],dp[j]+1);            }            ans=max(ans,dp[i]);        }        printf("%d\n",ans);    }    return 0;}</span>


0 0
原创粉丝点击