uva10635 Prince and Princess(LCS转LIS)
来源:互联网 发布:win7固态硬盘开机优化 编辑:程序博客网 时间:2024/04/28 02:31
明面好似是用LCS做,但是开一个(250*250)^2数组明显爆了,规模太大,而且时间复杂度肯定会超时的。看了一下其他解题报告才懂,好巧妙地转化为LIS。
思路:首先将王子的序列(也就是第一个序列)看作一个相对于公主的序列(第二个数列)是一个标准衡量的序列,就是将第一个序列按照输入的顺序定义为1,2,3,……,n,只不过值的定义不同了,就如:
第一个数列为:1,7,5,4,8,3,9
我们按这个顺序重新把他们看作1,2,3,……,9;
这是1的值为1,2的值为7,3的值为5……
然后第二个序列相对于第一个序列一定是递增的,所以重新建一个新的序列,求出的LIS即使答案,LIS的二分法做。
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#include <iostream>#include <queue>#include <stack>#include <vector>#include <map>#include <set>#include <string>using namespace std;#define maxn 250*250+1int dp[maxn],p[maxn],q[maxn];int main(){ int i,k,temp,cas=1; int n,a,b,t; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&a,&b); memset(p,-1,sizeof(p)); for(i=1;i<=a+1;i++) { scanf("%d",&k); p[k]=i; /*重新定义第一个序列*/ } for(i=1;i<=b+1;i++) { scanf("%d",&q[i]); q[i]=p[q[i]]; /*第二个序列相对于第一个序列的顺序*/ } temp=k=0; dp[temp]=q[1]; for(i=2;i<=b+1;i++) { if(q[i]==-1)continue; if(q[i]>dp[temp]) dp[++temp]=q[i]; else { k=lower_bound(dp,dp+temp,q[i])-dp;/*stl:二分法*/ dp[k]=q[i]; } } printf("Case %d: %d\n",cas,temp+1); cas++; }}
0 0
- UVa10635--Prince and Princess(LCS转LIS)
- uva10635 Prince and Princess(LCS转LIS)
- uva10635 Prince and Princess LCS 变 lIS
- 【动态规划14】UVA10635 Prince and princess(LCS转LIS)
- Uva10635 Prince and Princess(LIS)
- UVA10635 Prince and Princess(LIS)
- Uva - 10635 - Prince and Princess(LCS转LIS)
- UVa 10635 - Prince and Princess(LCS转LIS)
- uva 10635 Prince and Princess(LCS转LIS~)
- UVa10635 Prince and Princess ACM解题报告(LCS与LIS的转化+单调栈+二分查找代替DP)
- UVa10635 Prince ans Princess(LCS)
- UVA10635- Prince and Princess
- UVa10635 - Prince and Princess
- uva10635 - Prince and Princess
- UVa10635 Prince and Princess
- UVA 10635 Prince and Princess (LCS优化转LIS)
- UVa 10635 Prince and Princess / LCS 转 LIS
- uva 10635Prince and Princess (LCS转LIS)
- SqlSever获取数据库表名、字段名
- 微软远程桌面(安卓版)连接出现0x204错误的解决办法
- Y400中通过easybcd在win7下面的安装Ubuntu14
- Java之随机数
- Linux 编译报错 undefined reference to `pthread_create'
- uva10635 Prince and Princess(LCS转LIS)
- 计算几何 uva11117 Morley's Theorem
- ActiveMQ LevelDB持久化机制
- *LeetCode-Permutation
- 将VS2012的项目转化为VS2010
- 在Android studio中建立Java工程
- TSP问题
- 关于C#类中重写ToString方法和PHP类中__tostring()方法的比较
- PageAdapter不能刷新问题