Minimum Inversion Number

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

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
 
Author
CHEN, Gaoli
 
Source
 
题目意思:
给一个n,然后一个长度为n的数组,每个元素小于n且不重复,每次操作把首位元素放到末尾,求n次操作中最小的逆序对数目。
 
思路:
先求一下原始数组的逆序对数目,每次把首位放末尾的时候,num(逆序对数目)加上大于a[i]的,然后减去小于a[i]的。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 5005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n;
int b[N];
int a[N]; int lowbit(int x){
return x&(-x);
} void solve(int val,int x){
while(x<=n){
a[x]+=val;
x+=lowbit(x);
}
} int sum(int x){
int ans=;
while(x>){
ans+=a[x];
x-=lowbit(x);
}
return ans;
} main()
{
int t, i, j, k;
while(scanf("%d",&n)==){
memset(a,,sizeof(a));
int ans=;
for(i=;i<=n;i++) {
scanf("%d",&b[i]);
b[i]++;
ans+=sum(n)-sum(b[i]);
solve(,b[i]);
}
int num=ans;
for(i=;i<=n;i++){
num+=n-b[i]-b[i]+;//sum(n)-sum(b[i])-sum(b[i]-1);
ans=min(ans,num);
}
printf("%d\n",ans);
}
}

HDU 1394 树状数组求逆序对的更多相关文章

  1. POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)

    树状数组求逆序对   转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...

  2. [NOIP2013提高&洛谷P1966]火柴排队 题解(树状数组求逆序对)

    [NOIP2013提高&洛谷P1966]火柴排队 Description 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相 ...

  3. [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)

    [NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...

  4. 【bzoj2789】[Poi2012]Letters 树状数组求逆序对

    题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...

  5. “浪潮杯”第九届山东省ACM大学生程序设计竞赛(重现赛)E.sequence(树状数组求逆序对(划掉))

    传送门 E.sequence •题意 定义序列 p 中的 "good",只要 i 之前存在 pj < pi,那么,pi就是 "good": 求删除一个数, ...

  6. 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)

    2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...

  7. NOIP 2013 洛谷P1966 火柴排队 (树状数组求逆序对)

    对于a[],b[]两个数组,我们应选取其中一个为基准,再运用树状数组求逆序对的方法就行了. 大佬博客:https://www.cnblogs.com/luckyblock/p/11482130.htm ...

  8. poj3067 Japan 树状数组求逆序对

    题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...

  9. 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)

    链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

随机推荐

  1. Delphi 过程与函数

    注:该内容整理自以下链接. http://chanlei001.blog.163.com/blog/static/340306642011111615445266/ delphi 过程以保留字proc ...

  2. android实现系统电话通话过程中自动感应黑屏

    package com.developmenttools.customui.activity; import java.util.HashSet;import java.util.Set; impor ...

  3. BestCoder Valentine's Day Round

    昨晚在开赛前5分钟注册的,然后比赛刚开始就掉线我就不想说了(蹭网的下场……),只好用手机来看题和提交,代码用电脑打好再拉进手机的(是在傻傻地用手机打了一半后才想到的办法). 1001,也就是 hdu ...

  4. JavaScript屏蔽Backspace键

    原文:http://www.cnblogs.com/xdp-gacl/p/3785806.html 今天在IE浏览器下发现,当把使用readonly="readonly"属性将文本 ...

  5. 【服务器环境搭建-Centos】Nginx1.9.9 配置启用 --待续

    1.worker_processes worker_processes 4;## 4核,所以设置4个 worker_cpu_affinity 0001 0010 0100 1000; nginx在启动 ...

  6. centos下vsftpd安装与配置

    1.已经配置好可以上网了,所以即可通过yum install vsftpd安装啦.如果不能上网则可以通过dvd2.iso工具集rpm方式进行安装. 2.安装好后,默认本地可以通过匿名用户登录,但是其它 ...

  7. 使用Visual Studio制作安装包

    目 录 第1章 合并模块    3 1.1 SystemDll    3 1.1.1 收集文件    3 1.1.2 新建项目    4 1.1.3 增加自定义文件夹    4 1.1.4 设置部署位 ...

  8. 日期操作类--Date类

    Date-API ava.util包提供了Date类来封装当前的日期和时间.Date类提供两个构造函数来实例化Date对象.第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函 ...

  9. HTML5自定义属性对象Dataset简介

    一.html5 自定义属性介绍 我之前翻译的“你必须知道的28个HTML5特征.窍门和技术”一文中对于HTML5中自定义合法属性data-已经做过些介绍,就是在HTML5中我们可以使用data-前缀设 ...

  10. robotframework笔记3--如何编写好的测试用例使用机器人的框架

    命名 测试套件的名称   之后,你可能应该描述你的名字. 名称是从文件或目录名自动创建: 扩展了. 强调了转换空间. 如果名称都是小写,大写的单词是. 名称可以是比较长的,但是太长的名字不方便 文件系 ...