DP_Wooden Sticks
(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
Output
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
题意:求同时满足木棒的长宽都构成不下降子序列的最少组数。 思路:1、将木棒按照长(或宽)进行升序排序。
2、建立递推数组dp[],表示操作当前木棒时所需要的设定次数。
3、dp[]的递推公式: 若前面存在一个木棒的宽度大于当前木棒的宽度的时,当前木棒所需要进行的设置数一定会大于这个木棒所需要的设置数。
当前木棒的dp[]=max{dp[前面宽度大于当前木棒宽度的木棒所对应的下标]}+1
4、遍历数组dp[],最大的dp即为题目所求最少需要的设定次数.
注意:排序完成后的木棒默认只设定一次(即假设排序完成后的序列为一个不下降序列)dp[]值初始化为1
排序时在长度相同的情况下应优先按照宽度再次进行排序。
#include<cstdio>
#include<algorithm>
using namespace std;
int dp[];
struct Node{
int h,w;
}p[];
bool cmp( Node a, Node b){
if( a.h!=b.h )
return a.h<b.h ? true:false;
return a.w<b.w ? true:false;
}
int main()
{
int t,n;
int maxn; scanf("%d",&t);
while( t-- ){
maxn=;
scanf("%d",&n);
for( int i=; i<n; i++)
scanf("%d%d",&p[i].h,&p[i].w);
sort( p, p+n, cmp);
for( int j=; j<n; j++)
dp[j]=;
for( int i=; i<n; i++){
for( int j=; j<i; j++){
if( p[j].w>p[i].w ){
dp[i]=max(dp[i],dp[j]+);
}
}
}
for( int i=; i<n; i++)
if( dp[i]>maxn )
maxn=dp[i];
printf("%d\n",maxn);
} return ;
}
感谢SLH的耐心讲解
DP_Wooden Sticks的更多相关文章
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- POJ 2653 Pick-up sticks (线段相交)
题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...
- hduoj 1455 && uva 243 E - Sticks
http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...
- POJ 2653 Pick-up sticks【线段相交】
题意:n根木棍随意摆放在一个平面上,问放在最上面的木棍是哪些. 思路:线段相交,因为题目说最多有1000根在最上面.所以从后往前处理,直到木棍没了或者最上面的木棍的总数大于1000. #include ...
- POJ1065Wooden Sticks[DP LIS]
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21902 Accepted: 9353 De ...
- 【POJ 2653】Pick-up sticks 判断线段相交
一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...
- CF451A Game With Sticks 水题
Codeforces Round #258 (Div. 2) Game With Sticks A. Game With Sticks time limit per test 1 second mem ...
- POJ 2452 Sticks Problem
RMQ+二分....枚举 i ,找比 i 小的第一个元素,再找之间的第一个最大元素..... Sticks Problem Time Limit: 6000MS ...
- The 2015 China Collegiate Programming Contest D.Pick The Sticks hdu 5543
Pick The Sticks Time Limit: 15000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...
随机推荐
- 【线性代数】4-4:正交基和Gram算法(Orthogonal Bases and Gram-Schmidt)
title: [线性代数]4-4:正交基和Gram算法(Orthogonal Bases and Gram-Schmidt) categories: Mathematic Linear Algebra ...
- HTTPie 工具使用入门
HTTPie 是一个 HTTP 的命令行客户端,目标是让 CLI 和 web 服务之间的交互尽可能的人性化.这个工具提供了简洁的 http 命令,允许通过自然的语法发送任意 HTTP 请求数据,展示色 ...
- keepalived+mysql双主热备
这里使用keepalived实现mysql的双主热备高可用 实验环境: 主机名 IP 系统版本 软件版本 master 192.168.199.6/vip:192.168.199.111 Rhel7. ...
- call()与构造函数的运用
一.简介 call()和apply()方法是所有函数体的固有属性,可以在指定作用域下调用函数.这里面有两层意思:1.可以在另外的作用域下调用函数:2.当函数体是是构造函数时,call()方法能达到类之 ...
- fastjson序列化和反序列化报com.alibaba.fastjson.JSONException: autoType is not support异常问题,解决方案整合
1.问题起因 2017年3月15日,fastjson官方发布安全升级公告,该公告介绍fastjson在1.2.24及之前的版本存在代码执行漏洞,当恶意攻击者提交一个精心构造的序列化数据到服务端时,由于 ...
- Vue UI组件库
1. iView UI组件库 iView官网:https://www.iviewui.com/ 2.Vux UI组件库 Vux官网:https://vux.li/ 3.Element UI组件库 ...
- 阶段5 3.微服务项目【学成在线】_day04 页面静态化_21-页面静态化-静态化测试-静态化程序编写
public String getPageHtml(String pageId){ /** * 静态化程序获取页面的DataUrl * * 静态化程序远程请求DataUrl获取数据模型 * * 静态化 ...
- MySQL连接错误:Can't connect to MySQL server on'localhost' (10055)
在Windows服务器上确认服务器和mysql都是正常运行,但就是连接不上.搜了一下别人的解决方案, 参考这篇https://blog.csdn.net/langren697/article/deta ...
- 公司手机打卡app时间和百度时间差30秒解决
问题: 某天发现公司手机打卡app时间和百度时间差30秒解决 分析: nginx 192.168.0.23 外网 : 220.236.7.43 mysql主 192.168.0.2 ...
- centos 7设置limit,不生效问题
1:记录未修改之前的ulimit值 [root@bogon ~]# ulimit -a 2:修改配置文件 vim /etc/security/limits.conf 在后面添加 * s ...