50分求助

P1439 【模板】最长公共子序列

Alviss_lky @ 2019-08-27 08:52:04

#include<bits/stdc++.h>
using namespace std;
template<typename T>inline void read(T&x)
{
    T ll=1;
    T res=0;
    char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-')
        ll=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    res=(res<<3)+(res<<1)+ch-'0',ch=getchar();
    x=res*ll;
}
template<typename T>void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)
    putchar(x+'0');
    else
    {
        print(x/10);
        putchar(x%10+'0');
    }
}
int n,x,a,s[10001],h[10001][4],m[3][2];
#define maxx(p,q) p>q?p:q
#define lowbit(x) x&-x
int qaq(int w)
{
    int d=0;
    while(w) 
    {
        d=maxx(s[w],d);
        w-=lowbit(w);
    }
    return d;
}
int main()
{
    read(n);
    int i,j;
    for(i=1;i<=n;i++)
    {
        read(x);
        h[x][0]++;
        h[x][h[x][0]]=i;
    }
    for(i=1;i<=n;i++)
    {
        read(a);
        for(j=1;j<=h[a][0];j++)
        {
            m[j][0]=h[a][j];
            m[j][1]=qaq(h[a][j]-1)+1;
        }
        for(j=1;j<=h[a][0];j++)
        {
            while(m[j][0]<=n) 
            {
                s[m[j][0]]=maxx(m[j][1],s[m[j][0]]);
                m[j][0]+=lowbit(m[j][0]);
            }
        }
    }
    print(qaq(n));
    return 0;
}

by Alviss_lky @ 2019-08-27 08:53:50

数组开小了,本帖到此结束


by 大略 @ 2019-08-27 09:03:58

@唐路凌最可爱 %%%


by Alviss_lky @ 2019-08-27 09:05:09

@大略 不许挖坟


|