首页 > 代码库 > BZOJ2789 [Poi2012]Letters
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 & -x15 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\n", ans);54 return 0;55 }
BZOJ2789 [Poi2012]Letters
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。