当前位置:网站首页>【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;
}
边栏推荐
猜你喜欢

技术盛宴!华云数据携六大议题亮相OpenInfra Days China

巴比特 | 元宇宙每日必读:国内首个数字人产业专项支持政策发布,2025年北京数字人产业规模将破500亿元...

【面试高频题】可逐步优化的链表高频题

Mysql/stonedb - slow SQL - 2022-08-09 Q16 analysis
![[Interface Test] Decoding the request body string of the requests library](/img/99/82ef792dacd398a8a62dd94f235a91.png)
[Interface Test] Decoding the request body string of the requests library

生成NC文件时,报错“未定义机床”

伦敦银行情中短线的支撑和阻力位

杭电多校-Counting Stickmen-(思维+组合数+容斥)

iNFTnews | 迪士尼如何布局Web3

高手这样看现货白银走势图
随机推荐
多线程是同时执行多个线程的吗
CAD 绘制圆角处理
【JZOF】77按之字形打印二叉树
打包报错 AAPT: error: failed to read PNG signature: file does not start with PNG signature.
2021年国内外五大BI厂商——优秀的商业智能工具推荐
【面试高频题】可逐步优化的链表高频题
kubesphere
五分钟商学院(基础---商业篇)
带着昇腾去旅行:一日看尽金陵城里的AI胜景
金仓数据库 KingbaseGIS 使用手册(6.6. 几何对象校验函数、6.7. 空间参考系函数)
32 JZOF 】 【 print down on binary tree
联盟链技术应用的难点
k8s部署mysql
SRv6性能测量
正则表达式的实际使用
70. 爬楼梯进阶版
Mysql/stonedb - slow SQL - 2022-08-09 Q16 analysis
一体化伺服电机在三轴钻孔机中的应用
matplotlib散点图颜色分组图例
Gartner全球集成系统市场数据追踪,超融合市场增速第一