当前位置:网站首页>ABC 261 F - Sorting Color Balls (reverse pair)
ABC 261 F - Sorting Color Balls (reverse pair)
2022-07-31 00:33:00 【Harris-H】
ABC 261 F - Sorting Color Balls(逆序对)
Regardless of color,The minimum number of times that the adjacent swaps complete the sorting is the inverse logarithm.
The proof is that the number of times the largest number is moved to the end is the reverse logarithm,Then the next largest value、And so on until the minimum value.
Consider color,Therefore, we can subtract the inverse numbers of the same color.
那么答案就是: 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
We start with each colorvector存,Then run the reverse order pair.
Finally, run the whole side in reverse order.
reverse order 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;
}
边栏推荐
- MySQL笔记下
- MySQL master-slave replication and read-write separation script - pro test available
- 【深入浅出玩转FPGA学习14----------测试用例设计2】
- 会议OA项目待开会议、所有会议功能
- C语言力扣第48题之旋转图像。辅助数组
- In-depth understanding of the auto-increment operator from two error-prone written test questions
- What are the efficient open source artifacts of VSCode
- 过滤器(Filter)
- Niuke.com question brushing training (4)
- 【Yugong Series】July 2022 Go Teaching Course 019-For Circular Structure
猜你喜欢
[C language course design] C language campus card management system
The difference between h264 and h265 decoding
Detailed explanation of 9 common reasons for MySQL index failure
从两个易错的笔试题深入理解自增运算符
46.
xss靶机训练【实现弹窗即成功】
论文理解:“Designing and training of a dual CNN for image denoising“
[In-depth and easy-to-follow FPGA learning 14----------Test case design 2]
How to import game archives in joiplay emulator
【多线程】
随机推荐
Gabor filter study notes
SWM32系列教程6-Systick和PWM
mysql主从复制及读写分离脚本-亲测可用
Bypass of xss
How to solve types joiplay simulator does not support this game
限制字符绕过
The difference between substring and substr in MySQL
【愚公系列】2022年07月 Go教学课程 015-运算符之赋值运算符和关系运算符
【Yugong Series】July 2022 Go Teaching Course 017-IF of Branch Structure
45. [Application of list linked list]
xss绕过:prompt(1)
【c语言课程设计】C语言校园卡管理系统
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
Gabor滤波器学习笔记
ABC 261 F - Sorting Color Balls(逆序对)
Xss target drone training [success when pop-up window is realized]
How to adjust Chinese in joiplay simulator
Add text watermark to PHP image
MySQL中substring与substr区别
MySQL notes under