zoukankan      html  css  js  c++  java
  • BZOJ2789: [Poi2012]Letters

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2789

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

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

    n<=100W

    题解:一股浓浓的火柴排队既视感。。。

               原来一直不会做。。。今天看到了hzwer的题解

               贪心,每个字母一定选择离它最近的相同字母换过来,然后就成了求逆序对了

               orz  重编号一下就好,这一步可以用26个队列

    代码:

     1 #include<cstdio>
     2 
     3 #include<cstdlib>
     4 
     5 #include<cmath>
     6 
     7 #include<cstring>
     8 
     9 #include<algorithm>
    10 
    11 #include<iostream>
    12 
    13 #include<vector>
    14 
    15 #include<map>
    16 
    17 #include<set>
    18 
    19 #include<queue>
    20 
    21 #include<string>
    22 
    23 #define inf 1000000000
    24 
    25 #define maxn 1000000+5
    26 
    27 #define maxm 500+100
    28 
    29 #define eps 1e-10
    30 
    31 #define ll long long
    32 
    33 #define pa pair<int,int>
    34 
    35 #define for0(i,n) for(int i=0;i<=(n);i++)
    36 
    37 #define for1(i,n) for(int i=1;i<=(n);i++)
    38 
    39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
    40 
    41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
    42 
    43 #define mod 1000000007
    44 
    45 using namespace std;
    46 
    47 inline int read()
    48 
    49 {
    50 
    51     int x=0,f=1;char ch=getchar();
    52 
    53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    54 
    55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    56 
    57     return x*f;
    58 
    59 }
    60 int n,s[maxn];
    61 ll ans;
    62 char ss[maxn];
    63 queue<int>q[50];
    64 inline ll sum(int x)
    65 {
    66     int t=0;
    67     for(;x;x-=x&(-x))t+=s[x];
    68     return t;
    69 }
    70 inline void add(int x)
    71 {
    72     for(;x<=n;x+=x&(-x))s[x]++;
    73 }
    74 
    75 int main()
    76 
    77 {
    78 
    79     freopen("input.txt","r",stdin);
    80 
    81     freopen("output.txt","w",stdout);
    82 
    83     n=read();scanf("%s",ss+1);
    84     for1(i,n)q[ss[i]-'A'].push(i);
    85     scanf("%s",ss+1);
    86     for1(i,n)
    87     {
    88         int x=q[ss[i]-'A'].front();q[ss[i]-'A'].pop();
    89         add(n+1-x);
    90         ans+=sum(n+1-x-1);
    91     }
    92     cout<<ans<<endl;
    93 
    94     return 0;
    95 
    96 }
    View Code

     

             


  • 相关阅读:
    WordPress在nginx服务器伪静态
    excel根据一列的值匹配另一列
    linux上安装Anaconda并创建python虚拟环境
    python模块学习之numpy
    大数据测试工具
    python学习路线
    spark event log
    夏令时
    spark学习指南
    Yarn和Spark对比
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4088883.html
Copyright © 2011-2022 走看看