当前位置:网站首页>[Luogu] p1908 reverse sequence pair
[Luogu] p1908 reverse sequence pair
2022-07-24 15:34:00 【Record algorithm solution】
Title address :
https://www.luogu.com.cn/problem/P1908
Title Description :
Cat and cat TOM And mice JERRY Recently, it's a contest again , But it's all adults, after all , They don't like to play chasing games anymore , Now they like to play Statistics . lately ,TOM Old cat looked up a human class called “ Reverse alignment ” Things that are , It's defined like this : For a given sequence of positive integers , A reverse order pair is in a sequence a i > a j a_i>a_j ai>aj And i < j i<j i<j That's right . Knowing the concept , They compete who first counts the number of reverse pairs in a given sequence of positive integers . Note that there may be duplicate numbers in the sequence .
Input format :
first line , a number n n n, It means that there is n n n Number .
The second line n n n Number , Represents a given sequence . Each number in the sequence does not exceed 1 0 9 10^9 109.
Output format :
The number of reverse pairs in the output sequence .
Data range :
about 25 % 25\% 25% The data of , n ≤ 2500 n \leq 2500 n≤2500
about 50 % 50\% 50% The data of , n ≤ 4 × 1 0 4 n \leq 4 \times 10^4 n≤4×104.
For all the data , n ≤ 5 × 1 0 5 n \leq 5 \times 10^5 n≤5×105
You can use tree arrays directly . Traverse from back to front , Encountered a number x x x Go to the tree array to find the front of the array it maintains x − 1 x-1 x−1 The number and ( That is, how many numbers are smaller than the current number ), After that, I will x x x Add 1 1 1. Count the answers while traversing . The code is as follows :
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) (x & -x)
using namespace std;
const int N = 5e5 + 10;
int n, m, a[N], b[N];
int tr[N];
unordered_map<int, int> mp;
void add(int k, int x) {
for (; k <= n; k += lowbit(k)) tr[k] += x;
}
int sum(int k) {
int res = 0;
for (; k; k -= lowbit(k)) res += tr[k];
return res;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
// Do orderly discretization
sort(b, b + n);
m = unique(b, b + n) - b;
for (int i = 0; i < m; i++) mp[b[i]] = i + 1;
long res = 0;
for (int i = n - 1; i >= 0; i--) {
int x = mp[a[i]];
res += sum(x - 1);
add(x, 1);
}
printf("%ld\n", res);
}
Time complexity O ( n log n ) O(n\log n) O(nlogn), Space O ( n ) O(n) O(n).
边栏推荐
猜你喜欢

Nine key measures to maintain server security in Hong Kong

kubernetes多网卡方案之Multus_CNI部署和基本使用

你不能只会flex居中布局,精制动画讲解所有flex布局方式!通俗易懂纯干货教程!...

基于Lambert函数的时滞系统稳定性研究

2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第二题 智能服药助手 (已完结)

什么是防火墙?防火墙能发挥什么样的作用?

Istio1.12: installation and quick start

Kubectl_ Easy to use command line tool: Oh my Zsh_ Tips and tricks

【OpenCV 例程300篇】238. OpenCV 中的 Harris 角点检测

Route planning method for UAV in unknown environment based on improved SAS algorithm
随机推荐
zabbix管理员忘记登录密码
Multus of kubernetes multi network card scheme_ CNI deployment and basic use
华为无线设备配置WPA2-802.1X-AES安全策略
4279. Cartesian tree
Istio1.12: installation and quick start
华为相机能力
Cloud development standalone image Jiugongge traffic main source code
26. Code implementation of file using disk
Huawei wireless device configuration wpa2-802.1x-aes security policy
Outlook tutorial, how to set rules in outlook?
matlab图像去雾技术GUI界面-全局平衡直方图
How to deal with being attacked? Advanced anti DDoS IP protection strategy
What is a firewall? What role can firewalls play?
24.原生磁盘的使用
Kubectl_ Easy to use command line tool: Oh my Zsh_ Tips and tricks
[tkinter beautification] window out of system style (common to three systems)
2022 robocom world robot developer competition - undergraduate group (provincial competition) -- question 2: intelligent medication assistant (finished)
Database learning – select (multi table joint query) [easy to understand]
Read the paper with me - multi model text recognition network
报错【项目报错】