当前位置:网站首页>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;
}
边栏推荐
- WebServer process explanation (registration module)
- Kotlin特殊类
- 软考总结
- #Dasctf July Enabler WP
- "NIO Cup" 2022 Nioke Summer Multi-School Training Camp 4 DHKLN
- Data cleaning - ingest using es
- HCIP第十五天笔记
- Apache Doris series: detailed steps for installation and deployment
- Axure轮播图
- Apache Doris series: In-depth understanding of real-time analytical database Apache Doris
猜你喜欢

【LeetCode】55. 跳跃游戏 - Go 语言题解

mysql 中手动设置事务提交

在微服务中使用事件溯源的六大原因 - Herath

# # yyds dry goods inventory interview will brush TOP101: to determine whether there is a part of the list

uniapp develops WeChat applet - soft exam brushing applet

Week 19 Progress (Understanding IoT Basics)

45.【list链表的应用】

47. 【Pointers and Arrays】

Unity 加载读取PPT

Summary of the stock problem of state machine dynamic programming
随机推荐
【LeetCode】70. 爬楼梯 - Go 语言题解
Reverse linked list - in-place inversion method
uni-ui安装
.NET 跨平台应用开发动手教程 |用 Uno Platform 构建一个 Kanban-style Todo App
动态修改el-tab-pane 的label(整理)
How to open the payment channel interface?
“蔚来杯“2022牛客暑期多校训练营2 H.Take the Elevator
mysql 中手动设置事务提交
How to solve the error of joiplay simulator
VSCode高效开源神器有哪些
vscode上利用screen命令跑代码
ZZULIOJ: 1120: the most value to exchange
MySQL面试题
flutter 做底部的三个按键,有叠加,有填充
天空云变化案例
firewalld
机器学习1一回归模型(二)
Android security optimization - APP reinforcement
46.<list链表的举列>
Soft Exam Study Plan