HDU-1025-动规-最长上升子序列

2024-06-16 01:48

本文主要是介绍HDU-1025-动规-最长上升子序列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/*转载请注明出处:乄心-小黄豆http://blog.csdn.net/wuxinxiaohuangdou*/

题目大意:贫穷城市去富裕城市 进口资源要建公路,但不允许交叉,求最多能建几条公路?

Input:  n行,每行p(贫穷城市)r(富裕城市)。

Output: 最多建几天公路?按格式输出。

转化一下,容易看出是求 最长上升子序列(LIS).

第一种方法:/*140MS 4304K  AC  最长上升子序列O(nlogn)二分算法*/

算法精髓:维护一个一维数组!

#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 500010
int City[MAX];
int count=1;
int bsearch(int *c,int n,int a)  //二分找更具潜力的值! 
{        /*找到了则表示比起原先值,现在的值将能够得到更长的升序子序列,因为现在的值更小,向序列尾走的更远些!*/int j=0;int l=1,r=n;while(l<=r){int mid=(l+r)>>1;            if(c[mid]<a&&a<=c[mid+1]){j=mid+1;break;}else if(a<c[mid]){j=mid;                //j一定要保存一下这时的值!因为你需要的不一定满足第一个if语句!r=mid-1;}else l=mid+1;}return j;
}
int LIS(int *a,int n)
{int i,j,size=1;int *c=new int[n+1];//键值表示子序列长度,映射表示这个长度下的子序列最小尾数int *dp=new int[n+1];c[1]=a[1];dp[1]=1;for(i=2;i<=n;i++){if(a[i]>=c[size])  //如果比此时 最大的升序子序列长度 所对应的最小值 大的话 ,直接把长度加1.j=++size;elsej=bsearch(c,size,a[i]); //否则 去寻找能够用a[i]来更新的 最小尾数! c[j]=a[i];dp[i]=j;}return size;
}
int main()
{int n;while(~scanf("%d",&n)){int p,r;for(int i=1;i<=n;i++){scanf("%d%d",&p,&r);City[p]=r;}if(LIS(City,n)==1)cout<<"Case "<<count++<<":\n"<<"My king, at most 1 road can be built."<<endl<<endl;elsecout<<"Case "<<count++<<":\n"<<"My king, at most "<<LIS(City,n)<<" roads can be built."<<endl<<endl;}return 0;
}


第二种方法:以下是LIS的 O(n^2)的做法.   超时!!

#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 500010
int City[MAX];
int count=1;
int LIS(int *a,int n)
{int i,j,m=0,ans=1;int *dp=new int[n+1];dp[1]=1;for(i=2;i<=n;i++)       // 从序列头枚举到尾!{m=0;for(j=1;j<i;j++)   //枚举此时位置之前的子序列 的各个dp值即(升序子序列长度){if(dp[j]>m&&a[j]<a[i]) m=dp[j];}dp[i]=m+1;        //得到i位置的dp值!if(ans<dp[i])ans=dp[i];}return ans;
}
using namespace std;
int main()
{int n;while(~scanf("%d",&n)){int p,r;for(int i=1;i<=n;i++){scanf("%d%d",&p,&r);City[p]=r;}if(LIS(City,n)==1)cout<<"Case "<<count++<<":\n"<<"My king, at most 1 road can be built."<<endl<<endl;elsecout<<"Case "<<count++<<":\n"<<"My king, at most "<<LIS(City,n)<<" roads can be built."<<endl<<endl;}return 0;
}


 

这篇关于HDU-1025-动规-最长上升子序列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1065179

相关文章

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测 目录 时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测基本介绍程序设计参考资料 基本介绍 MATLAB实现LSTM时间序列未来多步预测-递归预测。LSTM是一种含有LSTM区块(blocks)或其他的一种类神经网络,文献或其他资料中LSTM区块可能被描述成智能网络单元,因为

代码随想录——摆动序列(Leetcode376)

题目链接 贪心 class Solution {public int wiggleMaxLength(int[] nums) {if(nums.length <= 1){return nums.length;}// 当前一对差值int cur = 0;// 前一对差值int pre = 0;// 峰值个数int res = 1;for(int i = 0; i < nums.length -

想让Python序列切片更高效?这些技巧你不可不知!

目录 1、自定义类实现切片 🍏 1.1 实现__getitem__方法 1.2 支持正负索引与步长 2、利用 collections.abc 模块 🧠 2.1 继承MutableSequence类 2.2 重写关键方法 3、使用标准库itertools.slice 🍲 3.1 itertools工具介绍 3.2 slice函数应用实例 4、通过生成器实现动态切片 🌀

上海邀请赛 A题目 HDU 5236(dp)

先求出没有ctrl+s的时候构造长度为i的期望f[i] 。然后枚举保存的次数,求出最小即可。 #include<cstdio>#include<cstdio>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<iostream>#include<map>

hdu 2586 树上点对最近距离 (lca)

,只要知道dis[i][j]=dis[i][root]+dis[j][root]-2*dis[Lca(i,j)][root].   其中root为树的根节点,LCA(i,j)为i,j的最近公共祖先。 所以我们先把所有的询问储存下来,然后离线直接查询。复杂度是o(n+q)的。 VIE #include<cstdio>#include<algorithm>#include<i

最长考拉兹序列

题目:  考虑如下定义在正整数集上的迭代规则:  n    n/2 (若n为偶数) n    3n+1 (若n为奇数) 从13开始,可以迭代生成如下的序列:         13  40  20  10  5  16  8  4  2  1 可以看出这个序列(从13开始到1结束)共有10项。 尽管还未被证明,但普遍认为,从任何数开始最终都能抵达1并结束, 这被称为 “考拉兹序列”。

leetcode刷题(97)——106. 从中序与后序遍历序列构造二叉树

根据一棵树的中序遍历与后序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 中序遍历 inorder = [9,3,15,20,7]后序遍历 postorder = [9,15,7,20,3] 返回如下的二叉树: 3/ \9 20/ \15 7 看下后序和中序遍历的框架: void traverse(TreeNode root) {trave

leetcode刷题(97)——105. 从前序与中序遍历序列构造二叉树

根据一棵树的前序遍历与中序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: 3/ \9 20/ \15 7 1.先回顾前序遍历和中序遍历的框架: void traverse(TreeNode root) {//

根据序列:2/1,3/2,5/3,...生成前30项打印出并求和

根据斐波那契数列 def fab(max): def fib_loop_while(max):max = maxa, b = 0, 1while max &

biostar handbook(五)|序列从何而来和质量控制

测序仪 2017年一篇发表在Nature的综述"DNA sequencing at 40: past, present and future"介绍了DNA测序这40年的发展历程。1976年,Sanger和Coulson同时发表了2种方法用于对上百个DNA碱基进行解码,这就是第一代测序技术。到了2005年,罗氏的454平台揭开了高通量测序的序幕,后面则是SOLiD,454和Illumina三方对抗