当前位置:网站首页>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;
}
边栏推荐
- DFS question list and template summary
- 牛逼的公司都在用的绩效管理法OKR
- leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
- Chevrolet Trailblazer, the first choice for safety and warmth for your family travel
- A Brief Talk About MPI
- How to open the payment channel interface?
- uniapp folding box secondary loop
- “蔚来杯“2022牛客暑期多校训练营2 H.Take the Elevator
- transition过渡&&animation动画
- 46.
猜你喜欢

HCIP Day 15 Notes

How to use joiplay emulator

image里的mode属性

PyTorch model export to ONNX file example (LeNet-5)

2D Transform Module && Media Queries

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

Computer shortcut icon whitening solution

.NET 跨平台应用开发动手教程 |用 Uno Platform 构建一个 Kanban-style Todo App
![45. [Application of list linked list]](/img/7a/ca026cafeceffd2daee68fe66e1882.png)
45. [Application of list linked list]

Shell编程条件语句 test命令 整数值,字符串比较 逻辑测试 文件测试
随机推荐
Kotlin特殊类
"Wei cup" school more than 2022 cattle summer camp 4 Nancy (polocy) pelosi article variance law of Arts
乌克兰外交部:乌已完成恢复粮食安全出口的必要准备
matplotlib图表多曲线多纵轴绘制工具方法
C# VSCode & Rider引用命名空间快捷键
Week 19 Progress (Understanding IoT Basics)
uniapp开发微信小程序-软考刷题小程序
Summary of BFS questions
“蔚来杯“2022牛客暑期多校训练营4 N.Particle Arts 规律 方差
[动态规划] 0-1背包问题和完全背包问题
[0x800706D9] solution appears in Microsoft Store
Machine Learning 1-Regression Model (2)
2022 China Logistics Industry Conference and Entrepreneur Summit Forum will be held in Hangzhou!
el-upload添加请求头
HCIP第十六天笔记
joiplay模拟器rtp如何安装
uni-ui installation
【VisDrone数据集】YOLOV4训练VisDrone数据集步骤与结果
封装、获取系统用户信息、角色及权限控制
$\text{ARC 145}$