uva 10635 Prince and Princess LIS
来源:互联网 发布:c语言中 代表什么 编辑:程序博客网 时间:2024/06/06 00:20
题意:有两个长度为p+1,和q+1的序列,第一个都为1,且元素都不相同,求最长公共子序列。
想法:总序列长度可以达到250^2,不能用O(pq)的方法求LCS,而发现所有元素都不相同,则可以将A重新编号为1,2,3,4 ……,然后将B映射,求LIS即可,A中不存在的在B中映射为0,可以直接求LIS,因为元素均不相同,而且第一个肯定是最小的为1,即便0后面更新,也只能更新长度为1的最小值。
#include<cstdio>#include<cstring>#include<algorithm>#define mem(name,value) memset(name,value,sizeof(name))#define FOR(i,n) for(int i=1;i<=n;i++)using namespace std;const int maxn = 255*255;const int inf = 0x3f3f3f3f;int A[maxn],B[maxn],g[maxn],d[maxn],h[maxn];int LIS(int *a,int n){ mem(g,inf); int ans = 0; for(int i=0;i<n;i++){ int k = lower_bound(g+1,g+n+1,a[i]) - g; ans = max(ans,k); g[k] = a[i]; d[i] = k; } return ans;}int main(){ int T,n,p,q,x; scanf("%d",&T); int kase = 0; while(T--){ printf("Case %d: ",++kase); mem(h,0); scanf("%d%d%d",&n,&p,&q); FOR(i,p+1) scanf("%d",&A[i]); FOR(i,p+1) h[A[i]] = i; int cnt = 0; for(int i=0;i<q+1;i++){ scanf("%d",&x); if(h[x]) B[cnt++] = h[x]; } int ans = LIS(B,cnt); printf("%d\n",ans); }}/*不存在的为0 不删除也可以,因为数不相同,而且第一个肯定是最小的,即便后来有0,也只会更新长度为1最小值,更新不了LIS;*/#include<cstdio>#include<cstring>#include<algorithm>#define mem(name,value) memset(name,value,sizeof(name))#define FOR(i,n) for(int i=1;i<=n;i++)using namespace std;const int maxn = 255*255;const int inf = 0x3f3f3f3f;int A[maxn],B[maxn],g[maxn],d[maxn],h[maxn];int LIS(int *a,int n){ mem(g,inf); int ans = 0; for(int i=1;i<=n;i++){ int k = lower_bound(g+1,g+n+1,a[i]) - g; ans = max(ans,k); g[k] = a[i]; d[i] = k; } return ans;}int main(){ int T,n,p,q; scanf("%d",&T); int kase = 0; while(T--){ printf("Case %d: ",++kase); mem(h,0); scanf("%d%d%d",&n,&p,&q); FOR(i,p+1) scanf("%d",&A[i]); FOR(i,q+1) scanf("%d",&B[i]); FOR(i,p+1) h[A[i]] = i; FOR(i,q+1) B[i] = h[B[i]]; int ans = LIS(B,q+1); printf("%d\n",ans); }}
0 0
- Uva 10635 - Prince and Princess(LIS)
- UVA 10635 Prince and Princess DP LIS
- uva 10635 Prince and Princess LIS
- UVA 10635 Prince and Princess lcs--》lis
- UVa 10635 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
- uva 10635 Prince and Princess (将LCS 转化为 LIS)
- uva 10635Prince and Princess (LCS转LIS)
- UVA 10635 - Prince and Princess LCS转化为LIS
- UVA 10635 - Prince and Princess (LCS转成LIS DP)
- UVA - 10635 Prince and Princess LCS转LIS
- UVA - 10635 - Prince and Princess (LCS转化为LIS)
- UVA 10635 - Prince and Princess ( LCS 转换为LIS )
- UVa 10635 - Prince and Princess(LCS转LIS)
- uva 10635-Prince and Princess 【LIS 求解 LCS】
- uva-10635 Prince and Princess LCS转化为LIS
- 十四、Shell if else语句
- Lua源代码阅读(四)tm元方法优化
- Android好奇宝宝_09_Handler Looper Message
- PCI总线地址空间与系统地址空间的关系
- C#和oracle编程
- uva 10635 Prince and Princess LIS
- hdu 2680 Choose the best route (Dijkstra & 反向图)
- PYQT 实现TabWidget 双击关闭功能 (附源码)
- Silverlight调用WCF服务引用示例
- 关于Ext的groupView的小组之间的排序问题
- Linux 缺页中断发展历史
- Editplus格式化xml
- Dijkstra's Shortest-Path Algorithm Implementation(TODO)
- 积累笔记--总结最简单的异步表单提交方法