CF463D Gargari and Permutations (LCS)

The main problem is that this information, which is an arrangement, rather than the number of columns

That is arranged for each row is the number of 1-n, but different positions, we observed only 1000 n

And taking into account for a longest common subsequence, i.e., if the number of two sequences can be public, then one number in each row is another number in the back

So we designed dp status f [i] represents the longest common subsequence i ending, at the time of the transfer, there have been only k times before they can be transferred, f [j] from the front and transferred from existing, or else 1, and j each row in front of each row i

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<set>
#define ull unsigned long long
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int N=1e5+10;
int f[N];
int pos[6][N];
int a[6][N];
int cnt[N];
vector<int> num;
int main(){
    int n,k;
    cin>>n>>k;
    int i;
    int j;
    for(i=1;i<=k;i++){
        for(j=1;j<=n;j++){
            scanf("%d",&a[i][j]);
        }
    }
    int res=0;
    for(i=1;i<=n;i++){
        for(j=1;j<=k;j++){
            int sign=a[j][i];
            cnt[sign]++;
            pos[j][sign]=i;
            if(cnt[sign]==k){
                int l;
                if(num.empty()){
                    f[sign]=1;
                }
                else{
                    for(l=0;l<num.size();l++){
                        int r;
                        int flag=0;
                        for(r=1;r<=k;r++){
                            if(pos[r][sign]<pos[r][num[l]]){
                                flag=1;
                                break;
                            }
                        }
                        if(flag)
                            f[sign]=max(f[sign],1);
                        else{
                            f[sign]=max(f[sign],f[num[l]]+1);
                        }
                    }

                }
                num.push_back(sign);
                res=max(res,f[sign]);
            }
        }
    }
    cout<<res<<endl;
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/ctyakwf/p/12637274.html