Bzoj 2789: [Poi2012]letters (BIT)

Source: Internet
Author: User

directly to the reverse order on the line ... Time complexity O (NLOGN)

-------------------------------------------------------------------------

#include <bits/stdc++.h>

using namespace std;#define IDX (c) ((c)-' A ')#define LOWBIT (x) ((x) &-(x))Const INT charset = +;const int MAXN = 1000009; stack<int> Let[charset];int N;A long long ans = 0;Char A[MAXN], B[MAXN];struct BIT {int B[MAXN];BIT () {memset (b, 0, sizeof B);}inline void Add (int p) {For (++p; p <= N; p + = Lowbit (p))b[p]++;}inline int sum (int p) {int ret = 0;For (++p; p; p-= Lowbit (p))ret + = b[p];return ret;}} bit;int main () {cin >> N;scanf ("%s", A); scanf ("%s", B);for (int i = 0; i < N; i++)Let[idx (A[i])].push (i);for (int i = N-1; ~i; i--) {int t = LET[IDX (B[i])].top (); Let[idx (B[i])].pop ();ans + = bit.sum (t);Bit.add (t);}cout << ans << "\ n";return 0;}

-------------------------------------------------------------------------

2789: [Poi2012]letters time limit: Sec Memory Limit: MB
Submit: Solved: 144
[Submit] [Status] [Discuss] Description

Give two strings A and B with the same length and uppercase letters, guaranteeing that each letter in a and b appears the same number of times.

Now each time you can swap two characters in a, the minimum number of times you need to swap can make a into B.


Input


The first line is a positive integer n (2<=n<=1,000,000) that represents the length of the string.

The second and third lines each have a string of length n and contain only uppercase English letters.



Outputa non-negative integer that represents the minimum number of interchanges. Sample Input3
Abc
Bca
Sample Output2HINT



BCA, ABC, BAC

Source

Acknowledgement Oimaster

Bzoj 2789: [Poi2012]letters (BIT)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.