题目链接

题目大意

给你一个长度为n的字符串,可以交换相邻两个元素,使得这个字符串翻转,求最少多少种次数改变

题目思路

如果要求数组排序所需要的冒泡次数,那其实就是逆序对

这个也差不多,但是如果是相同字符,用的应该是对应的最近的这个字母。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int n,cnt[30],ans[maxn],tree[maxn<<2];
char s[maxn];
vector<int> pos[30];
int query(int node,int L,int R,int l,int r){
if(L<=l&&r<=R){
return tree[node];
}
int mid=(l+r)>>1,sum=0;
if(mid>=L) sum+=query(node<<1,L,R,l,mid);
if(mid<R) sum+=query(node<<1|1,L,R,mid+1,r);
return sum;
}
void update(int node,int l,int r,int pos){
if(l==r){
tree[node]++;
return ;
}
int mid=(l+r)>>1;
if(mid>=pos) update(node<<1,l,mid,pos);
else update(node<<1|1,mid+1,r,pos);
tree[node]=tree[node<<1]+tree[node<<1|1];
}
int main(){
scanf("%d %s",&n,s+1);
for(int i=1;i<=n;i++){
int x=s[i]-'a'+1;
pos[x].push_back(n-i+1);
}
for(int i=n;i>=1;i--){
int x=s[i]-'a'+1;
ans[i]=pos[x][cnt[x]++];
}
ll pr=0;
for(int i=1;i<=n;i++){
int bug=query(1,1,ans[i],1,n);
pr+=query(1,ans[i],n,1,n);
update(1,1,n,ans[i]);
}
printf("%lld\n",pr);
return 0;
}

Educational Codeforces Round 96 (Rated for Div. 2) E. String Reversal 题解(思维+逆序对)的更多相关文章

  1. Educational Codeforces Round 96 (Rated for Div. 2) E. String Reversal (思维,逆序对)

    题意:给你一个字符串,每次可以调换现字符串的相邻两个字符,问最少操作多少次使得这个字符串等于其反转过来的字符串. 题解:先考虑字符串中没有相同字符的情况,那么我们每次将目前字符串的最后一个字符一直调换 ...

  2. Educational Codeforces Round 96 (Rated for Div. 2) D. String Deletion (思维)

    题意:有一个\(01\)串,每次操作要先删除一个位置上的元素,然后删除相同前缀和,直到字符串被删完,问最多能操作多少次. 题解: 对于一个长度大于\(1\)的相同前缀,我们最多只能对它操作一次,然后就 ...

  3. Educational Codeforces Round 102 (Rated for Div. 2) B. String LCM (构造,思维)

    题意:给你两个字符串\(a\)和\(b\),找出它们的\(lcm\),即构造一个新的字符串\(c\),使得\(c\)可以由\(x\)个\(a\)得到,并且可以由\(y\)个\(b\)得到,输出\(c\ ...

  4. Educational Codeforces Round 96 (Rated for Div. 2)

    A. Number of Apartments 题意:求方程的解 思路:直接模拟就行 代码: #include<iostream> #include<cstdio> #incl ...

  5. Educational Codeforces Round 76 (Rated for Div. 2)E(dp||贪心||题解写法)

    题:https://codeforces.com/contest/1257/problem/E 题意:给定3个数组,可行操作:每个数都可以跳到另外俩个数组中去,实行多步操作后使三个数组拼接起来形成升序 ...

  6. Educational Codeforces Round 92 (Rated for Div. 2) B、C题解

    TAT 第一场codeforces B. Array Walk #暴力 #贪心 题目链接 题意 有\(a1, a2, ..., an\) 个格子(每个格子有各自分数),最初为1号格(初始分数为\(a1 ...

  7. 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解

    [比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B  Run for your prize[贪心] ...

  8. Educational Codeforces Round 65 (Rated for Div. 2) E. Range Deleting(思维+coding)

    传送门 参考资料: [1]:https://blog.csdn.net/weixin_43262291/article/details/90271693 题意: 给你一个包含 n 个数的序列 a,并且 ...

  9. Educational Codeforces Round 73 (Rated for Div. 2)D(DP,思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[300007],b[3 ...

随机推荐

  1. uniapp swiper高度自适应问题

    这里的话是想做一个比较常见的左右滑动更改tab的效果,引用了uview-ui中的u-tabs-swiper组件,需要结合swiper组件来使用 先来讲下主体思路:每个tab页(swiper-item) ...

  2. 使用 beeline 连接 hive 数据库报错处理

    一.beeline连接hive报错 1. User: root is not allowed to impersonate root (state=08S01,code=0) 在初次搭建完hadoop ...

  3. EXCEL计数时间差--分钟数

  4. DES 实现

    原理 加密 置换: IP逆置换: 迭代: PC-1置换: PC-2置换: 子秘钥的生成: 加密函数f: 解密 代码 // C语言实现 #include<stdio.h> #include& ...

  5. Linux和MySQL的安装与基本操作

  6. 教你如何 分析 Android ANR 问题

    ANR介绍 ANR 的全称是 Application No Responding,即应用程序无响应,具体是一些特定的 Message (Key Dispatch.Broadcast.Service) ...

  7. 【Kata Daily 190920】Square(n) Sum(平方加总)

    题目: Complete the square sum function so that it squares each number passed into it and then sums the ...

  8. streamreader

    using (StreamReader sr = new StreamReader(@"C:\Documents and Settings\Administrator\桌面\1.txt&qu ...

  9. Sentinel 的一些小扩展

    随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从流量控制.熔断降级.系统自适应保护等多个维度来帮助您保障微服务的稳定 ...

  10. Docker - 解决 gitlab 容器上的项目进行 clone 时,IP 地址显示一串数字而不是正常 IP 地址的问题

    问题背景 通过 gitlab 容器创建了一个项目,想 clone 到本地,结果发现项目的 IP 地址是一串数字 问题排查 明明创建项目的时候,IP 地址还是正常的鸭! 再看看项目的 settings ...