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
题目大意
无非就是求下降子序列(
具体思路
看了CSDN博客中很多人都采取了贪心算法,但《挑战程序设计竞赛(第二版)》这一书中的练习题讲其编排到动态规划那里,那就耐心想想看是否可以用dp的方法求解。
仔细想想便可以知道,该问题可以转化为
我们可以通过鸽巢原理来证明以上结论的正确性:
首先设x为原题的下降子序列的最小数目,L为为
假定
按照假设
所以题目就变得很简单了,先把sticks按照
求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; }}
- POJ 1065 Wooden Sticks 解题报告-用动态规划方法解决(LIS变式)
- POJ 1065 Wooden Sticks(LIS)
- HDU:1051 Wooden Sticks(贪心+动态规划DP||LIS?)
- hdu1051 Wooden Sticks(LIS动态规划or贪心)
- POJ 1065 Wooden Sticks(贪心 LIS思想)
- poj 1065 Wooden Sticks dp LIS
- Poj 1065 Wooden Sticks【贪心+LIS?】
- Poj 1065 Wooden Sticks【贪心+LIS?】
- 1065--Wooden Sticks解题报告
- Wooden Sticks(hdu1051,LIS)
- poj 1163解题报告(动态规划)
- POJ1065 Wooden Sticks ACM解题报告(暴力贪心)
- POJ 1065 Wooden Sticks
- poj 1065 Wooden Sticks
- poj 1065 Wooden Sticks
- poj 1065 Wooden Sticks
- poj 1065 Wooden Sticks
- POJ 1065 Wooden Sticks
- Java 中的 List,Set 和 Map 的区别
- Leetcode445. 链表相加
- The proxy server is refusing connections – Fix for Firefox Browser
- 日期及时间处理包 Carbon 在 Laravel 中的简单使用
- face api协议分析
- POJ 1065 Wooden Sticks 解题报告-用动态规划方法解决(LIS变式)
- CodeForces 873C(贪心)
- SQL优化准则
- C++——选择排序
- 10小时入门大数据-全面掌握Hadoop开发的核心技能
- Tomcat安装
- 边缘检测
- 111
- 2017.10.22 VC助手 All instances of the lincense "XX" are in use.