现在的位置: 首页 > 综合 > 正文

10635 – Prince and Princess UVA

2013年06月07日 ⁄ 综合 ⁄ 共 4542字 ⁄ 字号 评论关闭

Prince and Princess
Input: 
Standard Input

Output: Standard Output

Time Limit: 3 Seconds

 


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, y, ... 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.

 

Output 

For each test case, print the case number and the length of longest route. Look at the output for sample input for details.

 

Sample Input                           Output for Sample Input

1

3 6 7

1 7 5 4 8 3 9

1 4 3 5 6 2 8 9

Case 1: 4


关键:

本题的实质意思就是求两个序列的最长公共子序列!!!

对于输入的两个序列,任取其中之一,将其中的n个元素

用1~n来标记!!!!!(原来有序现在也是有序的)

然后对于另外一个序列:

如果其中的元素在第一个序列中不出现则记为0;

将两个序列以一对应得到另一组标记。

那么本题的答案就是第二组标记中满足从左到右依次递减的

数的最大个数!!!!!!!!!!(利用函数(lower_bound)实现)

代码如下:

#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int INF  = 1000000000;
int num[63000];
int S[63000];
int temp[63000];
int main()
{
    int t;
    scanf("%d",&t);
    for(int i = 0;i<t;++i)
    {
        int N,p,q;
        scanf("%d%d%d",&N,&p,&q);

        memset(num,0,sizeof(num));

        int x;
        for(int j = 1;j<=p+1;++j)
        {
            scanf("%d",&x);
            num[x] = j;
        }

        int n = 0;
        for(int j = 1;j<=q+1;++j)
        {
            scanf("%d",&x);
            if(num[x])
            {
            S[n] = num[x];
            ++n;
            }
        }
       for(int j = 0;j < n;++j)
       temp[j] = INF;

       int ans = 0;
       for(int j = 0;j<n;++j)
       {
           int k = lower_bound(temp,temp+n,S[j]) - temp;
           temp[k] = S[j];
           ans = max(ans,k);

       }

      printf("Case %d: %d\n",i+1,ans+1);
    }
    return 0;
}

方法2:

/*
 * Author:  *****
 * Created Time:  2013/9/20 12:30:17
 * File Name: A.cpp
 * solve: A.cpp
 */
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<iostream>
#include<vector>
#include<queue>
//ios_base::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x,0,sizeof(x))
#define clrs( x , y ) memset(x,y,sizeof(x))
#define out(x) printf(#x" %d\n", x)
#define sqr(x) ((x) * (x))
typedef long long LL;

const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 63500;

int sgn(const double &x) {  return (x > eps) - (x < -eps); }
int  n;
int find(int ord[],int len,int tar)
{
   int L = 0;
   int R = len;
   int mid;
   while(L <= R)
   {
       mid = L + (R - L )/2;
       if(ord[mid]== tar)
       return mid;
       else
       if(tar < ord[mid])
       {
           R = mid - 1;
       }else
       {
           L = mid + 1;
       }
   }
   return L;
}
int LIS(int num[],int ord[])
{
    ord[0] = -1;
    ord[1] = num[0];
    int len = 1;
    int j;
    for(int i = 0;i<n;++i)
    {
        if(num[i] > ord[len])
        {
            len++;
            j = len;
        }
        else
        {
            j = find(ord,len,num[i]);
        }
        ord[j] = num[i];
    }
    return len;
}
int temp[maxn];
int num[maxn];
int ord[maxn];
int main() 
{
    //freopen("in.txt","r",stdin);
    int p,q;
    int T;
    scanf("%d",&T);
    int k = 1;
    while(T--)
    {
        scanf("%d%d%d",&n,&p,&q);
        n = n*n;
        int cnt = 1;
        rep(i,0,p+1)
        {
            int a;
            scanf("%d",&a);
            temp[a] = cnt;
            cnt++;
        }

        rep(i,0,q+1)
        {
            int a;
            scanf("%d",&a);
            if(temp[a])
            {
                num[i] = temp[a];
            }else
                num[i] = 0;
        }
         printf("Case %d: ",k++);
        cout<<LIS(num,ord)<<endl;
    }

    return 0;
}

如果数字个数少一点也可以用动态规划来做!

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include <math.h>
using namespace std;
int num1[63000];
int num2[63000];
int d[63000][63000];
int main()
{
    int t;
    scanf("%d",&t);
    for(int k = 1;k<=t;++k)
    {
        int n,p,q;
        scanf("%d%d%d",&n,&p,&q);
        for(int i = 1;i<=p+1;++i)
         scanf("%d",&num1[i]);
        for(int i = 1;i<=q+1;++i)
         scanf("%d",&num2[i]);
         memset(d,0,sizeof(d));
        for(int i = 1;i<=p+1;++i)
          for(int j = 1;j<=q+1;++j)
            if(num1[i]==num2[j])
              d[i][j] = d[i-1][j-1] + 1;
              else
              d[i][j] = max(d[i-1][j],d[i][j-1]);
        printf("Case %d:%d\n",k,d[p+1][q+1]);


    }

    return 0;
}

抱歉!评论已关闭.