zoukankan      html  css  js  c++  java
  • P3531 [POI2012]LIT-Letters

    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.

    输入输出样例

    输入样例#1: 
    3
    ABC
    BCA
    输出样例#1: 
    2

    说明

    给出两个长度相同且由大写英文字母组成的字符串A、B,保证A和B中每种字母出现的次数相同。

    现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B。

    ————————————————————————————————————————————————————————————————

    树状数组求逆序对,思路还是挺喵的

    code

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    typedef long long LL;
    const int MAXN = 1000010;
    LL ans, o[MAXN];
    int n, b[30][MAXN], a[MAXN], c[30];
    
    int lowbit(int x) {return x&(-x);}
    void Add(int x) {for(int i=x;i<=n;i+=lowbit(i))o[i]++;}
    LL query(int x) {LL ans=0;for(int i=x;i;i-=lowbit(i))ans+=o[i];return ans;}
    
    int main() {
        char s1[MAXN], s2[MAXN];
        scanf("%d%s%s", &n, s1, s2);
        for (int i = 0; i < n; ++ i) 
            b[s1[i]-'A'][++b[s1[i]-'A'][0]] = i;
        for (int i = 0; i < n; ++ i)
            a[i+1] = b[s2[i]-'A'][++c[s2[i]-'A']] + 1;
        for (int i = 1; i <= n; ++ i) {
            ans += query(n) - query(a[i] - 1);
            Add(a[i]);
        }
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    H5 + 3D + AR/VR 综述
    10中典型的软件开发模型
    总线宽度VS总线带宽
    最全程序设计流程、技术、工具、交付结果【软件全生命周期】
    一幅图读懂面向对象和面向过程程序设计的区别!
    C语言实现计算器,附源码,超简单!
    request.getRequestDispatcher()的两个方法forward()/include()!!!
    关于使用ResultSet ---结果集没有当前行
    JComboBox添加item的赋值类型问题!不一致的话会导致不能更改jcombobox的选择值
    关于String的两种赋值方式
  • 原文地址:https://www.cnblogs.com/hkttg/p/9396379.html
Copyright © 2011-2022 走看看