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< ...
随机推荐
- Zabbix 邮箱告警(Python脚本)
Python 脚本内容 #!/bin/env python #coding:utf- import smtplib from email.mime.text import MIMEText from ...
- Redis学习笔记(十六) Sentinel(哨兵)(下)
消失了一段时间,我又回来啦.不多说,继续把哨兵看完. 检测主观下线状态 默认情况下,Sentinel会以每秒一次的频率向所有与他创建了命令连接的实例(主从服务器以及其他Sentinel)发送PING命 ...
- css实现朋友圈照片排列布局
纯css实现朋友圈不同数量图片不同布局 首先可以打开朋友圈观察不同图片数量的几种布局,也可参考下图示例: 可以发现 除1张图片,4张图片特殊外,其他数量图片均使用一行三列的方式排列: 假设有如下HTM ...
- ROS 机器人技术 - 解决 ROS_INFO 不能正确输出 string 的问题!
一.输出「??」 项目调试一个节点,打印 ROS 信息时发现设置的节点名称都是问号: ROS_INFO("[%s]: camera_extrinsic_mat", kNodeNam ...
- MYSQL SQL 语句修改字段默认值
alter table tablename alter column drop default; (若本身存在默认值,则先删除) alter table tablename alter column ...
- SpringBoot任务
异步任务: 在方法上添加@Async注解 表明这个方法是一个异步的方法 package com.king.service; import org.springframework.scheduling. ...
- 使用numpy生成二维正态分布
参考资料: https://www.zhihu.com/question/39823283?sort=created https://www.zhihu.com/question/288946037/ ...
- windbg分析一次大查询导致的内存暴涨
项目上反馈了一个问题,就是在生产环境上,用户正常使用的过程中,出现了服务器内存突然暴涨,客户有点慌,想找下原因. 讲道理,内存如果是缓慢上涨一直不释放的话,应该是存在内存泄漏的,这种排查起来比较困难, ...
- 使用Docker构建企业Jenkins CI平台
在如今的互联网时代,随着软件开发复杂度的不断提高,软件开发和发布管理也越来越重要.目前已经形成一套标准的流程,最重要的组成部分就是持续集成(Continuous Integration,CI)及持续部 ...
- Day7-微信小程序实战-交友小程序首页UI
一般都是直接用微信提供的组件来进行布局的 在小程序中最好少用id,尽量用class 轮播图就是直接用swiper 直接在微信开发者文档里面->组件->swiper->示例代码 < ...