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< ...
随机推荐
- UVIYN MMDVM充电宝支持APRS与 YSF
需求就是要在APRS地图上显示对讲机位置 1.打开pi-star首页链接配置的专家(EXPERT)设置 下面链接快速打开 http://ip/admin/expert/edit_ysfgateway. ...
- [RH134] 8-磁盘管理
一.磁盘结构 我们以但磁盘的硬盘为例,如图所示: 从内向往有很多的磁道(这里我们只画了5条,实际上非常多),这个磁盘被划分为很多扇区.每个扇区有一个固定的大小,例如512Bytes. 对于多磁盘的硬盘 ...
- [go设计模式]工厂方法模式
简单工厂就是生产整个计算器,而工厂方法只生产计算器的一部分: 原有的简单工厂可以生'+' '-' '*' '/' ;但是如果添加新的部件'%',厂房就 需要扩充.修改很可以会影响原来部件的正常生产,这 ...
- jmeter 性能测试入门手册分享
深思熟虑之下,决定把这份性能测试入门手册分享给大家 最初整理这份教程的是因为自己在学习性能测试的过程中踩过了很多的坑,遇到了 数不清的问题,于是就想着将这些解决的问题全都归拢在一个文档里,方便自己查阅 ...
- 新Mac电脑pycharm爬虫环境安装与配置
*需要安装的软件:Pycharm.Squel pro.mysql.redis等. 1.下载安装pycharm. 2.下载安装item2. 3.安装brew:'ruby -e "$(curl ...
- 线程的状态及sleep、wait等方法的区别
1.创建状态 使用 new 关键字和 Thread 类或其子类建立一个线程对象后,该线程对象就处于新建状态.它保持这个状态直到程序 start() 这个线程. 2.就绪状态 当线程对象调用了start ...
- PAI-AutoLearning 图像分类使用教程
概述 PAI AutoLearning(简称PAI AL)自动学习支持在线标注.自动模型训练.超参优化以及模型评估.在平台上只需准备少量标注数据,设置训练时长即可得到深度优化的模型.同时自动学习PAI ...
- CentOS7.5搭建Hive2.3.3
一 Hive的下载 软件下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/hive/ 这里下载的版本是:apache-hive-2.3.3-bin.t ...
- 使用本地shadow socks代理
1,第一种方式 import urllib2 import socks from sockshandler import SocksiPyHandler opener = urllib2.build_ ...
- cb15a_c++_vector容器的自增长_每次增加百分之50
cb15a_c++_vector容器的自增长_每次增加百分之50每次自动容量代销扩充,增加百分之50_for windows C++,vector是用数组做出来的->数组的缺点和优点优点:具有下 ...