当前位置:网站首页>【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

How to match garbled characters regularly?

【集训DAY5】快速排序【模拟】【数学】

Technology feast!Huayun Data brings six topics to OpenInfra Days China

A summary of 6 common tools for cross-border e-commerce

IT传奇人物菲尔德的转型经验教训及给CIO的建议

为什么刀具数据库无法打开?

微信小程序获取微信用户步数

金仓数据库 KingbaseGIS 使用手册(6.4. 几何对象存取函数)

k8s部署mysql
随机推荐
matplotlib散点图颜色分组图例
2020年度SaaS TOP100企业名单
68.qt quick-qml多级折叠下拉导航菜单 支持动态添加/卸载 支持qml/widget加载等
SRv6性能测量
直播间搭建,按钮左滑出现删除等操作按钮
【诗歌】枕上诗书
集合运算样例
领跑政务云,连续五年中国第一
国内BI厂商一览
如何知道电脑开机记录?
集群的基础形式
Leetcode 530. 二叉搜索树的最小绝对差
多商户商城系统功能拆解25讲-平台端分销申请
Leetcode 98. 验证二叉搜索树
后台管理实现导入导出
Filament-Material 绘制基本图形
ABAP中Collect的用法
Has your phone ever been monitored?
【JZOF】32从上往下打印二叉树
打包报错 AAPT: error: failed to read PNG signature: file does not start with PNG signature.