zoukankan      html  css  js  c++  java
  • UVa 10635

    In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below:
    Prince stands in square 1, make p jumps and finally reach square n*n. He enters a square at most once. So if we use xp to denote the p-th square he enters, then x1, x2, ... xp+1 are all different. Note that x1 = 1 and xp+1 = n*n. Princess does the similar thing - stands in square 1, make q jumps and finally reach square n*n. We use y1, y2 , ... yq+1 to denote the sequence, and all q+1 numbers are different.
    Figure 2 belows show a 3x3 square, a possible route for Prince and a different route for Princess.
    The Prince moves along the sequence: 1 --> 7 --> 5 --> 4 --> 8 --> 3 --> 9 (Black arrows), while the Princess moves along this sequence: 1 --> 4 --> 3 --> 5 --> 6 --> 2 --> 8 --> 9 (White arrow).
    The King -- their father, has just come. "Why move separately? You are brother and sister!" said the King, "Ignore some jumps and make sure that you're always together."
    For example, if the Prince ignores his 2nd, 3rd, 6th jump, he'll follow the route: 1 --> 4 --> 8 --> 9. If the Princess ignores her 3rd, 4th, 5th, 6th jump, she'll follow the same route: 1 --> 4 --> 8 --> 9, (The common route is shown in figure 3) thus satisfies the King, shown above. The King wants to know the longest route they can move together, could you tell him?
    Input 
    The first line of the input contains a single integer t(1 <= t <= 10), the number of test cases followed. For each case, the first line contains three integers n, p, q(2 <= n <= 250, 1 <= p, q < n*n). The second line contains p+1 different integers in the range [1..n*n], the sequence of the Prince. The third line contains q+1 different integers in the range [1..n*n], the sequence of the Princess.【,,,,,嘤语不好】
    大意 

             一个n*n的棋盘(2 <= n <= 250),一个p+1个数的数组,各个数互不相同,第一个数是1,最后 一个数是n*n;一个q+1个数的数组,各个数互不相同,第一个数是1,最后 一个数是n*n;1 <= p,q < n*n;问这两个数组的LCS。

    分析

      刚开始看这么长一题吓一跳,读了一边题,害,这不就是求最长公共子序列吗。然后就这么写了,tle

      直接迷惑???       这咋优化???

      又看了一遍,发现一个有用的条件,他到达的格点序号没有重复的,利用这个条件,可以把这个问题转化成求lis,而lis有更快的算法(nlogn的二分查找贪心)

      设两个人的路径分别为 a[], b[]

        我们把 a 从头到尾扫一遍, 用 g[ a[ i ] ] 表示 a[ i ] 在数组中对应的位置

      再把 b 从头到尾扫一遍 ,如果 g[ b[ i ] ] 不等于零,说明这是 a 和 b 共有的元素,我们就把这个位置存到数组 c [ ] 里面

      完毕之后,我们在 c [ ] 中求一下 LIS,因为 c 中的位置序号必须上升,b 的那一部分路径才能和 a 对应起来。

      配合代码食用便于理解

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    using namespace std;
    const int maxn = 62505;
    const int inf = 0x3f3f3f3f;
    int a[maxn], b[maxn], n, p, q, f[255][255], c[maxn], g[maxn], low[maxn];
    int main(){
        int t; scanf("%d", &t);
        for(int cnt=1; cnt<=t; cnt++){
            memset(a, 0, sizeof(a));
            memset(b, 0, sizeof(b));
            memset(c, 0, sizeof(c));
            memset(g, 0, sizeof(g));
            memset(f, 0, sizeof(f));
            scanf("%d%d%d", &n, &p, &q);
            for(int i=1; i<=p+1; i++) scanf("%d", &a[i]), g[a[i]] = i; //记录位置
            for(int i=1; i<=q+1; i++) scanf("%d", &b[i]);
            int tot = 0;
            for(int i=1; i<=q+1; i++) if(g[b[i]]) c[++tot] = g[b[i]]; //将共同元素的位置存入c
            memset(low, 0x3f, sizeof(low));
            low[1] = c[1];
            int ans = 1;
            for(int i=2; i<=tot; i++){ //求 LIS
                if(c[i] > low[ans]) low[++ans] = c[i];
                else{
                    int x = lower_bound(low+1, low+ans+1, c[i]) - low;
                    low[x] = c[i];
                }
            }
            printf("Case %d: %d
    ", cnt, ans);
        }
        return 0;
    }
  • 相关阅读:
    Chrome开发者工具详解(1)
    Chrome开发者工具详解(2)
    Ubuntu ADSL拨号上网
    Bash中单引号和双引号的区别
    建立菜单
    波浪号和Hyphen扩展
    标准IO和重定向
    Bash变量扩展修改符
    mysql主键约束和唯一性约束
    Here文档
  • 原文地址:https://www.cnblogs.com/hzoi-poozhai/p/12662873.html
Copyright © 2011-2022 走看看