首页 > 其他分享 >HDU 1394 Minimum Inversion Number(树状数组)

HDU 1394 Minimum Inversion Number(树状数组)

时间:2023-06-12 14:35:48浏览次数:28  
标签:... HDU int Inversion Number a1 a2 include 序数


题意:有一个n个整数的排列,这n个整数就是0,1,2,3...n-1这n个数(但不一定按这个顺序给出)。现在先计算一下初始排列的逆序数,然后把第一个元素a1放到an后面去,形成新排列a2 a3 a4...an a1,然后再求这个排列的逆序数。继续执行类似操作(一共要执行n-1次)直到产生排列an a1 a2...an-1为止。计算上述所有排列的逆序数,输出最小逆序数。

思路:首先由于给出的序列是0到n-1,所以要+1,因为树状数组第0项是无法使用的。然后先求出一开始序列的逆序数,现在问题是怎么求题目要求的经过n-1次变换之后的最小逆序数呢?

           我们假设一开始的数列是 5 1 2 3 4 6 7 8 9 10,将5放到最后,那么就有4个比5小的逆序数减少1,有10-5个比5大的逆序数增加1,那么变换之后的序列数的公式就推出来了,就是ans-(ai-1)+(n-ai),自己推一下看看,那么这道题就解决了。



#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 5000+100
#define LL long long
int cas=1,T;
int c[maxn];
int lowbit(int x)
{
	return x&(-x);
}
int sum (int x)
{
	int ans = 0;
	while (x)
	{
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}

void add(int x,int v)
{
	while (x<=maxn)
	{
		c[x]+=v;
		x+=lowbit(x);
	}
}
int a[maxn];
int main()
{
	int n;
	while (scanf("%d",&n)!=EOF )
	{
		int anss = 0;
		memset(c,0,sizeof(c));
		for (int i = 1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			a[i]++;
			anss+=sum(n)-sum(a[i]);
			add(a[i],1);
		}
		int ans = anss;
		for (int i = 1;i<=n;i++)
		{
			anss +=n-2*a[i]+1;
			ans = min(ans,anss);
		}
		printf("%d\n",ans);
	}
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}






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



 






标签:...,HDU,int,Inversion,Number,a1,a2,include,序数
From: https://blog.51cto.com/u_16156555/6462541

相关文章

  • HDU 3743 Frosh Week(树状数组+离散化)
    题意和思路:和POJ2299几乎一样...离散化+树状数组#include<cstdio>#include<queue>#include<cstring>#include<iostream>#include<cstdlib>#include<algorithm>#include<vector>#include<map>#include<string>#includ......
  • POJ 2352 HDU1541 Stars(树状数组)
    题意:二维平面给定n个点的坐标,然后要你输出每个点的“等级“。每个点的等级是它的左下放的点个数(包括正下放和正左方的点)。即要你输出对于每个点(x,y)来说,有多少点的坐标(xi,yi)满足xi<=x且yi<=y。思路:题目给出的坐标中已经是按y升序排列,那么其实只用考虑x轴,那么显然就是在前面的......
  • HDU 3277 Marriage Match III(并查集+二分+最大流)
    题意:和HDU3081一样的题意,只不过多了一个条件,每个女孩除了能选自己喜欢的男生之外,还能选不超过K个自己不喜欢的男生,问游戏最多能进行几轮思路:除了选喜欢的,还能选任意K个不喜欢的,怎么建图呢?一开始我想每个女孩连喜欢的男孩,而且选K个不喜欢的男孩也连边,可是这K个要怎么确定呢?这种显然......
  • HDU 3081 Marriage Match II(二分+并查集+最大流)
    题意:有N个女孩要与N个男孩玩配对游戏.每个女孩有一个可选男孩的集合(即该女孩可以选自己集合中的任意一个男孩作为该轮的搭档).然后从第一轮开始,每个女孩都要和一个不同的男孩配对.如果第一轮N个女孩都配对成功,那么就开始第二轮配对,女孩依然从自己的备选男孩集合中选择,但是不能......
  • HDU 1556 Color the ball(树状数组区间更新)
    题意:中文题思路:一道区间更新,单点查询的裸题,用线段树做更好,因为还没看到所以这里用树状数组做。树状数组标记区间的方法很特别,比如给区间[a,b]内的气球涂颜色时,我们add(a,1),add(b+1,-1),单点查询的时候sum(x)就是x这个气球被涂色的总次数。建议先在纸上自己试一下看看,有点抽象,可以这......
  • hdu2084数塔(DP)
    思路:简单的数塔DP#include<stdio.h>#include<string.h>#include<algorithm>usingnamespacestd;inta[105][105],dp[105][105];intmain(){intt,n,i,j;scanf("%d",&t);while(t--){scanf("%d",......
  • HDU 5491 The Next(构造)
    题意:给你D(D<=2^31),s1和s2,求比D大的第一个数并且这个数二进制1的数目在[s1,s2]范围里,输出这个数思路:直接从D+1算起,如果1的数目>s2那就跳过,如果s1>sum1,那么就将这个数的低位s1-sum1个0补成1,那么就是最优的了#include<bits/stdc++.h>usingnamespacestd;#defineLLlonglongint......
  • HDU 5489 Removed Interval(DP)
    题意:求去掉某一个长度为L的子串的LIS思路:画画图其实比较显然的想法是去掉这个区间的时候答案是右边以第一个数开头的LIS+左边最后一个数小于右边第一个数的LIS,为什么是右边以第一个数开头的LIS呢,因为如果是在这个L的后第二个是最佳答案的话那么我在这个“窗口”滑到L+1这个位置的时......
  • HDU 5492 Find a path(DP)
    思路:将式子化开其实就是求(n+m-1)*s1-s2的最小值,s1表示各个格子的平方和,s2表示和的平方,留意到数据范围较小,令dp[i][j][k]为走到第i行第j列当前和为k的平方和的最小值,最后答案就是(n+m-1)*dp[i][j][k]-k*k#include<bits/stdc++.h>usingnamespacestd;#defineinf1e9inta[33][3......
  • Palindrome Number
    Givenanintegerx,returntrueifxisapalindrome,andfalseotherwise.Example1:Input:x=121Output:trueExplanation:121readsas121fromlefttorightandfromrighttoleft.Example2:Input:x=-121Output:falseExplanation:Fromleftto......