洛谷P3531 [POI2012]LIT-Letters
题目描述
Little Johnny has a very long surname.
Yet he is not the only such person in his milieu.
As it turns out, one of his friends from kindergarten, Mary, has a surname of the same length, though different from Johnny's.
Moreover, their surnames contain precisely the same number of letters of each kind - the same number of letters A, same of letters B, and so on.
Johnny and Mary took to one another and now often play together.
One of their favourite games is to gather a large number of small pieces of paper, write successive letters of Johnny's surname on them, and then shift them so that they obtain Mary's surname in the end.
Since Johnny loves puzzles, he has begun to wonder how many swaps of adjacent letters are necessary to turn his surname into Mary's. For a child his age, answering such question is a formidable task.
Therefore, soon he has asked you, the most skilled programmer in the kindergarten, to write a program that will help him.
给出两个长度相同且由大写英文字母组成的字符串A、B,保证A和B中每种字母出现的次数相同。
现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B。
输入输出格式
输入格式:
In the first line of the standard input there is a single integer (
) denoting the length of Johnny's surname.
The second line contains Johnny's surname itself, i.e., contains its successive letters (without spaces).
The third line contains Mary's surname in the same format: a string of letters (with no spaces either).
Both strings consist only of capital (upper-case) letters of the English alphabet.
In tests worth 30% of points it additionally holds that .
输出格式:
Your program should print a single integer to the standard output: the minimum number of swaps of adjacent letters that transforms Johnny's surname into Mary's.
输入输出样例
说明
给出两个长度相同且由大写英文字母组成的字符串A、B,保证A和B中每种字母出现的次数相同。
现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B。
我们可以先考虑一下小的数据范围
如果只有A B两个字母
那么只有两种情况
A B \nA B 对于这种情况我们是不用进行交换的
- A B \nB A 这种情况只需交换一次即可
为了方便讨论,我们把 A B按照顺序标号为1 2
那么第二种情况就是
1 2
2 1 此时2在1前,且2比1大(这不是废话么。。)
那么我们不难就会想到:逆序对
如果不放心,可以自己验证几组数据,意会一下就很明确了
现在就有两个问题:
一.逆序对怎么求
1.暴力,绝对TLE
2.归并排序
3.树状数组
二.怎么维护出现的顺序
楼下的那位大佬是用的类似于邻接表的一个东西,蒟蒻表示看不懂,,,,
我们不难发现这个位置是满足先进先标顺序的
那么开26个队列来维护就好了23333
另外
不开long long见祖宗!!!
多年oi一场空!!!!!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<deque>
#include<queue>
#define LL long long
#define lb(x) ((x)&(-x))
using namespace std;
const int MAXN=;
inline int read()
{
char c=getchar();int x=,f=;
while(c<''||c>'') {if(c=='-') f=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-,c=getchar();return x*f;
}
int n;
queue<int>q[];
int a[MAXN];
int tree[MAXN];
inline void Point_Add(int pos,int val)
{
while(pos<=n)
{
tree[pos]+=val;
pos+=lb(pos);
}
}
inline int Interval_Sum(int pos)
{
int ans=;
while(pos)
{
ans+=tree[pos];
pos-=lb(pos);
}
return ans;
}
int main()
{
n=read();
for(int i=;i<=n;i++)
{
char c=getchar();
q[c-'A'].push(i);
}
char c=getchar();// 可恶的换行符
for(int i=;i<=n;i++)
{
char c=getchar();
a[i]=q[c-'A'].front();
q[c-'A'].pop();
}
LL ans=;
for(int i=;i<=n;i++)
{
Point_Add(a[i],);
ans+=i-Interval_Sum(a[i]);// 树状数组求逆序对,不懂的自行百度
}
printf("%lld",ans);
return ;
}
洛谷P3531 [POI2012]LIT-Letters的更多相关文章
- 洛谷 P3539 [POI2012]ROZ-Fibonacci Representation 解题报告
P3539 [POI2012]ROZ-Fibonacci Representation 题意:给一个数,问最少可以用几个斐波那契数加加减减凑出来 多组数据10 数据范围1e17 第一次瞬间yy出做法, ...
- 洛谷P3538 [POI2012]OKR-A Horrible Poem [字符串hash]
题目传送门 A Horrible Poem 题目描述 Bytie boy has to learn a fragment of a certain poem by heart. The poem, f ...
- 洛谷P3533 [POI2012]RAN-Rendezvous
P3533 [POI2012]RAN-Rendezvous 题目描述 Byteasar is a ranger who works in the Arrow Cave - a famous rende ...
- BZOJ2801/洛谷P3544 [POI2012]BEZ-Minimalist Security(题目性质发掘+图的遍历+解不等式组)
题面戳这 化下题面给的式子: \(z_u+z_v=p_u+p_v-b_{u,v}\) 发现\(p_u+p_v-b_{u,v}\)是确定的,所以只要确定了一个点\(i\)的权值\(x_i\),和它在同一 ...
- 洛谷P3539 [POI2012] ROZ-Fibonacci Representation
题目传送门 转载自:five20,转载请注明出处 本来看到这题,蒟蒻是真心没有把握的,还是five20大佬巨orz 首先由于斐波拉契数的前两项是1,1 ,所以易得对于任何整数必能写成多个斐波拉契数加减 ...
- 洛谷P3537 [POI2012]SZA-Cloakroom(背包)
传送门 蠢了……还以为背包只能用来维护方案数呢……没想到背包这么神奇…… 我们用$dp[i]$表示当$c$的和为$i$时,所有的方案中使得最小的$b$最大时最小的$b$是多少 然后把所有的点按照$a$ ...
- 洛谷P3534 [POI2012] STU
题目 二分好题 首先用二分找最小的绝对值差,对于每个a[i]都两个方向扫一遍,先都改成差满足的形式,然后再找a[k]等于0的情况,发现如果a[k]要变成0,则从他到左右两个方向上必会有两个连续的区间也 ...
- 【洛谷3546_BZOJ2803】[POI2012]PRE-Prefixuffix(String Hash)
Problem: 洛谷3546 Analysis: I gave up and saw other's solution when I had nearly thought of the method ...
- 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]
题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...
随机推荐
- CUDA笔记12
这几天配置了新环境,而且流量不够了就没写. 看到CSDN一个人写了些机器学习的笔记,于是引用一下http://blog.csdn.net/yc461515457/article/details/504 ...
- Objective-C 小记(10)__weak
本文使用的 runtime 版本为 objc4-706. __weak 修饰的指针最重要的特性是其指向的对象销毁后,会自动置为 nil,这个特性的实现完全是依靠运行时的.实现思路是非常简单的,对于下面 ...
- [USACO08DEC]拍头Patting Heads 水题
类似素数筛,暴力可过,不需要太多的优化 Code: #include<cstdio> #include<algorithm> #include<string> us ...
- 京东在2018年成为Intel全球最大PC零售渠道
京东宣布,根据Intel公布的数据,京东在2018年成为Intel全球最大的PC零售渠道. 近日,京东.Intel高层进行了战略会晤,在总结回顾2018年合作成果的同时,就2019年进一步深度战略合作 ...
- caioj 1087 动态规划入门(非常规DP11:潜水员)(二维背包)
这道题的难点在于价值可以多. 这道题我一开始用的是前面的状态推现在的状态 实现比较麻烦,因为价值可以多,所以就设最大价值 为题目给的最大价值乘以10 #include<cstdio> #i ...
- 用例子看ASP.NET Core Identity是什么?
原文:用例子看ASP.NET Core Identity是什么? 目录 前言 基于声明的认证(Claims-based Authentication) Claim 在ASP.NET Core Iden ...
- OpenJDK源码研究笔记(七)–Java字节码文件(.class)的结构
最近在看OpenJDK源码的过程中,顺便看了Java编译器(javac)的源码. 为了理解javac的源码,需要先搞懂Java字节码文件(.class)的结构. 于是,我就认真看了下OpenJDK中J ...
- 从串口设置、读取、并分析um220模块的数据
转载请注明:http://blog.csdn.net/wang_zheng_kai 导航制导与控制实验室 2014年11月10日 好久没有写博客了,先从一个小小的程序開始一段新的历程吧. 近期的项目主 ...
- 软件project总结
软件project的文档完结了.在这里面学到了好多东西.也通过它分析了对机房收费系统进行了更加具体的分析.尽管早就明确了之间的联系,可是越温习越体会到逻辑的重要性和全心全意为人民服务的精神. 这些文档 ...
- Java中二进制字节与十六进制互转
在Java中字节与十六进制的相互转换主要思想有两点: 1.二进制字节转十六进制时,将字节高位与0xF0做"&"操作,然后再左移4位,得到字节高位的十六进制A;将字节低位与0 ...