UVaLive LA 5052 UVa 1481 - Genome Evolution (很巧妙的思维)

Xi, a developmental biologist is working on developmental distances of chromosomes. A chromosome, in the Xi's simplistic view, is a permutation from n genes numbered 1 to n. Xi is working on an evolutionary distance metric between two chromosomes. In Xi's theory of evolution any subset of genes lying together in both chromosomes is a positive witness for chromosomes to be similar.

A positive witness is a pair of sequence of the same length A and A', where A is a consecutive subsequence of the first chromosome, A' is a consecutive subsequence of the second chromosome, and A is a permutation of A'. The goal is to count the number of positive witnesses of two given chromosomes that have a length greater than one.

Input 

There are several test case in the input. Each test case starts with a line containing the number of genes (2$ \le$n$ \le$3000). The next two lines contain the two chromosomes, each as a list of positive integers. The input terminates with a line containing ``0'' which should not be processed as a test case.

Output 

For each test case, output a single line containing the number of positive witness for two chromosomes to be similar.

Sample Input 

4 
3 2 1 4 
1 2 4 3 
5 
3 2 1 5 4
3 2 1 5 4
0

Sample Output 

3 
10

题意:

给出1-n的两个排列A和B,统计有多少二元组(A',B')满足:A' B' 分别是A B的子序列,且A' B'包含的整数集完全相同 A' B'均应至少包含两个元素


思路:

我没有想出来,看了网上的题解,发现智商是硬伤啊

补充一下大白上面题意 A  B序列均没有重复元素 所以才可以这有做

直接枚举A的所有子序列O(n^2) 同时A的子序列subA的元素在B中出现的位置的最大值和最小值

如果位置的最大值和最小值之差+1==len(subA) 那么这个就是一个解 因为没有重复元素!



#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 3000 + 20;
int A[maxn];
int B[maxn];

int main() {
    int n;

    while(scanf("%d", &n) != EOF && n) {
        for(int i=1; i<=n; i++) scanf("%d", &A[i]);
        for(int i=1; i<=n; i++) {
            int t;
            scanf("%d", &t);
            B[t] = i;
        }
        int ans = 0;
        for(int i=1; i<n; i++) {
            int minp = B[A[i]];
            int maxp = B[A[i]];
            for(int j=i+1; j<=n; j++) {
                int p = B[A[j]];
                minp = min(minp, p);
                maxp = max(maxp, p);
                if(maxp - minp == j - i)
                    ans++;
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值