当前位置:网站首页>【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;
}
边栏推荐
猜你喜欢
生成NC文件时,报错“未定义机床”
【面试高频题】可逐步优化的链表高频题
集群的基础形式
Gartner's global integrated system market data tracking, hyperconverged market growth rate is the first
2022年最新《谷粒学院开发教程》:10 - 前台支付模块
matplotlib散点图自定义坐标轴(文字坐标轴)
伦敦银行情中短线的支撑和阻力位
61.【快速排序法详解】
杭电多校-Counting Stickmen-(思维+组合数+容斥)
A summary of 6 common tools for cross-border e-commerce
随机推荐
[Interface Test] Decoding the request body string of the requests library
【诗歌】爱你就像爱生命
力扣:377. 组合总和 Ⅳ
CMake使用记录
matplotlib散点图自定义坐标轴(文字坐标轴)
【JZOF】77 Print binary tree in zigzag
Comprehensive analysis of FPGA basics
【实用工具系列】MathCAD入门安装及快速上手使用教程
关于服务治理
2022-08-09 mysql/stonedb-慢SQL-Q16分析
ElasticSearcch集群
33. Fabric通道、组织、节点、权限间关系
【JZOF】82二叉树中和为某一值的路径(一)
The latest "Grain Academy Development Tutorial" in 2022: 10 - Front-end payment module
巴比特 | 元宇宙每日必读:国内首个数字人产业专项支持政策发布,2025年北京数字人产业规模将破500亿元...
微信小程序获取微信用户步数
2022-08-09 mysql/stonedb-subquery performance improvement-introduction
完全背包理论
【面试高频题】可逐步优化的链表高频题
你的手机曾经被监控过吗?