当前位置:网站首页>2022 Henan Mengxin League game (3): Henan University B - reverse pair count
2022 Henan Mengxin League game (3): Henan University B - reverse pair count
2022-07-26 04:32:00 【WA_ automata】
B - Count in reverse order
solution : First , For a permutation , If the interval is reversed , The number in reverse order is equal to all the plants in the area
Logarithm minus the current reverse logarithm , That is, the original positive sequence pair becomes the reverse sequence pair , Original inverse
Sequence pairs become positive sequence pairs . If for each position, record its inverse to all subsequent positions
This problem can be solved by pairing the numbers in sequence , Consider from [ l , r ] → [ l , r + 1 ] [l, r] → [l, r + 1] [l,r]→[l,r+1] The number of reverse pairs increases
Add interval [ l , r ] [l, r] [l,r] Middle ratio a r + 1 a_r+1 ar+1 The number of , So you can make statistics in advance for each
Location , To each position in front of him, how many numbers are bigger than him , Directly after statistics
Calculate the reverse order pair .
- Time complexity O ( n 2 ) O(n^2) O(n2)
Of course, you can also use a tree array to count reverse pairs , You can pass this question well .
- Time complexity approximation O ( n 2 ) O(n^2) O(n2)
#include<cstdio>
#include<cstring>
const int N = 6010;
int a[N],tr[N],f[N][N],g[N][N];
int n;
inline int lowbit(int x)
{
return x&-x;
}
inline void add(int x,int y)
{
for(int i=x;i<=n;i+=lowbit(i))
tr[i]+=y;
}
inline int query(int x)
{
int res=0;
for(int i=x;i>0;i-=lowbit(i))
res+=tr[i];
return res;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
f[i][j]=f[i][j-1]+query(n)-query(a[j]-1);
g[i][j]=g[i][j-1]+query(a[j]);
add(a[j],1);
}
memset(tr,0,sizeof tr);
}
int q;scanf("%d",&q);
while(q--)
{
int l,r;scanf("%d%d",&l,&r);
printf("%d\n",f[1][n]-f[l][r]+g[l][r]);
}
return 0;
}
边栏推荐
- The difference between positive samples, negative samples, simple samples and difficult samples in deep learning (simple and easy to understand)
- UE4 two ways to obtain player control
- MySQL日志分类:错误日志、二进制日志、查询日志、慢查询日志
- Throttling anti shake function of JS handwritten function
- Rotate array minimum number
- Unable to find sygwin.s file during vscode debugging
- How to make your language academic when writing a thesis? Just remember four sentences!
- 青少年创客教育的创意设计原理
- 自动化测试框架该如何搭建?
- 再获认可 | 赛宁网安连续上榜《CCSIP 2022中国网络安全产业全景图》
猜你喜欢
随机推荐
Tutorial on using the one click upgrade function of the rtsp/onvif protocol video platform easynvr service
Life related -- the heartfelt words of a graduate tutor of Huake (mainly applicable to science and Engineering)
Throttling anti shake function of JS handwritten function
计算离散点的曲率(matlab)
这种是我的vs没连上数据库吗
2022河南萌新联赛第(三)场:河南大学 A - 玉米大炮
旋转数组最小数字
Makefile knowledge rearrangement (super detailed)
makefile知识再整理(超详细)
Authentication Encyclopedia (cookies, sessions, tokens, JWT, single sign on), in-depth understanding and understanding of authentication
Yapi installation
机器学习之信用卡欺诈检测
UE4 获取玩家控制权的两种方式
UE4 switching of control rights of multiple roles
10、 Interceptor
ASP. Net core actionfilter filter details
Build a maker Education Laboratory for teenagers
力扣每日一题-第42天-661. 图片平滑器
How to write the abbreviation of the thesis?
[C language foundation] 13 preprocessor









