HDU-1051/POJ-1065 Wooden sticks 木棍子(动态规划 LIS 线型动归)
嘤嘤嘤,实习半年多的小蒟蒻的第一篇博客(题解)
英文的:
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.
谷歌翻译:
题目
一堆n根木棍。每个棒的长度和重量是预先已知的。这些木棒将由木工机械一一加工。机器需要准备一些时间(称为准备时间)来准备处理木棍。设置时间与清洁操作以及更换机器中的工具和形状有关。木工机的设置时间如下:
(a)第一个木棍的准备时间为1分钟。
(b)在处理长度为l和重量为w的棒之后,如果l <= l'并且w <= w',则机器将不需要设置长度为l'和重量为w'的棒的设置时间。否则,将需要1分钟进行设置。
您将找到处理给定的n根木棍的最短准备时间。例如,如果您有五根长度和重量对分别为(9,4),(2,5),(1、2),(5、3)和(4,1)的摇杆,则最小设置时间应该是2分钟,因为有对(4,1),(5,3),(9,4),(1,2),(2,5)对的序列。
输入值
输入包含T个测试用例。在输入文件的第一行中给出了测试用例的数量(T)。每个测试用例由两行组成:第一行具有整数n,1 <= n <= 5000,代表测试例中木棍的数量,第二行包含2n个正整数l1,w1,l2, w2,...,ln,wn,每个大小最大为10000,其中li和wi分别是第i个木棍的长度和重量。 2n个整数由一个或多个空格分隔。
输出量
输出应包含以分钟为单位的最短建立时间,每行一个。
样例输入
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
样例输出
2
1
3
思路:拦截导弹(洛谷P1020)+友好城市(洛谷P2782),没有做过的需要提前做
1.这道题乍一看是一个线性DP(因为既没有区间又没有树),根据l <= l'并且w <= w'的条件很容易就联想到求最大不下降子序列即LIS,但是细细一看发现这就是个拦截导弹(洛谷P1020,可以关注下我的博客)的加强版,如果我们只处理一边的数据,不过是让我们求出上升子序列的最小值,根据我们在拦截导弹中习得的Dilworth定理(最少链划分=最长反链长度),可以直接把这道题转化为求最长下降子序列的长度(不管是求最长上升还是下降还是不上升不下降都可以用nlogn的优化)
2.表面上看大木棒子既有长度又有重量,其实我们可以把他直接当做结构体中的两个值进行处理,这就类似于之前我们做过的友好城市,只不过那道题里是连接南岸和北岸的桥,桥不能相交,而这道题中就是大木棒子的长度和重量,尽量使前一根木棒的长度重量小于后一根木棒的长度重量,所以我们可以先对结构体中的一个值进行排序,然后对另一个值进行LIS的处理
注意:在我们第二步中对结构体的排序需要进行判等的处理,如果两个大木棒子的某一个值相等,就需要对这两根大木棒子另一个值判断,最后把较大值排在后面,以便做第一步的LIS。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=5e3+,INF=0x3f3f3f3f;//max别太大,亲测会MLE
int n,f[maxn],low1[maxn],low2[maxn],ans1=,ans2=;
struct Node{
int x,y;
}a[maxn],b[maxn];
bool cmp1(Node A,Node B){
if(A.x==B.x)return A.y<B.y;//如果相等,让较小值排在前面,便于LIS的处理
return A.x<B.x;
}
bool cmp2(Node A,Node B){
if(A.y==B.y)return A.x<B.x;
return A.y<B.y;
}
int main(){
// freopen("data.txt","r",stdin);
int t;
cin>>t;
while(t--){
cin>>n;
memset(low1,,sizeof(low1));
memset(low2,,sizeof(low2));
memset(a,,sizeof(a));//别忘了这里的预处理
memset(b,,sizeof(b));
ans1=ans2=;//这里也是
for(int i=;i<=n;i++){
cin>>a[i].x;
cin>>a[i].y;
b[i].x=a[i].x;
b[i].y=a[i].y;
}//这里我为了防止暴毙,对木棒两个值都进行了LIS的处理
sort(a+,a+n+,cmp1);
sort(b+,b+n+,cmp2);
low1[]=a[n].y,low2[]=b[n].x;
for(int i=n-;i>=;i--){//这里跟最长上升子序列的区别不大,细细体会
if(low1[ans1]<a[i].y)low1[++ans1]=a[i].y;
else low1[lower_bound(low1+,low1+ans1+,a[i].y)-low1]=a[i].y;
}
for(int i=n-;i>=;i--){
if(low2[ans2]<b[i].x)low2[++ans2]=b[i].x;
else low2[lower_bound(low2+,low2+ans2+,b[i].x)-low2]=b[i].x;
}
cout<<min(ans1,ans2)<<endl;
}//嘤嘤嘤
}
没做拦截导弹和友好城市的一定要做噢
--2020-04-06
HDU-1051/POJ-1065 Wooden sticks 木棍子(动态规划 LIS 线型动归)的更多相关文章
- HDU ACM 1051/ POJ 1065 Wooden Sticks
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心
参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...
- POJ 1065 Wooden Sticks
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16262 Accepted: 6748 Descri ...
- 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 ...
- poj -1065 Wooden Sticks (贪心or dp)
http://poj.org/problem?id=1065 题意比较简单,有n跟木棍,事先知道每根木棍的长度和宽度,这些木棍需要送去加工,第一根木棍需要一分钟的生产时间,如果当前木棍的长度跟宽度 都 ...
- 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 ...
- POJ 1065 Wooden Sticks(zoj 1025) 最长单调子序列
POJ :http://poj.org/problem?id=1065 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId= ...
- POJ 1065 Wooden Sticks【贪心】
题意: 有一些木棍,每个有长度和重量,要求把这些木棍排成若干两个属性值均不下降的序列.问至少要分为多少个序列.且要保证排出来的子序列数最少. 思路: ( 9 , 4 ) ,( 2 , 5 ) ,( 1 ...
- POJ 1065 Wooden Sticks Greed,DP
排序后贪心或根据第二关键字找最长下降子序列 #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...
随机推荐
- el-upload配合vue-cropper实现上传图片前裁剪
需求背景 上传一个封面图,在上传之前需要对图片进行裁剪,上传裁剪之后的图片,类似微信的上传头像. 技术方案 上传肯定是用element的 el-upload 组件实现上传,非常方便,各种钩子函数. 裁 ...
- iOS-pthread && NSThread && iOS9网络适配
几个概念: 进程:"正在运行"应用程序(app)就是一个进程,它至少包含一个线程: 进程的作用:为应用程序开辟内存空间: 线程:CPU调度的最小单元: ...
- Linux文件系统--基于EXT2
一.文件系统基本知识 ① 存储在永久性存储介质,由程序按照某种格式制作的数据集合叫做文件 ② 磁盘上管理文件的文件.数据结构和操作构成磁盘文件系统,简称文件系统 ③ 文件属性 (1)名称 (2)位置 ...
- 第一次使用Genymotion遇到的问题:for an unknown reson,VirtualBox DHCP has not assigned an IP address to virtual
解决方案:http://www.aiuxian.com/article/p-554135.html
- TD课程通的最终版评价
相较之前的软件,功能得到了完善,同时也修复了BUG,还增加了辅助工具,可以查询一周的占用情况,省空间,很方便. 添加课程表的功能也得到了完善,同时增加了链接到教务系统的功能,方便查询相关的课程.问题是 ...
- pip install 执行过程中遇到的各种问题
一.pip install 安装指定版本的包 要用 pip 安装指定版本的 Python 包,只需通过 == 操作符 指定. pip install robotframework == 2.8.7 将 ...
- <WP8开发学习笔记>修改panorama全景控件的标题的大小
panorama(全景)控件非常具有WinPhone特色,但是那个巨大的标题许多时候会让人觉得违和.怎么修改它呢? 最开始想到的是加一个FontSize,结果毫无影响.╮(╯-╰)╭ <phon ...
- Razor 视图
Razor 视图 关于视图引擎 视图引擎简单理解就是能够支持对视图的解析,在 ASP.NET MVC 中,视图引擎的作用就是把视图处理成浏览器能够执行的 HTML 代码,不同的视图引擎,语法规则不一样 ...
- ca71a_c++_指向函数的指针_通过指针调用函数txwtech
/*ca71a_c++_指向函数的指针_通过指针调用函数用typedef简化函数指针的定义简化前: bool(*pf)(const string&, const string &); ...
- MFC编辑框接收数据动态更新与刷新方法代码示例-如何让编辑框内容实时更新
MFC编辑框接收数据动态更新与刷新方法代码示例-如何让编辑框内容实时更新 关键代码: //发送数据通知 //from txwtech@163.com LRESULT CCommSampleDlg::O ...