题意:给定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定理)的更多相关文章

  1. POJ 1065 Wooden Sticks Greed,DP

    排序后贪心或根据第二关键字找最长下降子序列 #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...

  2. POJ 1065 Wooden Sticks#贪心+qsort用法

    (- ̄▽ ̄)-* 这道题用到了cstdlib库的qsort()函数: 用法链接:http://www.cnblogs.com/syxchina/archive/2010/07/29/2197382.h ...

  3. POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心

    参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...

  4. poj -1065 Wooden Sticks (贪心or dp)

    http://poj.org/problem?id=1065 题意比较简单,有n跟木棍,事先知道每根木棍的长度和宽度,这些木棍需要送去加工,第一根木棍需要一分钟的生产时间,如果当前木棍的长度跟宽度 都 ...

  5. POJ 1065 Wooden Sticks(zoj 1025) 最长单调子序列

    POJ :http://poj.org/problem?id=1065 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId= ...

  6. 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 ...

  7. POJ 1065 Wooden Sticks

    Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16262 Accepted: 6748 Descri ...

  8. HDU ACM 1051/ POJ 1065 Wooden Sticks

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. HDU 1051 Wooden Sticks 贪心||DP

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. 第1节 storm编程:8、storm的分发策略

    8. Storm的分发策略 Storm当中的分组策略,一共有八种: 所谓的grouping策略就是在Spout与Bolt.Bolt与Bolt之间传递Tuple的方式.总共有八种方式: 1)shuffl ...

  2. Django:验证码相关问题

    http://blog.csdn.net/csapr1987/article/details/7728315 https://zhidao.baidu.com/question/13837387222 ...

  3. SSH、SSL与HTTPS的联系

    SSH 维基百科中对SSH协议的定义如下 Secure Shell(缩写为SSH),由IETF的网络工作小组(Network Working Group)所制定:SSH为一项创建在应用层和传输层基础上 ...

  4. 腾讯2019秋招--小q爬塔(dp)

    小Q爬塔 题目描述: 小Q 正在攀登一座宝塔,这些宝塔很特别.塔总共有 n 层,但是每两层之间的净高却不相同,所以造成了小Q 爬过每层的时间也不同.如果某一层的高度为 x,那么爬过这一层所需的时间也是 ...

  5. ibd2sdi — InnoDB表空间SDI提取实用程序

    参考mysql8.0官方文档 https://dev.mysql.com/doc/refman/8.0/en/ibd2sdi.html ibd2sdi是一个实用程序,用于从表空间文件中提取 序列化的字 ...

  6. sklearn中实现多分类任务(OVR和OVO)

    sklearn中实现多分类任务(OVR和OVO) 1.OVR和OVO是针对一些二分类算法(比如典型的逻辑回归算法)来实现多分类任务的两种最为常用的方式,sklearn中专门有其调用的函数,其调用过程如 ...

  7. firewalld学习--service的使用和配置

    service配置文件 firewalld默认给我们提供的ftp的service配置文件ftp.xml <?xml version="1.0" encoding=" ...

  8. Golang gin开源实例——表设计

    UML Model 基本模型定义 type Model struct { ID int `gorm:"primary_key" json:"id"` Creat ...

  9. [PHP] php作为websocket的客户端实时读取推送日志文件

    首先要使用composer来下载一个第三方扩展就可以实现php的websocket客户端,直接在当前目录生成下composer.json文件就可以了composer require textalk/w ...

  10. 基于Hadoop3.1.2集群的Hive3.1.2安装(有不少坑)

    前置条件: 已经安装好了带有HDFS, MapReduce, Yarn 功能的 Hadoop集群 链接: ubuntu18.04.2 hadoop3.1.2+zookeeper3.5.5高可用完全分布 ...