当前位置:网站首页>【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;
}
边栏推荐
- 你的手机曾经被监控过吗?
- harbor配置远程仓库
- 2021年国内外五大BI厂商——优秀的商业智能工具推荐
- 【JZOF】77 Print binary tree in zigzag
- 【mysql】查询今天9点
- 【面试高频题】可逐步优化的链表高频题
- 【集训DAY4】矩形【线段树】
- 直播预告 | ICML 2022 11位一作学者在线分享神经网络,图学习等前沿研究
- Gold Warehouse Database KingbaseGIS User Manual (6.2. Management Functions)
- Explore the TiDB Lightning source code to solve the found bugs
猜你喜欢
什么是平面文件数据库? 如何导入多种格式的文件:DSV、JSON、XML?
了解什么是架构基本概念和架构本质
complete knapsack theory
用哈希简单封装unordered_map和unordered_set
一体化伺服电机在三轴钻孔机中的应用
【集训DAY4】询问【Hash】
Mysql/stonedb - slow SQL - 2022-08-09 Q16 analysis
带着昇腾去旅行:一日看尽金陵城里的AI胜景
[Interface Test] Decoding the request body string of the requests library
ABAP中Collect的用法
随机推荐
【诗歌】爱你就像爱生命
ABAP中Collect的用法
首席信息官如何将可持续性和技术结合起来
【JZOF】82二叉树中和为某一值的路径(一)
为什么刀具数据库无法打开?
《GB5084-2021》PDF下载
全面解析FPGA基础知识
【云原生】一文讲透Kubevela addon如何添加腾讯Crane
iNFTnews | 迪士尼如何布局Web3
The latest "Grain Academy Development Tutorial" in 2022: 10 - Front-end payment module
带着昇腾去旅行:一日看尽金陵城里的AI胜景
你的手机曾经被监控过吗?
2022年最新《谷粒学院开发教程》:10 - 前台支付模块
金仓数据库 KingbaseGIS 使用手册(6.6. 几何对象校验函数、6.7. 空间参考系函数)
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
Live Preview | ICML 2022 11 first-author scholars share online neural network, graph learning and other cutting-edge research
What are the Shenzhen fortress machine manufacturers?Which one do you recommend?
linux上使用docker安装redis
【诗歌】枕上诗书
Explore the TiDB Lightning source code to solve the found bugs