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 integers n and m (1n,m100000) -- the length of two sequences. The second line contains n integers: a1,a2,...,an (1ai106). The third line contains n integers: b1,b2,...,bm        (1bi106).

There are at most 1000 test cases and the sum of n and m does not exceed 2×106.
 

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]]]);
                           Inda[a[i]] = i;
                           dpb[i] = dpb[Indb[b[i] - 1]] + 1;
                           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
原创粉丝点击