BZOJ 2789 Poi2012 Letters 树状数组
来源:互联网 发布:qq群踢人软件 编辑:程序博客网 时间:2024/06/04 19:22
题目大意:给定两个字符串A和B,每次可以交换A中相邻两个字符,求最少交换多少次后A可以变成B
首先我们需要找到A中每个字符和B中每个字符的对应关系
容易发现,同样的字符一定是按顺序一一对应的,因为交换两个字符的相对位置是需要代价的poi
然后就是树状数组求逆序对了poi
水水更健康poi
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 1001001using namespace std;struct abcd{int to,next;}table[M];int head[128],tot;int n,a[M];char s1[M],s2[M];void Add(int x,int y){table[++tot].to=y;table[tot].next=head[x];head[x]=tot;}namespace BIT{int c[M];void Update(int x){for(;x<=n;x+=x&-x)c[x]++;}int Get_Ans(int x){int re=0;for(;x;x-=x&-x)re+=c[x];return re;}}int main(){using namespace BIT;int i;cin>>n;scanf("%s%s",s1+1,s2+1);for(i=1;i<=n;i++)Add(s1[i],i);for(i=n;i;i--){a[i]=table[head[s2[i]]].to;head[s2[i]]=table[head[s2[i]]].next;}long long ans=0;for(i=n;i;i--){ans+=Get_Ans(a[i]);Update(a[i]);}cout<<ans<<endl;return 0;}
0 0
- BZOJ 2789 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
- 【BZOJ2789】【树状数组】[Poi2012]Letters 题解
- bzoj 2789 [Poi2012]Letters 求逆序对
- [BZOJ2789][Poi2012]Letters(树状数组求逆序对)
- 树状数组求逆序对-洛谷P3531 [POI2012]LIT-Letters
- BZOJ 2789 Poi 2012 Letters 树状数组 逆序对
- 2789: [Poi2012]Letters
- 2789: [Poi2012]Letters
- 【BZOJ】【P2789】【Poi2012】【Letters】【题解】【逆序对】
- bzoj2789 letters 树状数组
- BZOJ 2789 Letters
- 【POI2012】【BZOJ2789】Letters
- 黑马程序员——面向对象-Object类和包
- 如何处理Ibatis结合MySQL数据库使用时的事务操作
- Android--二维码
- HDU 1049 Climbing Worm
- BaseDaoImpl中使用泛型来反向获得当前的实体类
- BZOJ 2789 Poi2012 Letters 树状数组
- 回调的实现
- Windows窗口程序闪退问题
- 开心转转乐游戏正式上线了!
- w3School首次学习---Day01【CSS】
- UVa 10344 算23点
- 编译原理笔记(1)
- Django 表单介绍和查用方法
- 酒店之王