当前位置:网站首页>[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).
边栏推荐
- Research on stability of time-delay systems based on Lambert function
- 2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第二题 智能服药助手 (已完结)
- 15. Talk about these common multi-threaded interview questions
- Database learning – select (multi table joint query) [easy to understand]
- Varnish4.0缓存代理配置
- IP protocol - network segment division
- Android SQLite database practice
- C# SQLite Database Locked exception
- 遭受DDoS时,高防IP和高防CDN的选择
- Leetcode 1288. delete the covered interval (yes, solved)
猜你喜欢

MySQL学习笔记(总结)

matlab图像去雾技术GUI界面-全局平衡直方图

JMeter - call the interface for uploading files or pictures

2022 robocom world robot developer competition - undergraduate group (provincial competition) -- question 3 running team robot (finished)

【Bug解决】Win10安装pycocotools报错

2022 robocom world robot developer competition - undergraduate group (provincial competition) -- question 1: don't waste gold (finished)

Personal practical experience: Data Modeling "whether account data belongs to dimension or account domain"

Route planning method for UAV in unknown environment based on improved SAS algorithm

Discussion on the basic use and address of pointer in array object

Leetcode-09 (next rank + happy number + full rank)
随机推荐
Kubectl_好用的命令行工具:oh-my-zsh_技巧和窍门
C# SQLite Database Locked exception
Applet tab
遭受DDoS时,高防IP和高防CDN的选择
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第三题 跑团机器人 (已完结)
Research on stability of time-delay systems based on Lambert function
在LAMP架构中部署Zabbix监控系统及邮件报警机制
Kubectl_ Easy to use command line tool: Oh my Zsh_ Tips and tricks
Date class and time class definitions (operator overload application)
C. Recover an RBS
What is a firewall? What role can firewalls play?
2022 robocom world robot developer competition - undergraduate group (provincial competition) CAIP full version solution
[TA frost wolf \u may - hundred people plan] Figure 3.4 introduction to delayed rendering pipeline
Here comes the problem! Unplug the network cable for a few seconds and plug it back in. Does the original TCP connection still exist?
[USENIX atc'22] an efficient distributed training framework whale that supports the super large-scale model of heterogeneous GPU clusters
Exomeiser annotates and prioritizes exome variants
matlab图像去雾技术GUI界面-全局平衡直方图
【ACWing】909. 下棋游戏
Sklearn.metrics module model evaluation function
MySql函数