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

相关文章

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

关于最长递增子序列问题概述

《关于最长递增子序列问题概述》本文详细介绍了最长递增子序列问题的定义及两种优化解法:贪心+二分查找和动态规划+状态压缩,贪心+二分查找时间复杂度为O(nlogn),通过维护一个有序的“尾巴”数组来高效... 一、最长递增子序列问题概述1. 问题定义给定一个整数序列,例如 nums = [10, 9, 2

poj3261(可重复k次的最长子串)

题意:可重复k次的最长子串 解题思路:求所有区间[x,x+k-1]中的最小值的最大值。求sa时间复杂度Nlog(N),求最值时间复杂度N*N,但实际复杂度很低。题目数据也比较水,不然估计过不了。 代码入下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstring

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin