当前位置:网站首页>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;
}
边栏推荐
- Jetpack Compose学习(8)——State及remeber
- what is jira
- xss绕过:prompt(1)
- h264和h265解码上的区别
- (5) fastai application
- Method for deduplication of object collection
- web漏洞之需要准备的工作
- Go study notes (84) - Go project directory structure
- A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES
- [Yugong Series] July 2022 Go Teaching Course 015-Assignment Operators and Relational Operators of Operators
猜你喜欢
随机推荐
对象集合去重的方法
Ukraine's foreign ministry: wu was restored to complete the export of food security
论文理解:“Designing and training of a dual CNN for image denoising“
MPI简谈
web漏洞之需要准备的工作
What is Promise?What is the principle of Promise?How to use Promises?
(五)fastai应用
What are the efficient open source artifacts of VSCode
数据库的严格模式
Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a
How to adjust Chinese in joiplay simulator
软件开发设计流程
MySQL数据库约束,表的设计
【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
How to Repair Word File Corruption
MySQL triggers
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
【愚公系列】2022年07月 Go教学课程 013-常量、指针
IOT cross-platform component design scheme
【深度学习】Transformer模型详解









