【bzoj2789】 [Poi2012]Letters
来源:互联网 发布:php集成环境包 编辑:程序博客网 时间:2024/06/05 15:47
Description
给出两个长度相同且由大写英文字母组成的字符串A、B,保证A和B中每种字母出现的次数相同。
现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B。
Input
第一行一个正整数n (2<=n<=1,000,000),表示字符串的长度。
第二行和第三行各一个长度为n的字符串,并且只包含大写英文字母。
Output
一个非负整数,表示最少的交换次数。
Sample Input
3
ABC
BCA
ABC
BCA
Sample Output
2
HINT
ABC -> BAC -> BCA
Solve
树状数组求逆序对
Solve
树状数组求逆序对
#include<iostream>#include<cstdio>#include<vector>using namespace std;const int N=1000005;vector <int> wz[26];int sum[N],ss,n,x,len[26],now;char s1[N],s2[N];long long ans;void add(int i){for (;i<=n;i+=(i&(-i)))sum[i]++;}int query(int i){for (ss=0;i;i-=(i&(-i)))ss+=sum[i];return ss;}int main (){scanf ("%d%s%s",&n,s1+1,s2+1);for (int i=1;i<=n;++i)wz[s1[i]-'A'].push_back(i);for (int i=1;i<=n;++i){add(now=wz[s2[i]-'A'][len[s2[i]-'A']++]);ans+=i-query(now);}printf ("%lld",ans);return 0;}
阅读全文
0 0
- 【POI2012】【BZOJ2789】Letters
- BZOJ2789: [Poi2012]Letters
- BZOJ2789: [Poi2012]Letters
- 【bzoj2789】 [Poi2012]Letters
- 【bzoj2789】[Poi2012]Letters
- 【BZOJ2789】【树状数组】[Poi2012]Letters 题解
- [BZOJ2789][Poi2012]Letters(树状数组求逆序对)
- bzoj2789 letters 树状数组
- bzoj2789.Letters(贪心 && 逆序数)
- BZOJ 2789: [Poi2012]Letters
- [BZOJ 2789]POI2012 Letters
- bzoj 2789: [Poi2012]Letters
- 2789: [Poi2012]Letters
- 2789: [Poi2012]Letters
- BZOJ 2789: [Poi2012]Letters
- 【BZOJ】【P2789】【Poi2012】【Letters】【题解】【逆序对】
- BZOJ 2789 Poi2012 Letters 树状数组
- bzoj 2789 [Poi2012]Letters 树状数组
- hive桶表
- Python 字典(dict)细节及注意事项
- 验证一组数字是否magic square
- 1112. Stucked Keyboard (20)
- 数据内存存储
- 【bzoj2789】 [Poi2012]Letters
- HDUoj 5723 Abandoned country(最小生成树+dfs
- Codeforces Round #14 (Div. 2) D. Two Paths ( 树的直径
- 网络性能测试工具qperf使用
- 51 node 基础题
- RecyclerView系列(3)—ItemDirection打造粘性悬浮分组title
- UVA 1639——Candy(期望,精度处理)
- Android自实现百分比布局
- 关于word2016保存失败