Minimum Inversion Number HDU - 1394(逆序数变形)

2024-04-16 01:58

本文主要是介绍Minimum Inversion Number HDU - 1394(逆序数变形),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

题意:
给出一个排列,每次可以将开头的一个数移到最后面,最后变成an a1 a2 a3… an-1。求移动过程中的最小逆序数,原序列逆序数也算一种。
思路:
移动过程的逆序数变化其实是确定的。
假设开头是x,那么后面的序列大于x的数y = n - x,小于x的数z = x - 1。
逆序数变化为y - x = n - 2 * x + 1。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>using namespace std;const int maxn = 5e3 + 7;
int n;
int a[maxn],c[maxn];void add(int x,int v)
{while(x <= n){c[x] += v;x += x & (-x);}
}int query(int x)
{int res = 0;while(x){res += c[x];x -= x & (-x);}return res;
}int main()
{while(~scanf("%d",&n)){memset(c,0,sizeof(c));int ans = 0,tmp = 0;for(int i =  1;i <= n;i++){scanf("%d",&a[i]);a[i]++;tmp += query(n) - query(a[i]);add(a[i],1);}ans = tmp;for(int i = 1;i < n;i++){tmp = tmp + n - 2 * a[i] + 1;ans = min(ans,tmp);}printf("%d\n",ans);}return 0;
}

这篇关于Minimum Inversion Number HDU - 1394(逆序数变形)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907517

相关文章

上海邀请赛 A题目 HDU 5236(dp)

先求出没有ctrl+s的时候构造长度为i的期望f[i] 。然后枚举保存的次数,求出最小即可。 #include<cstdio>#include<cstdio>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<iostream>#include<map>

hdu 2586 树上点对最近距离 (lca)

,只要知道dis[i][j]=dis[i][root]+dis[j][root]-2*dis[Lca(i,j)][root].   其中root为树的根节点,LCA(i,j)为i,j的最近公共祖先。 所以我们先把所有的询问储存下来,然后离线直接查询。复杂度是o(n+q)的。 VIE #include<cstdio>#include<algorithm>#include<i

Leetcode 3195. Find the Minimum Area to Cover All Ones I

Leetcode 3195. Find the Minimum Area to Cover All Ones I 1. 解题思路2. 代码实现 题目链接:3195. Find the Minimum Area to Cover All Ones I 1. 解题思路 这一题还是挺简单的,只要找到所有1所在的元素的上下左右4个边界,作为目标矩形的四个边即可。 2. 代码实现 给出python

HDU_1013

这个题目尤其需要注意的是开始输入的时候的数的大小,开始输入时有可能非常的大超过了长整型的范围,所以不能开始用整形来存放输入的,,就是开始的时候没有注意到这个问题所以开始的时候一直没有AC,后来就是用一个数组接收输入,然后在经过第一步转化之后就可以用一个整数来装了 #include <iostream>#include <stdlib.h>#include <string>usin

最长回文子串(百度笔试题和hdu 3068)

版权所有。所有权利保留。 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/17123559 求一个字符串的最长回文子串。注意子串是连续的,子序列是不连续的。对于最长回文子序列,要用动态规划解,具体请看: http://blog.csdn.net/xiaofei_it/article/details/1

杭电ACM hdu 2110 Crisis of HDU 解题报告(母函数)

Problem Description 话说上回讲到HDU大战东洋小苟,结果自然是中方大胜,这一战也使得海东集团在全球同行业中的地位更加巩固。随着集团的发展,很多创业时期的元老逐步功成身退,先是8600移民海外,然后是linle夫妇退隐山林,逐渐的,最初众多的元老只剩下XHD夫妇和Wiskey三人了。 到了2020年,因为扩张过度加上老鼠数量逐年减少,公司的发展遇到了前所未有的危机,此时集团已经

杭电ACM hdu 2082 找单词 解题报告(母函数)

Problem Description 假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26。那么,对于给定的字母,可以找到多少价值<=50的单词呢?单词的价值就是组成一个单词的所有字母的价值之和,比如,单词ACM的价值是1+3+14=18,单词HDU的价值是8+4+21=33。(组成的单词与排列顺序无关,比如

杭电ACM hdu 2079 选课时间 解题报告(母函数)

Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合。你来帮帮他吧。(xhd认为一样学分的课没区别)   Input 输入数据的第一行是一个数据T,表示有T组数据。 每组数据的第一行是两个整数n(1 <= n <= 40),k(1 <= k <= 8)。 接着有k行,每行有两个整数a(1 <= a <= 8),b

lintcode Ugly Number II python

Description Ugly number is a number that only have factors 2, 3 and 5. Design an algorithm to find the nth ugly number. The first 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12... 利用动态规划的思想 de

iNOC产品部-杨辉三角的变形(第二种方法也可以通过,测试数据太弱,n10000就会爆的)

// iNOC产品部-杨辉三角的变形(第二种方法也可以通过,测试数据太弱,n>10000就会爆的) #include<bits/stdc++.h>using namespace std;int F(int n,int k){if(k==1||k==2*n-1)return 1;if(k<1||k>2*n-1)return 0;return F(n-1,k)+F(n-1,k-1)+F(n-1