BZOJ2789 [Poi2012]Letters

恩、、蒟蒻只会写沙茶题了。。。唔~

这道题首先想到了逆序对,但是每个字母有多个们怎么办呢。。。

欸,对哦,必须是最近两个相同字母的进行配对,然后就可以搞出一个数列来了。。。

然后就没有然后了!(去年逆序对写错的蒟蒻不想再说逆序对的问题了。。。)

 1 /**************************************************************
 2     Problem: 2789
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:1060 ms
 7     Memory:8704 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <cstring>
12 #include <queue>
13  
14 #define lowbit(x) x & -x
15 using namespace std;
16 const int N = 1000005;
17 int n;
18 int val, BIT[N];
19 long long ans;
20 queue <int> q[26];
21  
22 int read() {
23     char ch = getchar();
24     while (ch < 'A' || 'Z' < ch)
25         ch = getchar();
26     return ch - 'A';
27 }
28  
29 void add(int x) {
30     while (x <= n)
31         ++BIT[x], x += lowbit(x);
32 }
33  
34 int query(int x) {
35     int res = 0;
36     while (x)
37         res += BIT[x], x -= lowbit(x);
38     return res;
39 }
40  
41 int main() {
42     scanf("%d", &n);
43     int i, t;
44     for (i = 1; i <= n; ++i)
45         q[t = read()].push(i);
46     for (i = 1; i <= n; ++i) {
47         t = read();
48         val = q[t].front();
49         q[t].pop();
50         ans += query(n) - query(val);
51         add(val);
52     }
53     printf("%lld
", ans);
54     return 0;
55 }
View Code
By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
原文地址:https://www.cnblogs.com/rausen/p/4098897.html