当前位置:网站首页>【SSL集训DAY2】Sort【树状数组】
【SSL集训DAY2】Sort【树状数组】
2022-08-09 22:35:00 【VL——MOESR】

思路:
模拟一下就会发现,搞完一次后最多的长度就只有2了,于是变成了求逆序对
c o d e code code
#include<iostream>
#include<cstdio>
#define lowbit(x) x & -x
using namespace std;
const int MAXN = 1e6 + 10;
int n;
int a[MAXN];
long long c[MAXN];
long long ans;
long long query_(int x) {
long long res = 0;
for(; x; x -= lowbit(x)) res += c[x];
return res;
}
void add(int x) {
for(; x <= n; x += lowbit(x)) c[x] ++;
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);
int i = 1;
while(i <= n) {
int j = i + 1;
while(a[j] < a[j - 1] && j <= n) j ++;
j --;
if(i != j) ans ++;
for(int k = i, g = j; k < g; k ++, g --) swap(a[k], a[g]);
i = j + 1;
}
for(int i = 1; i <= n; i ++) {
ans += query_(n) - query_(a[i]);
add(a[i]);
}
printf("%lld", ans);
return 0;
}
边栏推荐
猜你喜欢
随机推荐
[JZOF] 82 binary tree with a path of a certain value (1)
68.qt quick-qml多级折叠下拉导航菜单 支持动态添加/卸载 支持qml/widget加载等
技术盛宴!华云数据携六大议题亮相OpenInfra Days China
离散选择模型之Gumbel分布
金仓数据库 KingbaseGIS 使用手册(6.4. 几何对象存取函数)
【诗歌】爱你就像爱生命
【JZOF】32从上往下打印二叉树
70. 爬楼梯进阶版
Mysql集群 ShardingSphere
【励志】名言警句
带着昇腾去旅行:一日看尽金陵城里的AI胜景
十位时间戳转化成时间
LiveData : Transformations.map and Transformations.switchMap usage
matplotlib散点图颜色分组图例
直播平台怎么搭建,原生js实现编辑器撤消/恢复功能
直播预告 | ICML 2022 11位一作学者在线分享神经网络,图学习等前沿研究
如何正则匹配乱码?
【云原生】一文讲透Kubevela addon如何添加腾讯Crane
后台管理实现导入导出
国内十大活跃报表 BI 产品深度对比及点评








