参考链接: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 贪心的更多相关文章

  1. POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)

    Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...

  2. HDU 1257 最少拦截系统 (DP || 贪心)

    最少拦截系统 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  3. HDU 1257 最少拦截系统(贪心 or LIS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)   ...

  4. hdu 1257 最少拦截系统【贪心 || DP——LIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  5. HDU——1257最少拦截系统(贪心)

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  6. 题解报告:hdu 1257 最少拦截系统(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1257 Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是 ...

  7. hdu 1257 最少拦截系统(简单贪心)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1257 虽然分类是dp感觉还是贪心 比较水 #include <iostream> #inclu ...

  8. HDU 1257 最少拦截系统 【贪心】

    <题目链接> 题目大意: 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度 ...

  9. HDU 1257 最少拦截系统(贪心)

    解题思路:用一个vector存下数据,从头开始非递增遍历,并把符合条件的删除,一次操作,ans++,当vector为空时退出循环.(PS:学到了vector的erase操作,竟然还有返回值,涨姿势了) ...

随机推荐

  1. poj 2049 Let it Bead(polya模板)

      Description Cannery Row percent of the target audience insists that the bracelets be unique. (Just ...

  2. poj 1328 Radar Installation(贪心)

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  3. Codeforces554C:Kyoya and Colored Balls(组合数学计算+费马小定理)

    题意: 有k种颜色,每种颜色对应a[i]个球,球的总数不超过1000 要求第i种颜色的最后一个球,其后面接着的必须是第i+1种颜色的球 问一共有多少种排法 Sample test(s) input o ...

  4. python3-day5(模块)

    1.获取路径import os,sys #获取全部路径 print(os.path.abspath(__file__)) #获取目录 print(os.path.dirname(os.path.abs ...

  5. Android设置定时执行执行一次任务

    private Handler handler = new Handler(){ public void handleMessage(Message msg) { super.handleMessag ...

  6. LoadRunner测试下载功能点脚本(方法二)

    在上一篇<LoadRunner下载功能点脚本(方法一)>中,实现的脚本仅是录制下载功能点的脚本,现在性能需求的场景更改如下: 性能需求:对系统某页面中,点击下载并将下载文件保存到本地电脑的 ...

  7. VS2015 添加DNX SDK

    第一次运行VS2015,添加第一个ASP.NET 5程序时会报一个错误“DNX SDK版本 “dnx-clr-win-x86.1.0.0-beta5”无法安装. 解决办法: 打开CMD :输入 @po ...

  8. mysql 中的数据类型

    unsigned   既为非负数,用此类型可以增加数据长度! 例如如果    tinyint最大是127,那    tinyint    unsigned    最大   就可以到    127 * ...

  9. Android测试框架1(非原创)

    1.继承AndroidTestCase :public class JunitTest3 extends AndroidTestCase {} 2.在AndroidManifest.xml清单文件中添 ...

  10. Java数据输入

    以下是数据输入实例: //以下是数据输入实例 import java.util.Scanner;//导入java.util.Scanner,Scanner首字母大写 public class Test ...