UVA - 10635 Prince and Princess 转化为 LIS

首先每个序列的各个数字互不相同,可以给 a 串中的每个数字标一个编号(1 - n)
然后(把 b 中的每个数字也附上编号,)按照 a 中的数字所对应的编号,给 b 中每个数字附上相应的编号
这样 只需要求 b 编号序列的 最长上升子序列长度就好了
因为题中说了 第一个数字是1,b的第一个编号一定是1,所以不用担心 b 中出现而a 中没有的数字编号为0 会影响结果
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
// cout << "  ===  " << endl;

using namespace std;
typedef long long ll;
const int maxn = 100000 + 7, INF = 0x3f3f3f3f, mod = 1e9+7;
int T, n, p, q, ans;
int a[maxn], b[maxn], c[maxn], d[maxn];

void init() {
    memset(a, 0, sizeof a);
    memset(b, 0, sizeof b);
    memset(c, 0, sizeof c);
    for(int i = 1; i <= p; ++i)
        scanf("%d", &a[i]);
    for(int i = 0; i < q; ++i)
        scanf("%d", &b[i]);

    for(int i = 1; i <= p; ++i)
        c[a[i]] = i;
    for(int i = 0; i < q; ++i)
        b[i] = c[b[i]];
}

void solve() {
    memset(d, INF, sizeof d);
    for(int i = 0; i < q; ++i)
        *lower_bound(d, d+q+1, b[i]) = b[i];
    int ans = lower_bound(d, d+q+1, INF) - d;
    printf("%d\n", ans);
}

int main() {
    scanf("%d", &T);
    int kase = 1;
    while(T--) {
        scanf("%d%d%d", &n, &p, &q);
        p++, q++;
        init();
        printf("Case %d: ", kase++);
        solve();
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值