当前位置:网站首页>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;
}
边栏推荐
- Axure Carousel
- joiplay模拟器不支持此游戏类型怎么解决
- Installation considerations for pytorch
- 2021GDCPC广东省大学生程序设计竞赛 H.History
- 实验7(MPLS实验)
- Chevrolet Trailblazer, the first choice for safety and warmth for your family travel
- Introducing the visualization tool Netron
- In MySQL, the stored procedure cannot realize the problem of migrating and copying the data in the table
- uniapp develops WeChat applet - soft exam brushing applet
- An easy-to-use interface testing tools - the Postman
猜你喜欢
uniapp开发微信小程序-软考刷题小程序
Reverse linked list - in-place inversion method
软件开发设计流程
transition过渡&&animation动画
47. 【Pointers and Arrays】
Steven Giesel recently published a 5-part series documenting his first experience building an application with the Uno Platform.
flex-direction容器属性
How to import game archives in joiplay emulator
Introducing the visualization tool Netron
天空云变化案例
随机推荐
Reverse linked list - head insertion inversion method
2022 Nioke Summer Multi-School Training Camp 1 J Serval and Essay
写了多年业务代码,我发现了这11个门道,只有内行才知道
HCIP第十五天笔记
乌克兰外交部:乌已完成恢复粮食安全出口的必要准备
第一节 zadig 入门
PS Basic Learning (1)
宽客必备神器-AKShare
常用的正则表达式
Apache Doris series: detailed steps for installation and deployment
How to solve the error of joiplay simulator
MPI简谈
Shell编程条件语句 test命令 整数值,字符串比较 逻辑测试 文件测试
uniapp develops WeChat applet - soft exam brushing applet
How to import game archives in joiplay emulator
[SAM模板题] P3975 [TJOI2015] 弦论
matplotlib图表多曲线多纵轴绘制工具方法
Axure Carousel
[0x800706D9] solution appears in Microsoft Store
joiplay模拟器不支持此游戏类型怎么解决