HDU 5904 LCIS __ dp、LCIS
来源:互联网 发布:sqlserver可视化工具 编辑:程序博客网 时间:2024/05/16 08:58
LCIS
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 710 Accepted Submission(s): 323
Problem Description
Alex has two sequences a1,a2,...,an and b1,b2,...,bm . He wants find a longest common subsequence that consists of consecutive values in increasing order.
Input
There are multiple test cases. The first line of input contains an integer T , indicating the number of test cases. For each test case:
The first line contains two integersn and m (1≤n,m≤100000) -- the length of two sequences. The second line contains n integers: a1,a2,...,an (1≤ai≤106) . The third line contains n integers: b1,b2,...,bm (1≤bi≤106) .
There are at most1000 test cases and the sum of n and m does not exceed 2×106 .
The first line contains two integers
There are at most
Output
For each test case, output the length of longest common subsequence that consists of consecutive values in increasing order.
Sample Input
33 31 2 33 2 110 51 23 2 32 4 3 4 5 6 11 2 3 4 51 121
Sample Output
150
Source
BestCoder Round #87
My Solution
dp、LCIS、 最长公共上升子序列且每次递增 1
状态定义:dpa[i] 表示以ai结尾的每次递增 1 的 LIS 的最大长度,
dpb[j] 表示以bi结尾的的每次递增 1 的 LIS 的最大长度,
边界:当 i == 1时, dpa[i] = 1, dpb[i] = 1;
状态转移方程: dpa[i] = dpa[Inda[a[i] - 1]] + 1;
dpa[i] = max(dpa[i], dpa[Inda[a[i]]]);
dpa[i] = max(dpa[i], dpa[Inda[a[i]]]);
Inda[a[i]] = i;
dpb[i] = dpb[Indb[b[i] - 1]] + 1;
dpb[i] = max(dpb[i], dpb[Indb[b[i]]]);
dpb[i] = max(dpb[i], dpb[Indb[b[i]]]);
Indb[b[i]] = i;
求ans:ans = max(ans, min(dpa[i], dpb[Indb[a[i]]])); // 1 <= i <= n
#include <iostream>#include <cstdio>#include <map>#include <vector>using namespace std;typedef long long LL;const int maxn = 1e5 + 8;int dpa[maxn], dpb[maxn], a[maxn], b[maxn];//map<int, vector<int> > Inda, Indb;map<int, int> Inda, Indb; //维护最后一个ai的下标int main(){ #ifdef LOCAL freopen("c.txt", "r", stdin); //freopen("c.out", "w", stdout); #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); int T, n, m, ans; cin >> T; while(T--){ cin >> n >> m; for(int i = 1; i <= n; i++){ cin >> a[i]; //Inda[a[i]].push_back(i); } for(int i = 1; i <= m; i++){ cin >> b[i]; //Indb[b[i]].push_back(i); } int sz; for(int i = 1; i <= n; i++){ if(i != 1){ dpa[i] = dpa[Inda[a[i] - 1]] + 1; dpa[i] = max(dpa[i], dpa[Inda[a[i]]]); } else dpa[i] = 1; Inda[a[i]] = i; } for(int i = 1; i <= m; i++){ if(i != 1){ dpb[i] = dpb[Indb[b[i] - 1]] + 1; dpb[i] = max(dpb[i], dpb[Indb[b[i]]]); } else dpb[i] = 1; Indb[b[i]] = i; } ans = 0; for(int i = n; i > 0; i--){ if(Indb[a[i]] != 0){ ans = max(ans, min(dpa[i], dpb[Indb[a[i]]])); } } cout << ans << endl; Inda.clear(); Indb.clear(); } return 0;}
Thank you!
------from ProLights 0 0
- HDU 5904 LCIS __ dp、LCIS
- 【HDU 5904 LCIS + dp】
- HDU-5904 LCIS(dp)
- HDU 5904 LCIS (DP)
- HDU 5904 LCIS (dp)
- hdu 5904 LCIS【dp】
- hdu 5904 LCIS(简单dp)
- HDU 5904 LCIS(dp)
- HDU 5904:LCIS(LCIS)
- Hdu 1904 LCIS (dp)
- HDU --- 5904 LCIS 【思维 + 简单dp】
- 【14.06%】【hdu 5904】LCIS
- HDU 5904 - LCIS
- HDU-5904-LCIS
- hdu 5904 LCIS
- HDOJ 5904 LCIS (dp)
- LCIS
- LCIS
- Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project 'myf'. java.
- CDatabase类
- 百度离线地图服务器搭建
- sqlserver---删除表数据drop、truncate和delete的用法
- HTML中设置input等文本框为不可操作
- HDU 5904 LCIS __ dp、LCIS
- 硬盘接口查看
- 96. Unique Binary Search Trees
- Java中面向对象的几个注意点
- 利用Matrix对图片进行变换以及实现动画方法浅谈
- ubuntu16.04下使用mongodb
- Myeclipse代码提示修改
- Discuz常用知识点总结
- 谷歌离线地图包下载