BZOJ2789 [Poi2012]Letters
2014-11-15 09:53
369 查看
恩、、蒟蒻只会写沙茶题了。。。唔~
这道题首先想到了逆序对,但是每个字母有多个们怎么办呢。。。
欸,对哦,必须是最近两个相同字母的进行配对,然后就可以搞出一个数列来了。。。
然后就没有然后了!(去年逆序对写错的蒟蒻不想再说逆序对的问题了。。。)
View Code
这道题首先想到了逆序对,但是每个字母有多个们怎么办呢。。。
欸,对哦,必须是最近两个相同字母的进行配对,然后就可以搞出一个数列来了。。。
然后就没有然后了!(去年逆序对写错的蒟蒻不想再说逆序对的问题了。。。)
/************************************************************** 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
相关文章推荐
- [bzoj2789] [Poi2012]Letters
- 【bzoj2789】 [Poi2012]Letters
- BZOJ 2789: [Poi2012]Letters( BIT )
- BZOJ 2789 Poi 2012 Letters 树状数组 逆序对
- 【BZOJ2789】[Poi2012]Letters 树状数组
- [BZOJ2789][Poi2012]Letters(树状数组求逆序对)
- 【BZOJ2789】【树状数组】[Poi2012]Letters 题解
- 【树状数组】bzoj2789 [Poi2012]Letters
- Bzoj 2789: [Poi2012]Letters 树状数组,逆序对
- BZOJ2789 : [Poi2012]Letters
- bzoj 2789 [Poi2012]Letters 贪心+逆序对
- bzoj 2789 [Poi2012]Letters 求逆序对
- BZOJ 2789: [Poi2012]Letters
- BZOJ 2789 Poi2012 Letters 树状数组
- bzoj 2789: [Poi2012]Letters 树状数组求逆序对
- [BZOJ 2789]POI2012 Letters
- 【POI2012】【BZOJ2789】Letters
- 【bzoj2789】[Poi2012]Letters
- BZOJ2789 [Poi2012]Letters 【树状数组】
- [bzoj2789][Poi2012]Letters_树状数组