您的位置:首页 > 其它

BZOJ2789 [Poi2012]Letters

2014-11-15 09:53 369 查看
恩、、蒟蒻只会写沙茶题了。。。唔~

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

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

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

/**************************************************************
Problem: 2789
User: rausen
Language: C++
Result: Accepted
Time:1060 ms
Memory:8704 kb
****************************************************************/

#include <cstdio>
#include <cstring>
#include <queue>

#define lowbit(x) x & -x
using namespace std;
const int N = 1000005;
int n;
int val, BIT
;
long long ans;
queue <int> q[26];

int read() {
char ch = getchar();
while (ch < 'A' || 'Z' < ch)
ch = getchar();
return ch - 'A';
}

void add(int x) {
while (x <= n)
++BIT[x], x += lowbit(x);
}

int query(int x) {
int res = 0;
while (x)
res += BIT[x], x -= lowbit(x);
return res;
}

int main() {
scanf("%d", &n);
int i, t;
for (i = 1; i <= n; ++i)
q[t = read()].push(i);
for (i = 1; i <= n; ++i) {
t = read();
val = q[t].front();
q[t].pop();
ans += query(n) - query(val);
add(val);
}
printf("%lld\n", ans);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: