hdu 1394 Minimum Inversion Number(线段树求逆序数)
2015-10-08 00:47
525 查看
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15122 Accepted Submission(s): 9235
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
思路就是求逆序数
按给出的顺序 把数插入到线段树中 这个数已经已经插入了 在该叶子节点标1 上面节点中sum的值表示这个区间中已经插入了多少个数
查找逆序数 就是从这个数后面的一个位置开始搜 看已经有多少比这个数大的数插入到线段树中了 即有多少个比这个数大的数在这个数的前面 总和就是逆序数
最小的呢。。就是循环 一次把前面的n-1个数放到最后
记录当前的逆序数就可以了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
struct node
{
int l,r;
int sum;
};
node no[MAXN*4];
int a[MAXN];
void build(int l,int r,int k)
{
no[k].l=l;
no[k].r=r;
no[k].sum=0;
if(l==r)
return;
int mid=(l+r)/2;
build(l,mid,k*2);
build(mid+1,r,k*2+1);
}
void update(int k,int num)
{
// bug;
if(no[k].l==no[k].r&&no[k].l==num)
{
no[k].sum++;
return;
}
int mid=(no[k].l+no[k].r)/2;
if(num<=mid) update(k*2,num);
else update(k*2+1,num);
no[k].sum=no[k*2].sum+no[k*2+1].sum;
}
int sum;
void query(int l,int r,int k)
{
// bug;
if(no[k].l>=l&&no[k].r<=r)
{
sum+=no[k].sum;
return;
}
int mid=(no[k].l+no[k].r)/2;
if(r<=mid) query(l,r,k*2);
else if(l>mid) query(l,r,k*2+1);
else
{
query(l,mid,k*2);
query(mid+1,r,k*2+1);
}
}
int main()
{
// fread;
int n;
while(scanf("%d",&n)!=EOF)
{
build(0,n-1,1);
// bug;
int cnt=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=0;
if(a[i]!=n-1)
query(a[i]+1,n-1,1);
cnt+=sum;
update(1,a[i]);
}
// bug;
int res=cnt;
for(int i=0;i<n;i++)
{
cnt=cnt+(n-1-a[i])-a[i];//因为是从0开始的 把a[i]放到最后 前面有a[i]个比a[i]小的有n-1-a[i]个比a[i]大的
//所以 逆序数较少a[i]个 增加n-1-a[i]个
res=min(res,cnt);
}
printf("%d\n",res);
}
return 0;
}
相关文章推荐
- zookeeper集群模式__me
- 同样加班 不同收获(转)
- HDU 4738-Caocao's Bridges-无向图找桥 DAG Tarjan
- 开源中国源码学习笔记
- 【POJ】:POJ 1575元辅音
- Jump Game II 解答
- UIPickerView常用属性
- 欢迎使用CSDN-markdown编辑器
- git global in c9.io
- MvvmLight for Xamarin.Forms
- UVALive 6139 Interval Product(树状数组)
- Android Studio中如何引用图片资源
- <优化算法>下降方法descent method
- 【转】ubuntu下允许root用户ssh远程登录
- 关情纸尾-----Quartz2D-图片添加水印
- leetcode 8.String to Integer (atoi)
- Python中的对象的排序
- Android UI设计小知识——富文本
- C++ String
- c++ 相关的技术资源整理归类