当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

智能创意中的尺寸拓展模块

transition transition && animation animation

机器学习1一回归模型(二)

jira是什么

【LeetCode】64. 最小路径和 - Go 语言题解

Steven Giesel recently published a 5-part series documenting his first experience building an application with the Uno Platform.

image里的mode属性

Steven Giesel 最近发布了一个由5部分内容组成的系列,记录了他首次使用 Uno Platform 构建应用程序的经验。

Excel basic study notes

2021GDCPC Guangdong University Student Programming Competition H.History
随机推荐
C# VSCode & Rider引用命名空间快捷键
【LeetCode】55. 跳跃游戏 - Go 语言题解
如何在 AWS 中应用 DevOps 方法?
【VisDrone数据集】YOLOV4训练VisDrone数据集步骤与结果
PyTorch model export to ONNX file example (LeNet-5)
mysql中关于存储过程无法实现迁移复制表中数据问题
VSCode高效开源神器有哪些
【萌新解题】删除链表的倒数第 N 个结点
@requestmapping注解的作用及用法
Kotlin特殊类
joiplay模拟器rtp如何安装
image里的mode属性
写了多年业务代码,我发现了这11个门道,只有内行才知道
ZZULIOJ:1119: sequence order
The first level must project independently
Lambda表达式
Word文件损坏如何修复
【飞控开发基础教程10】疯壳·开源编队无人机-PID 基础原理
Debezium error series 20: task failed to create new topic. Ensure that the task is authorized to create topics
The performance management method OKR is used by all companies