您的位置:首页 > 其它

UVA - 10635 Prince and Princess 转化为 LIS

2017-08-09 10:40 363 查看
首先每个序列的各个数字互不相同,可以给 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: