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操作,竟然还有返回值,涨姿势了) ...
随机推荐
- mysql 存储过程:提供查询语句并返回查询执行影响的行数
mysql 存储过程:提供查询语句并返回查询执行影响的行数DELIMITER $$ DROP PROCEDURE IF EXISTS `p_get_select_row_number`$$ CREAT ...
- pyqt记录内容(音乐播放器)
#这是UI文件 # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'AudioPlayerDia ...
- Highcharts 点击反选
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- Oracle—RMAN备份(一)
一.RMAN备份相关概念 1.RMAN备份中表空间不需要处于backup模式下,它备份数据文件,归档日志文件,控制文件,spfile和备份集片,但不备份联机重做日志文件,临时文件和口令文件. 2.备份 ...
- css布局详解(二)——标准流布局(Nomal flow)
css标准流布局(Nomal flow) 一.正常流 这是指西方语言中文本从左向右,从上向下显示,这也是我们熟悉的传统的HTML文档中的文本布局.注意,在非西方的语言中,流方向可能不同.大多数元素都在 ...
- js Date扩展Format()函数
Date.prototype.Format = function (formatStr) { var str = formatStr; var Week = ['日', '一', '二', '三', ...
- uva Fire!
算法:BFS Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner o ...
- 重写String类,也有些区别,供参考
头文件如下: #pragma once #include <string> #include <string.h> #include <stdlib.h> #inc ...
- 最小日志量的insert操作
--1.实验环境 SQL> conn scott/tiger Connected to Oracle Database 11g Enterprise Edition Release 11.2.0 ...
- iscroll源码初涉
最近尝试做web app时候,用上了神器iScroll,鉴于功力尚浅,并没有完全用好神器,所以今天特意来认真学习! 翻开官网,目前的版本是5,但是相关的文章并不多,具体的文件版本是: iscroll. ...