博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1394 Minimum Inversion Number
阅读量:4507 次
发布时间:2019-06-08

本文共 2481 字,大约阅读时间需要 8 分钟。

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21957    Accepted Submission(s): 13098

Problem Description
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.
For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:
a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.
 

 

Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
 

 

Output
For each case, output the minimum inversion number on a single line.
 

 

Sample Input
10 1 3 6 9 0 8 5 7 4 2
 

 

Sample Output
16
 
思路:线段树区间最值,单点增减,求最小逆序数,先求出一个,然后可以发现公式,进而推出其他的解
 
实现代码:
#include
#include
#include
using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int M = 5555;int sum[M<<2];int n;void pushup(int rt){ sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void build(int l,int r,int rt){ sum[rt] = 0; if(l==r) return ; int m = (l+r) >> 1; build(lson); build(rson);}void update(int p,int l,int r,int rt){ if(l == r){ sum[rt] ++; return ; } int m = (l + r) >> 1; if(p <= m) update(p,lson); else update(p,rson); pushup(rt);}int query(int L,int R,int l,int r,int rt){ if(L <= l && r <= R){ return sum[rt]; } int m = (l + r) >> 1; int ret = 0; if(L <= m) ret += query(L,R,lson); if(R>m) ret += query(L,R,rson); return ret;}int a[M];int main(){ int sum1; while(scanf("%d",&n)!=EOF){ build(0,n-1,1); int sum1 = 0; for(int i = 0;i < n;i++){ scanf("%d",&a[i]); sum1 += query(a[i]+1,n-1,0,n-1,1); update(a[i]+1,0,n-1,1); } int ans = sum1; for(int i = 0;i < n; i++){ sum1 = sum1 - a[i] + n - a[i] - 1; ans = min(ans,sum1); } printf("%d\n",ans); } return 0;}

 

 
 

转载于:https://www.cnblogs.com/kls123/p/7840591.html

你可能感兴趣的文章
正则表达式之 数据验证 与 文本替换
查看>>
linux 安装mysql数据库——yum安装法
查看>>
Visual Studio 2008 不能更改安装目录的原因
查看>>
关于求最大公约数
查看>>
为Linux配置常用源:epel和IUS
查看>>
天府地
查看>>
C#高级编程
查看>>
JS实现从照片中裁切自已的肖像
查看>>
使用 https://git.io 缩短 a GitHub.com URL.
查看>>
拷贝、浅拷贝、深拷贝解答
查看>>
四元数
查看>>
StackAndQueue(栈与队列)
查看>>
URLOS安装、升级、卸载
查看>>
在win7下配置sql2005允许远程访问
查看>>
aspose.cell 设置excel里面的文字是超链接
查看>>
POJ 1067 取石子游戏
查看>>
django开发框架-view & template
查看>>
[Linux]systemd和sysV
查看>>
时间日期正则表达
查看>>
JSON.NET 简单的使用
查看>>