当前位置:网站首页>ABC 261 F - Sorting Color Balls(逆序对)
ABC 261 F - Sorting Color Balls(逆序对)
2022-07-30 23:55:00 【Harris-H】
ABC 261 F - Sorting Color Balls(逆序对)
若不考虑颜色,求相邻交换完成排序的最小次数就是逆序对数。
证明就是先将最大数移到最后的次数就是逆序对数,然后是次大值、依此类推直到最小值。
若考虑颜色,因此我们对于相同颜色的逆序数减掉即可。
那么答案就是: a n s = t o t r e v e r s e − t o t c o l i ans=tot_{reverse}-tot_{col_i} ans=totreverse−totcoli
我们先对每种颜色用vector存,然后跑逆序对。
最后再整体跑一边逆序对即可。
逆序对用 B I T BIT BIT 即可。
时间复杂度: O ( n l o g n ) O(nlogn) O(nlogn)
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
int n,x[N],c[N],tr[N];
vector<int>V[N];
long long rs;
void Ad(int i,int w){
for(;i;i-=i&-i)tr[i]+=w;
}
int Qr(int i){
int rs=0;
for(;i<=n;i+=i&-i)rs+=tr[i];
return rs;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&c[i]);
V[c[i]].push_back(i);
}
for(int i=1;i<=n;++i)scanf("%d",&x[i]);
for(int i=1;i<=n;++i){
int l=V[i].size();
for(int o=0;o<l;++o){
rs-=Qr(x[V[i][o]]+1);
Ad(x[V[i][o]],1);
}
for(int o=0;o<l;++o)Ad(x[V[i][o]],-1);
}
for(int i=1;i<=n;++i){
rs+=Qr(x[i]+1);
Ad(x[i],1);
}
printf("%lld\n",rs);
return 0;
}
边栏推荐
- 【LeetCode】42. 接雨水 - Go 语言题解
- 借助深度估计的点云场景重建
- 【萌新解题】删除链表的倒数第 N 个结点
- In MySQL, the stored procedure cannot realize the problem of migrating and copying the data in the table
- 一款好用的接口测试工具——Postman
- 雪佛兰开拓者,安全保障温暖你的家庭出行的第一选择
- pytorch的安装注意事项
- "Wei cup" school more than 2022 cattle summer camp 4 Nancy (polocy) pelosi article variance law of Arts
- MPI简谈
- joiplay模拟器如何使用
猜你喜欢
随机推荐
leetcode 406. Queue Reconstruction by Height
leetcode:127. 单词接龙
45.【list链表的应用】
[SAM template question] P3975 [TJOI2015] string theory
Dry goods | 4 tips for MySQL performance optimization
ZZULIOJ: 1120: the most value to exchange
The first level must project independently
How to use joiplay emulator
PyTorch model export to ONNX file example (LeNet-5)
2021GDCPC广东省大学生程序设计竞赛 B.Byfibonacci
第一个独立完成的千万级项目
Shell编程条件语句 test命令 整数值,字符串比较 逻辑测试 文件测试
Apache Doris series: In-depth understanding of real-time analytical database Apache Doris
uni-ui安装
transition过渡&&animation动画
Word文件损坏如何修复
Installation considerations for pytorch
mysql中关于存储过程无法实现迁移复制表中数据问题
mysql 中手动设置事务提交
@requestmapping注解的作用及用法