POJ - 1065 Wooden Sticks(贪心+dp+最长递减子序列+Dilworth定理)
题意:给定n个木棍的l和w,第一个木棍需要1min安装时间,若木棍(l’,w’)满足l' >= l, w' >= w,则不需要花费额外的安装时间,否则需要花费1min安装时间,求安装n个木棍的最少时间。
分析:
1、将木棍按l排序后,实质上是求按w形成的序列中的最长递减子序列。
eg:
5
4 9 5 2 2 1 3 5 1 4
将木棍按l排序后,按w形成的序列为4 1 5 9 2, 则若按照4 5 9 1 2的顺序安装(按照木棍标号为1 3 4 2 5的顺序安装),只需两分钟。
容易发现,所需时间为上升子序列的个数,根据Dilworth定理,最少的chain个数等于最大的antichain的大小,即最少上升子序列的个数等于最长递减子序列的长度。
2、按上例,最后求得dp数组中是单减序列,为9,2,-1,……,可见最少上升子序列的个数等于最长递减子序列的长度。
3、注意使用lower_bound()时从大到小排序后的greater<int>()。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 5000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
struct Node{
int l, w;
void read(){
scanf("%d%d", &l, &w);
}
bool operator < (const Node& rhs)const{
return l < rhs.l || (l == rhs.l && w < rhs.w);
}
}num[MAXN];
int dp[MAXN];
int main(){
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i){
num[i].read();
}
sort(num, num + n);
memset(dp, -1, sizeof dp);
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(j == 0 || dp[j - 1] > num[i].w){
dp[j] = max(dp[j], num[i].w);
}
}
}
printf("%d\n", lower_bound(dp, dp + n, -1, greater<int>()) - dp);
}
return 0;
}
POJ - 1065 Wooden Sticks(贪心+dp+最长递减子序列+Dilworth定理)的更多相关文章
- POJ 1065 Wooden Sticks Greed,DP
排序后贪心或根据第二关键字找最长下降子序列 #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...
- POJ 1065 Wooden Sticks#贪心+qsort用法
(- ̄▽ ̄)-* 这道题用到了cstdlib库的qsort()函数: 用法链接:http://www.cnblogs.com/syxchina/archive/2010/07/29/2197382.h ...
- POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心
参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...
- poj -1065 Wooden Sticks (贪心or dp)
http://poj.org/problem?id=1065 题意比较简单,有n跟木棍,事先知道每根木棍的长度和宽度,这些木棍需要送去加工,第一根木棍需要一分钟的生产时间,如果当前木棍的长度跟宽度 都 ...
- 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 (贪心)
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
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16262 Accepted: 6748 Descri ...
- HDU ACM 1051/ POJ 1065 Wooden Sticks
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1051 Wooden Sticks 贪心||DP
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- 分享Linux CentOS7 VMware 系统目录结构、 ls命令 、文件类型、alias命令——笔记
一. 系统目录结构 生成目录树结构: tree -a 显示所有 tree -d 仅显示目录 tree -L n n代表数字..表示要显示几层... tree -f 显示完整路径.. yum insta ...
- WebApi如何接收前台传递过来的数组
var ids = ["111", "222", "333"];$.ajax({ url: host + '/User/deleteByID ...
- 5.6 Nginx Rewrite模块配置
- python类的对象使用
class student(): def __init__(self,age,sex): self.age = age self.sex = sex #self._ ...
- 非阻塞多路IO
socket.listen() rfds=[] wfds=[] while(select(rfds,wfds,timeout)){//事件循环 client=socket.accept(timeout ...
- P1069 微博转发抽奖
P1069 微博转发抽奖 转跳点:
- Emergency
题意:有N个点,M条边,每个点有权值,问从起点到终点最短路的个数以及权值最大的最短路的权值. 分析:修改Dijstra模板. #include<bits/stdc++.h> using n ...
- 第3节 sqoop:7、通过java代码远程连接linux执行shell命令
数据库的数据同步软件sqoop 数据同步 关系型数据库到大数据平台 任务:sqoop 是批量导入数据太慢,如何做到实时的数据同步 实时的数据同步工具: canal 阿里开源的一个数据库数据实时同步的软 ...
- 打开c++ 项目遇到的错误
前言 后续持续更新: 无法打开源文件windows.h https://blog.csdn.net/Mr__George/article/details/87714252 找不到duilib.h ht ...
- 将xml字符串的所有叶标签转换成Map集合
实际问题:对方服务器接口采用webservice方式返回xml报文,现需解析xml获取所有叶节点的标签名及携带的值 解决方案:利用dom4j解析xml并利用递归获取叶节点,将标签名及标签值封装到Map ...