当前位置:网站首页>模板_求排列逆序对_基于归并排序
模板_求排列逆序对_基于归并排序
2022-07-06 02:06:00 【这题AC再睡.】
//
#include<bits/stdc++.h>
using namespace std;
#define int long long // 大数据量
const int N=1e5+6;
int in[N];
int ans;
void merge( int x1,int y1,int x2,int y2 )
{
int tt[N];
int i=x1,j=x2,pos=0;
while( i<=y1 && j<=y2 ) // j-( x1+pos ) 位置差
{
if( in[i]<=in[j] ) tt[pos++]=in[i++];
else { ans+=j-( x1+pos ); tt[pos++]=in[j++]; }
}
while( i<=y1 ) tt[pos++]=in[i++];
while( j<=y2 ) tt[pos++]=in[j++];
for( i=0;i<pos;i++ ) in[x1+i]=tt[i];
}
void f( int x,int y )
{
if( x==y ) return ;
int mid=( x+y )>>1;
f( x,mid );
f( mid+1,y );
merge( x,mid,mid+1,y );
}
signed main()
{
int n,i;
while( cin>>n )
{
for( i=0;i<n;i++ ) cin>>in[i];
ans=0;
f( 0,n-1 );
cout<<ans<<endl;
}
return 0;
}
边栏推荐
- 同一个 SqlSession 中执行两条一模一样的SQL语句查询得到的 total 数量不一样
- SQL statement
- 729. 我的日程安排表 I / 剑指 Offer II 106. 二分图
- Open source | Ctrip ticket BDD UI testing framework flybirds
- leetcode-2.回文判断
- General process of machine learning training and parameter optimization (discussion)
- RDD partition rules of spark
- PHP campus financial management system for computer graduation design
- How to set an alias inside a bash shell script so that is it visible from the outside?
- Spark accumulator
猜你喜欢
TrueType字体文件提取关键信息
2022年PMP项目管理考试敏捷知识点(8)
selenium 等待方式
Computer graduation design PHP campus restaurant online ordering system
[width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)
Executing two identical SQL statements in the same sqlsession will result in different total numbers
[robot library] awesome robots Libraries
插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
【机器人库】 awesome-robotics-libraries
leetcode-两数之和
随机推荐
Paper notes: graph neural network gat
Bidding promotion process
Leetcode3, implémenter strstr ()
Text editing VIM operation, file upload
Redis key operation
This time, thoroughly understand the deep copy
RDD creation method of spark
[robot hand eye calibration] eye in hand
数据工程系列精讲(第四讲): Data-centric AI 之样本工程
Unity learning notes -- 2D one-way platform production method
【社区人物志】专访马龙伟:轮子不好用,那就自己造!
Executing two identical SQL statements in the same sqlsession will result in different total numbers
selenium 元素定位(2)
Global and Chinese market of wheelchair climbing machines 2022-2028: Research Report on technology, participants, trends, market size and share
Derivation of Biot Savart law in College Physics
插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
【机器人库】 awesome-robotics-libraries
Selenium element positioning (2)
Exness: Mercedes Benz's profits exceed expectations, and it is predicted that there will be a supply chain shortage in 2022
Card 4G industrial router charging pile intelligent cabinet private network video monitoring 4G to Ethernet to WiFi wired network speed test software and hardware customization