POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心
参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629
(HDU 1257 解题思路一样就不继续讲解)
POJ 1065题意:给你n个木块,分别给出其长度和重量,然后要对这些木块进行加工,如果木块1的长度和重量都不大于木块2,
那么这两个木块可以放在一起加工,从而只用花费1个时间单位。问要如何进行加工从而能够花费最少时间单位。
知识点:
偏序集:若一个集合A为偏序集,那么满足:1、任意一个元素X属于集合,那么这个元素X≤X
2、如果集合内的元素X和Y,X≤Y且Y≤X,那么X=Y
3、如果集合内的元素X,Y,Z,X≤Y且Y≤Z,那么X≤Z
Dilworth定理:对于一个偏序集,其最少链划分数等于其最长反链的长度。
Dilworth定理的对偶定理:对于一个偏序集,其最少反链划分数等于其最长链的长度。
Dilworth定理的证明先留着,后面有空再证。
解法:首先对所有木块按照长度从小到大进行排序,然后问题就转化为了:求最少划分数,能够使得每个划分里面木块的重量是非递减的。
所以当前问题就忽略了长度,只用考虑重量就可以了。
排好序的木块,显然,其重量的集合的大于等于关系就是偏序关系,
比如木块A的重量必然≥A;若A的重量≥B,B≥A,那么A=B;若A≥B,B≥C,那么A≥C。
每个划分里面的木块重量非递减,那么显然划分里两两木块的重量是可以比较的,也就是链。
此时就转换成了求该集合的最少链划分数,也就是最长反链的长度。
反链,也就是集合中的元素不满足非递减关系,也就是递减关系。
所以问题就转换成为了求最长递减子序列(LDS)的长度
POJ 1065(LDS DP) AC代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 5005;
int t,n,d[N];
bool vis[N];
struct node
{
int w,l;
}T[N];
int cmp(node n1, node n2)
{
if(n1.w == n2.w) return n1.l < n2.l;
else return n1.w < n2.w;
}
void solve()
{
int ans = 0; sort(T,T+n, cmp); memset(d, 0, sizeof(d)); for(int i = 0; i < n; i++)
{
int mx = 0; for(int j = 0; j < i; j++)
if(T[j].l > T[i].l && d[j] > mx)
mx = d[j]; d[i] = mx+1;
ans = max(ans, d[i]);
}
printf("%d\n", ans);
}
int main()
{
//freopen("in.txt", "r", stdin);
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d %d", &T[i].w, &T[i].l);
solve();
}
return 0;
}
5.4:做了第二天的一道题,求最长上升子序列的长度。然后用了nlogn的解法,而求最长下降子序列也一样可以用该方法。
POJ 1065(LDS 的nlogn)AC代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 5005;
int t,n,d[N];
//d[i] 长度为i+1的最长下降子序列中的末尾元素的最大值
struct node
{
int w,l;
}T[N];
int cmp(node n1, node n2)
{
if(n1.w == n2.w) return n1.l < n2.l;
else return n1.w < n2.w;
}
void solve()
{
int ans = 0; sort(T,T+n, cmp); memset(d, -1, sizeof(d)); for(int i = 0; i < n; i++)
{
*lower_bound(d,d+n,T[i].l, greater<int>()) = T[i].l;
}
printf("%d\n", lower_bound(d,d+n,-1, greater<int>())-d);
}
int main()
{
//freopen("in.txt", "r", stdin);
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d %d", &T[i].w, &T[i].l);
solve();
}
return 0;
}
POJ 1065(贪心) AC代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 5005;
int t,n,d[N];
bool vis[N];
struct node
{
int w,l;
}T[N];
int cmp(node n1, node n2)
{
if(n1.w == n2.w) return n1.l < n2.l;
else return n1.w < n2.w;
}
void solve()
{
int ans = 0; sort(T,T+n, cmp);
memset(vis, false, sizeof(vis)); for(int i = 0; i < n; i++)
{
if(vis[i]) continue;
int last = T[i].l;
for(int j = i+1; j < n; j++)
if(T[j].l >= last && !vis[j])
{
vis[j] = true;
last = T[j].l;
}
ans++;
}
printf("%d\n", ans);
}
int main()
{
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d %d", &T[i].w, &T[i].l);
solve();
}
return 0;
}
POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心的更多相关文章
- POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)
Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...
- HDU 1257 最少拦截系统 (DP || 贪心)
最少拦截系统 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- HDU 1257 最少拦截系统(贪心 or LIS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1257 最少拦截系统【贪心 || DP——LIS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU——1257最少拦截系统(贪心)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 题解报告:hdu 1257 最少拦截系统(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1257 Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是 ...
- hdu 1257 最少拦截系统(简单贪心)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1257 虽然分类是dp感觉还是贪心 比较水 #include <iostream> #inclu ...
- HDU 1257 最少拦截系统 【贪心】
<题目链接> 题目大意: 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度 ...
- HDU 1257 最少拦截系统(贪心)
解题思路:用一个vector存下数据,从头开始非递增遍历,并把符合条件的删除,一次操作,ans++,当vector为空时退出循环.(PS:学到了vector的erase操作,竟然还有返回值,涨姿势了) ...
随机推荐
- c语言结构体在内存中存储,字节对齐
注意: 出于效率的考虑,C语言引入了字节对齐机制,一般来说,不同的编译器字节对齐机制有所不同,但还是有以下3条通用准则: (1)结构体变量的大小能够被其最宽基本类型成员的大小所整除: (2)结构体每个 ...
- 推荐C/C++常见的面试题目
http://blog.163.com/bingqingyujie..5/blog/static/75559361201011861958534/ 里面有详细的面试类型
- [Redux] Passing the Store Down Implicitly via Context
We have to write a lot of boiler plate code to pass this chore down as a prop. But there is another ...
- 关于时间的操作(JavaScript版)——年月日三级级联(默认依次显示请选择年、请选择月和请选择日)
这篇博客和前一篇博客基本同样,仅仅是显示的默认值不同: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN&quo ...
- 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6564592 在前一篇文章提到,从源代码树下载下 ...
- samba错误
1.session setup failed: NT_STATUS_LOGON_FAILURE 该错误表示用户有误, 可能是用户不存在, 也有可能是密码错误, 或者用户只是在samba和系统的用户中的 ...
- JS高级程序设计学习笔记之基本包装类型
概述 基本类型:string.boolean.number 每当读取一个基本类型的值的时候,后台就会创建一个对应的基本包装类型的对象,从而让我们能够调用一些方法来操作这些数据. 使用new操作符创建的 ...
- EF数据建模(一)
大中型软件开发过程中常会使用ORM技术,ORM全称是“对象-关系映射Object-Relation-Mappping”.是将数据库中的数据对象的形式表现出来,并将通过面向对象的方式将这些对象组织起来, ...
- Windows命令行(DOS命令)教程–2 (转载) http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_1.html
二.符号约定 为了便于说明格式,这里我们使用了一些符号约定,它们是通用的: C: 盘符 Path 路径 Filename 文件名 .ext 扩展名 Filespec 文件标识符 [ ] 方括号中的项目 ...
- 支付宝SDK快速入口链接
支付宝快捷支付SDK官方网站