题目传送门

Minimum Inversion Number

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

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
 
Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1698 1540 1542 1255 2795 
题意:给你一个环状数列,比如1234,转一下成2341等等,问你在每次转化的时候,
        形成的数列最小的逆序数是多少?
题解:先用O(logn)的时间求出原始数列的逆序数,然后我们从上面的转移中可以发现,
        每次形成的新数列都是原数列头一个数被转移到最末的位置,即每次要从之前的逆序数
        先-a[i](失去开头数的逆序数),再+n-1-a[i](在末尾新增的逆序数);最后一直枚举
        下来边求最小值即可
代码:
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=;
int sum[maxn<<];
void PushUP(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void build(int l,int r,int rt)
{
sum[rt]=;
if(l==r) return ;
int m=(l+r)>>;
build(lson);
build(rson);
PushUP(rt);
}
void update(int p,int l,int r,int rt)
{
if(l==r){
sum[rt]++;
return ;
}
int m=(l+r)>>;
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 ret=;
int m=(l+r)>>;
if(L<=m) ret+=query(L,R,lson);
if(R>m) ret+=query(L,R,rson);
return ret;
}
int main()
{
int n;
int a[maxn];
while(~scanf("%d",&n))
{
build(,n-,);
int ans=;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
ans+=query(a[i],n-,,n-,);
update(a[i],,n-,);
}
int cnt=ans;
for(int i=;i<n;i++)
{
ans+=n-a[i]-a[i]-;
cnt=min(ans,cnt);
}
printf("%d\n",cnt);
}
return ;
}
 

hdu1394 Minimum Inversion Number (线段树求逆序数&&思维)的更多相关文章

  1. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  2. HDU_1394_Minimum Inversion Number_线段树求逆序数

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  3. hdu1394(Minimum Inversion Number)线段树

    明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...

  4. HDU-1394 Minimum Inversion Number 线段树+逆序对

    仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...

  5. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  6. hdu 13394 Minimum Inversion Number 线段树

    题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...

  7. HDU-1394 Minimum Inversion Number(线段树求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  8. HDU - 1394 Minimum Inversion Number (线段树求逆序数)

    Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs ( ...

  9. Minimum Inversion Number(线段树求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

随机推荐

  1. nginx location配置讲解

    location语法:表示uri方式定位 基础语法有三种: location = pattern {} 精准匹配 location pattern {} 一般匹配 location ~ pattern ...

  2. centos7操作防火墙

    1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...

  3. Django 路由层与视图层

    1.路由层 1.1无名分组 1.2 有名分组 1.3 反向解析 1.4 路由分发 1.5 名称空间 2.伪静态网页 3.虚拟环境 4.视图层 1.1 JsonResponse 1.2 FBV与CBV ...

  4. openstack stein部署手册 5. placement

    # 建立数据库用户及权限 create database placement; grant all privileges on placement.* to placement@'localhost' ...

  5. openstack stein部署手册 3. keystone

    # 建立数据库用户及权限 create database keystone; grant all privileges on keystone.* to keystone@'localhost' id ...

  6. export default{} 和 new Vue()都是什么意思

    在生成.导出.导入.使用 Vue 组件的时候,有些新手就会常常被位于不同文件的 new Vue() 和 export default{} 搞得晕头转向.它们含义到底是什么,又有什么异同呢? 首先,Vu ...

  7. Python---基础---数据类型的内置函数

    2019-05-23 ---------------------------- 一. #数据类型的内置函数Python有哪些数据类型?Number   数值型string   字符型list     ...

  8. 卷积神经网络(Text--cnn)(知识点整理)

    参考:http://www.wildml.com/2015/12/implementing-a-cnn-for-text-classification-in-tensorflow/ 独热编码(one- ...

  9. HashMap测试程序2

    package com.iotek.map; import java.util.HashMap;import java.util.Map; public class HashMapDemo2 { /* ...

  10. Jmeter参数化控件意见收集

    1.可以读取EXCEL,可以自定义SHEET,行和列: 2.数据可以加密传输,加密方式如下: 1)SHA1 2)SHA224 3)SHA256 4)SHA384 5)SHA512 6)MD5 7)Hm ...