Uva10635 Prince and Princess

题目戳这里
这题如果用\(f_{i,j}\)这样dp的话肯定过不了,必须另辟蹊径。题目说了数字不重复。我们先只留下两个数组共有的数字。然后我们处理出这样一个数组\(S\)\(S_i\)表示\(A_i\)这个元素在\(B\)中的下标,然后模型转换就成为了求\(S\)中最长上升子序列了,这个\(O(NlogN)\)的求法大家应该都会。这里我写的是树状数组版本的。

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

#define lowbit(x) (x&-x)
const int maxn = 250*250+10;
int pos[maxn],S[maxn],T,tree[maxn],N,P,Q,cnt,ans;

inline void ins(int a,int b) { for (;a <= N*N;a += lowbit(a)) tree[a] = max(tree[a],b); }
inline int calc(int a) { int ret = 0; for (;a;a -= lowbit(a)) ret = max(ret,tree[a]); return ret; }

inline int read()
{
    int ret = 0,f = 1; char ch;
    do ch = getchar(); while (!(ch >= '0'&&ch <= '9')&&ch != '-');
    if (ch == '-') ch = getchar(),f = -1;
    do ret = ret*10+ch-'0',ch = getchar(); while (ch >= '0'&&ch <= '9');
    return ret*f;
}

int main()
{
    freopen("10635.in","r",stdin);
    freopen("10635.out","w",stdout);
    scanf("%d",&T);
    for (int Case = 1;Case <= T;++Case)
    {
        printf("Case %d: ",Case);
        N = read(); P = read()+1; Q = read()+1; cnt = ans = 0;
        for (int i = 1;i <= N*N;++i) pos[i] = tree[i] = 0;
        for (int i = 1;i <= P;++i) pos[read()] = i;
        for (int i = 1,b;i <= Q;++i)
        {
            b = read(); 
            if (pos[b]) S[++cnt] = pos[b];
        }
        for (int i = 1;i <= cnt;++i)
        {
            int f = calc(S[i]-1)+1;
            ans = max(ans,f); ins(S[i],f);
        }
        printf("%d\n",ans);
    }
    fclose(stdin); fclose(stdout);
    return 0;
}

转载于:https://www.cnblogs.com/mmlz/p/6345463.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值