POJ 1065 Wooden Sticks(LIS,最少链划分)
题意:求二维偏序的最少链划分。
用到Dilworth定理:最少链划分=最长反链。(对偶也成立,个人认为区别只是一个维度上的两个方向,写了个简单的证明
相关概念:偏序集,链,反链等等概念可以参考这里:http://www.cnblogs.com/fstang/archive/2013/03/31/2991220.html
Dilworth定理的证明(英文的):http://aleph.math.louisville.edu/teaching/2009FA-681/notes-091119.pdf
证明
简要来说,设链的最小划分数为p,最长反链长度为l,证明分两步:
(1).p≥l。因为反链上任意两点都不可能在同一个链中,抽屉原理可知p≥l。
(2).p≤l。反复选择反链的极小元点集Xi从全集S中删除直到S为空,
每次阶段的选择的Xi则构成一个链划分(这个可以反证,如果Xi存在a和b无法比较,那么反链中是可比的,则a和b不可能同时是反链的极小元)
最后一次删除将会对应最长的反链。所以链划分了l次,而p是最小链划分,所以有l≥p。
至于实现上,我采用的是O(nlogn)的逆向求LIS的方法来得到最长反链的。
(似乎根据证明第二步可以得出贪心做法,不过因为要排序时间复杂度还是O(nlogn)。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
//#include<bits/stdc++.h>
using namespace std; const int N = 5e3+; struct Stick
{
int l, w;
bool operator <(const Stick &th) const{
return l < th.l || (l == th.l && w < th.w);
}
void IN(){ scanf("%d%d",&l,&w); }
}stick[N]; int g[N]; //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
int T; cin>>T;
while(T--){
int n; scanf("%d",&n);
for(int i = ; i < n; i++){
stick[i].IN();
}
sort(stick,stick+n);
memset(g,0x3f,sizeof(int)*n);
int ans = ;
for(int i = n-; i >= ; i--){
int k = lower_bound(g+,g+n-i,stick[i].w)-g;
ans = max(ans,k);
g[k] = stick[i].w;
}
printf("%d\n",ans);
}
return ;
}
POJ 1065 Wooden Sticks(LIS,最少链划分)的更多相关文章
- 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 ...
- 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 (贪心)
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(zoj 1025) 最长单调子序列
POJ :http://poj.org/problem?id=1065 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId= ...
- 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【贪心】
题意: 有一些木棍,每个有长度和重量,要求把这些木棍排成若干两个属性值均不下降的序列.问至少要分为多少个序列.且要保证排出来的子序列数最少. 思路: ( 9 , 4 ) ,( 2 , 5 ) ,( 1 ...
- POJ - 1065 Wooden Sticks(贪心+dp+最长递减子序列+Dilworth定理)
题意:给定n个木棍的l和w,第一个木棍需要1min安装时间,若木棍(l’,w’)满足l' >= l, w' >= w,则不需要花费额外的安装时间,否则需要花费1min安装时间,求安装n个木 ...
随机推荐
- JAVA对象创建的过程
Java中一个实例对象被创建的过程 一.类的加载过程 首先,Jvm在执行时,遇到一个新的类时,会到内存中的方法区去找class的信息,如果找到就直接拿来用,如果没有找到,就会去将类文件加载到方法区.在 ...
- VS连接SQL Server数据库,增删改查详细教程(C#代码)_转载
工具: 1.Visual Studio (我使用的是vs2013) 2.SQL Server (我使用的是sql server2008) 操作: 1.打开SQL Server,打开后会看到数据库的初 ...
- 解决Navicat无法连接到Mysql
Navicat无法连接到Mysql,返回的错误码是Lost connection to MySQL server at ‘reading initial communication packet’, ...
- 洛谷P3387 缩点模板
给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次. 因为可以重复经过点, ...
- Mysql-6-数据类型和运算符
1.mysql数据类型 (1)数值数据类型:包括整数类型tinyint.smallint.mediumint.int.bigint,浮点小数类型float和double,定点小数类型decimal. ...
- python web开发之flask框架学习(1) 创建flask项目
python 开发越来越火,作为菜鸟,也应该学习一下,感觉还可以,记录下来,方便学习参考. 不管做什么开发首先肯定是安装环境,这里我用的是pycharm,python3.如果不清楚怎么安装环境可以去网 ...
- Centos下搭建nginx反向代理
上一节已经用编译的方式搭建好了一个nginx,链接在下面 https://www.toutiao.com/i6693130510777975300/ 然后这次我们把上次搭建好的nginx作为反向代理的 ...
- Shell面试,笔试整理
1.经典Shell脚本面试问题:https://blog.csdn.net/hyszyl/article/details/60970307
- 基于CentOS系统下的Oracle的安装
背景 最近的数据库的实验课,要求利用虚拟机安装CentOS系统,并在此系统上安装Oracle_11g软件实现监听,在windows系统上安装SQL Developer软件作为客户端 ,从而可以在SQL ...
- WPF 使用 fontawesome
<Style TargetType="TextBlock" x:Key="tree-icon"> <Style.Setters> < ...