• 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;
    }
  • 相关阅读:
    第一次结对编程作业
    第一次个人编程作业
    获取file中字段,写入到TXT文件中
    通过file中的字段查询MySQL内容
    MySQL常用语句
    MySQL乱码问题
    脚本数据编码格式转换
    mysql 常用命令操作
    thinkphp项目 Class 'finfo' not found
    POJ3255--次短路
  • 原文地址:https://www.cnblogs.com/hkttg/p/9396379.html
Copyright © 2020-2023  润新知