HDU-1394 Minimum Inversion Number(线段树求逆序数)
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15675 Accepted Submission(s): 9569
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
这道题目有两个地方可以留意一下。
首先求一个数列的逆序数
首先将这个数列排序,然后从最后一个数,边询问边更新
第二,根据题意,将第一个数放到最后一个,形成的新数列应该怎么求逆序数。其实可以递推,和没有变化前的数列比较逆序数增加了(n-1-a[i])-a[i]
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define MAX 5000
int segTree[MAX*4+5];
int n;
int a[MAX];
int b[MAX];
int c[MAX];
void PushUp(int node)
{
segTree[node]=segTree[node<<1]+segTree[node<<1|1];
}
void build(int node,int begin,int end)
{
if(begin==end)
{
segTree[node]=0;
return;
}
int m=(begin+end)>>1;
build(node<<1,begin,m);
build(node<<1|1,m+1,end);
PushUp(node);
}
void Update(int node,int begin,int end,int ind,int num)
{
if(begin==end)
{
segTree[node]+=num;
return;
}
int m=(begin+end)>>1;
if(ind<=m)
Update(node<<1,begin,m,ind,num);
else
Update(node<<1|1,m+1,end,ind,num);
PushUp(node);
}
int Query(int node,int begin,int end,int left,int right)
{
if(left<=begin&&end<=right)
return segTree[node];
int ret;
int m=(begin+end)>>1;
ret=0;
if(left<=m)
ret+=Query(node<<1,begin,m,left,right);
if(right>m)
ret+=Query(node<<1|1,m+1,end,left,right);
return ret;
}
int cmp(int x,int y)
{
return x<y;
}
int main()
{
int sum;
while(scanf("%d",&n)!=EOF)
{
sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
c[i]=a[i];
b[a[i]]=i;
}
build(1,1,n);
sort(a+1,a+n+1,cmp);
for(int i=n;i>=1;i--)
{
sum+=Query(1,1,n,1,b[a[i]]);
Update(1,1,n,b[a[i]],1);
}
int num;
num=sum;
int ans;
ans=sum;
for(int i=1;i<n;i++)
{
num+=-c[i]+(n-1-c[i]);
if(ans>num)
ans=num;
}
printf("%d\n",ans);
}
return 0;
HDU-1394 Minimum Inversion Number(线段树求逆序数)的更多相关文章
- [HDU] 1394 Minimum Inversion Number [线段树求逆序数]
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- hdu - 1394 Minimum Inversion Number(线段树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...
- HDU 1394 Minimum Inversion Number 线段树
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...
- HDU 1394 Minimum Inversion Number(线段树 或 树状数组)
题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...
- HDU_1394_Minimum Inversion Number_线段树求逆序数
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- hdu 13394 Minimum Inversion Number 线段树
题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...
- hdu 1394 Minimum Inversion Number (树状数组求逆序对)
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- HDU 1394 Minimum Inversion Number (树状数组)
题目链接 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the numb ...
- HDU 1394 Minimum Inversion Number(树状数组/归并排序实现
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- Go 文件操作(创建、打开、读、写)
https://blog.csdn.net/Tovids/article/details/77887946
- [mysql] Incorrect string value: '\xE4\xBC\x9A\xE5\x91\x98' for column 'name' at row 1
数据库字符集错误, 修改为UTF8/utf8mb4字符集即可.
- Oauth2.0(五):Authorization Code 授权
Authorization Code 方式适用于有自己的服务器的应用.之所以叫这个名字,是因为流程中引入了一个叫做 authorization code 的东西.这个东西是一个一次性的临时凭证,用来换 ...
- MS Chart Control 學習手記(二) - 圓餅圖
using System.Web.UI.DataVisualization.Charting; 02 using System.Drawing; 03 04 namespace Chart.AJA ...
- BearSkill纯代码搭建iOS界面
欢迎相同喜欢动效的project师/UI设计师/产品增加我们 iOS动效特攻队–>QQ群:547897182 iOS动效特攻队–>熊熊:648070256 浅谈一下 关于iOS兼容布局一直 ...
- Android学习——在Android中使用OpenCV的第一个程序
刚開始学习Android,因为之前比較熟悉OpenCV,于是就想先在Android上执行OpenCV试试 =============================================== ...
- spring定时任务详解(@Scheduled注解)多线程讲解
(一)在xml里加入task的命名空间 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...
- 【GIS】Cesium1.49编译
1.npm install 2.npm install --save-dev gulp 3.gulp default 4.npm run build 5.npm start 遇到问题 1.gulp不好 ...
- Kafka 0.11版本新功能介绍 —— 空消费组延时rebalance
在0.11之前的版本中,多个consumer实例加入到一个空消费组将导致多次的rebalance,这是由于每个consumer instance启动的时间不可控,很有可能超出coordinator确定 ...
- solr java demo 基础入门
<!--solr的maven依赖--> <dependencies> <dependency> <groupId>org.apache.solr&l ...