UVa1481 Genome Evolution

看到全排列立马想到位置映射.

但是之后却卡住了, 想不出来怎么判断是否全部连续.

没办法, 暴力的写了一个vis数组判断, 果然TLE了.

看了看网上的代码, 其实挺简单的, 记录一下 l 和 r ,然后看看长度是否一致就ok.

我真傻啊

关于全排列的映射思想, 这儿也有一道不错的题  P1439 【模板】最长公共子序列

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 const int MAXN = 3e3 + 20;
 7 inline int read()
 8 {
 9     int x = 0; char ch = getchar();
10     while(!isdigit(ch)) ch = getchar();
11     while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
12     return x;
13 }
14 
15 int N;
16 int a[MAXN], b[MAXN];
17 
18 int main()
19 {
20     while(cin>>N, N)
21     {
22         for(int i = 1; i <= N; i++) a[read()] = i;
23         for(int i = 1; i <= N; i++) b[i] = a[read()];
24         int ans = 0;
25         for(int s = 1; s <= N; s++)
26         {
27             int k = 0, l = b[s], r = b[s];
28             for(int t = s + 1; t <= N; t++)
29             {
30                 l = min(l, b[t]);
31                 r = max(r, b[t]);
32                 ++k;
33                 if(r - l == k) ans++;
34             }
35         }
36         cout<<ans<<endl;
37     }
38     return 0;
39 }

猜你喜欢

转载自www.cnblogs.com/wsmrxc/p/9247663.html