POJ 1065 Wooden Sticks 解题报告-用动态规划方法解决(LIS变式)

来源:互联网 发布:java 天气预报接口 编辑:程序博客网 时间:2024/05/29 10:12

POJ 1065 Wooden Sticks 解题报告-用动态规划方法解决(LIS变式)

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

题目大意

无非就是求下降子序列(wiwi+1lili+1)的最小数目。

具体思路

看了CSDN博客中很多人都采取了贪心算法,但《挑战程序设计竞赛(第二版)》这一书中的练习题讲其编排到动态规划那里,那就耐心想想看是否可以用dp的方法求解。

仔细想想便可以知道,该问题可以转化为 l 递减的情况下 w 的最长上升子序列的长度,也就是转化成dp的经典问题之一 ——LIS问题。

我们可以通过鸽巢原理来证明以上结论的正确性:

首先设x为原题的下降子序列的最小数目,L为为l递减的情况下 w 的最长上升子序列的长度。

假定 x<L ,我们先把L中的每个元素挖出来,这些位置就变成空穴,空穴数目为L的长度。然后将原序列按照题目要求分成x个下降序列,即每一个下降序列都满足 wiwi+1lili+1

按照假设 x<L ,根据鸽巢原理可知存在一个下降序列它有两个或以上的空穴,然后再从L抽取相应数目的元素放进那个空穴中,由于L中的每两个元素要满足 wiwi+1lili+1 ,这并不满足原题所要求的下降子序列的要求。所以很容易就可以进一步地说明x的最小值就是L的长度。

所以题目就变得很简单了,先把sticks按照 l 从大到小排一下顺序,然后再求 w的LIS长度。

求LIS有两种方法,一种是按照以下递推式来做:

定义dp[i]:=ai为末尾的最长上升子序列的长度

则以ai结尾的上升子序列为
1. 只包含 ai 的子序列
2. 在满足 j<i 并且 aj<ai 为结尾的上升子列的末尾,追加上 ai 后得到的子序列。

这样就可以得到如下递推关系,时间复杂度为O(n2)

dp[i]=max{1,dp[j]+1|aj<ai}
长度为 i+1 的上升子序列中末尾元素的最小值(不存在的话就是INF)
这个方法可以用二分搜索进行进一步的简化,最终的时间复杂度可以达到O(nlogn)

参考代码

#include <iostream>#include <algorithm>using namespace std;static const int N_MAX = 5001;static const int INF = 1<<21;int N;struct stick{    int l,w;};stick S[N_MAX];int dp[N_MAX];bool comp(const stick& a, const stick& b){    return a.l > b.l;}int solve() //用第二种方法求解{    //init    fill(dp,dp+N,INF); //降序排列    sort(S,S+N,comp);    for(int i=0;i<N;i++)    {        *lower_bound(dp,dp+N,S[i].w) = S[i].w;    }    return lower_bound(dp,dp+N,INF) - dp;}int main(){    int T;    cin>>T;    for(int i=0;i<T;i++)    {        cin>>N;        for(int j=0;j<N;j++)            cin>>S[j].l>>S[j].w;        cout<<solve()<<endl;    }}
原创粉丝点击