Bzoj 2789: [poi2012]letters tree-like array, reverse

Source: Internet
Author: User

2789: [poi2012]letters time limit:20 Sec Memory limit:128 MB
submit:278 solved:185
[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.

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



BCA, ABC, BAC

Source

Acknowledgement Oimaster

Exercises

Tree Array + reverse order

Hu: Bzoj seems to have hung Qaq, good sad 555555

Okay, get to the point.

First of all, the minimum number of times to exchange adjacent elements and the like is definitely used in a tree-like array for reverse. Then consider greed.

The greedy idea of this problem is very good to think, that is, the B-series each number of its in a series of the recent number to move over.

For example:

A series: Abaca

B Series: ABCAA

For each number in the B sequence, we go to the position where the a sequence should appear, and if the letter is the same, look backwards until we find the first position that has not been selected in the a sequence.

The above example can be obtained by a sequence: 1 2 4 3 5

And then reverse the mess ...

Specific to see the program, your own hand push to understand ^w^

PS: Setting int variable to char type is also cool ... Qaq

1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <cmath>5#include <iostream>6#include <algorithm>7 using namespacestd;8 #defineLL Long Long9 #defineMAXN 1000010Ten structnode One { A     intv,w; - }A[MAXN],B[MAXN]; - /*struct NODE the { - int v,w; - }B[MAXN];*/ - intN; + intBIT[MAXN],C[MAXN]; - CharA[MAXN],B[MAXN]; + BOOLCMP (node Aa,node BB) A { at     if(AA.V==BB.V)returnaa.w<BB.W; -     returnaa.v<BB.V; - } - /*bool Cmp1 (NODE aa,node bb) - { - if (AA.V==BB.V) return aa.w<bb.w; in return aa.v<bb.v; - }*/ to intLowbit (intO) {returno& (-o);} + voidUpdate (intOintO1) - { the      while(o<=N) *     { $bit[o]+=O1;Panax Notoginsengo+=lowbit (o); -     } the } + intSum (into) A { the     intsum=0; +      while(o>0) -     { $sum+=Bit[o]; $o-=lowbit (o); -     } -     returnsum; the } - intMain ()Wuyi { the     inti; -LL ans=0; Wuscanf"%d",&n); -scanf"\n%s\n%s", A +1, B +1); About      for(i=1; i<=n;i++) $     { -a[i].v=a[i]-'A'+1; b[i].v=b[i]-'A'+1; -a[i].w=b[i].w=i; -     } ASort (A +1, a+n+1, CMP); +Sort (b +1, b+n+1, CMP); the      for(i=1; i<=n;i++) c[a[i].w]=B[I].W; -memset (BIT,0,sizeof(BIT)); ans=0; $      for(i=n;i>=1; i--) the     { theAns+=sum (c[i]-1); theUpdate (C[i],1); the     } -printf"%lld", ans); in fclose (stdin); the fclose (stdout); the     return 0; About}

Bzoj 2789: [poi2012]letters tree-like array, reverse

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.