1394-Minimum Inversion Number
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20162 Accepted Submission(s):
12110
..., 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.
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.
single line.
1 3 6 9 0 8 5 7 4 2
一个排列中逆序的总数就称为这个排列的逆序数。
思路:先暴力求出开始时的逆序数,然后会有一个性质:每次把末尾的数掉到序列前面时,减少的逆序对数为n-1-a[i] ,增加的逆序对数为a[i] ,所以求出其他情况时的逆序数。
#include<cstdio> int t[];
int n,ans,sum;
int main()
{
while (scanf("%d",&n)!=EOF)
{
sum = ;
for (int i=; i<=n; ++i)
scanf("%d",&t[i]);
for (int i=; i<n; ++i)
for (int j=i+; j<=n; ++j)
if (t[i]>t[j]) sum++;
ans = sum;
for (int i=n; i>=; --i)
{
sum -= n--t[i];
sum += t[i];
if (sum < ans) ans = sum;
}
printf("%d\n",ans);
}
return ;
}
1394-Minimum Inversion Number的更多相关文章
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- 逆序数2 HDOJ 1394 Minimum Inversion Number
题目传送门 /* 求逆序数的四种方法 */ /* 1. O(n^2) 暴力+递推 法:如果求出第一种情况的逆序列,其他的可以通过递推来搞出来,一开始是t[1],t[2],t[3]....t[N] 它的 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址: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 - 树状数组
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- hdu 1394 Minimum Inversion Number 逆序数/树状数组
Minimum Inversion Number Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...
- hdu 1394 Minimum Inversion Number(逆序数对) : 树状数组 O(nlogn)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 //hdu 题目 Problem Description The inversion number ...
- HDU 1394——Minimum Inversion Number——————【线段树单点增减、区间求和】
Minimum Inversion Number Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- HDU 1394 Minimum Inversion Number(最小逆序数 线段树)
Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...
随机推荐
- 关于《Selenium 2自动化测试实战 基于Python语言》学习过程中键盘的常用操作
下边是自己在学习过程中总结的一些常用键盘的操作
- 使flex-direction: column的子元素height: 100%生效的办法
在flex-direction: column子元素里直接使用height:100%,height并不会被设置成100% <!DOCTYPE html> <html lang=&qu ...
- Android(java)学习笔记10:同步中的死锁问题以及线程通信问题
1. 同步弊端: (1)效率低 (2)如果出现了同步嵌套,就容易产生死锁问题 死锁问题及其代码 : (1)是指两个或者两个以上的线程在执行的过程中,因争夺资源产生的一种互相等待现象 (2)同步代码块的 ...
- POJ 最小球覆盖 模拟退火
最小球覆盖:用半径最小的球去覆盖所有点. 纯粹的退火算法,是搞不定的,精度不够,不然就会TLE,根本跑不出答案来. 任取一点为球心,然后一点点靠近最远点.其实这才是最主要的. 因为:4个点确定一个球, ...
- set 和select 的区别
简单赋值是没有区别的
- P1666 前缀单词
P1666 前缀单词 tire树上跑dp 首先将trie树建出来,然后对于每个节点.考虑他的子节点. 子节点的方案数都互不干扰,所以子节点与其他子节点的的方案数可以利用乘法原理算出来. 然后如果这个节 ...
- 【Calculus 微积分の一些个人理解】
微积分 微积分(Calculus)是高等数学中研究函数的微分(Differentiation).积分(Integration)以及有关概念和应用的数学分支.它是数学的一个基础学科.内容主要包括极限.微 ...
- 用c#语言编写分解质因数
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- Oracle listener.log 清理
在oracle中,如果不对监听日志文件进行截断(定期清理),那么监听日志文件会变得越来越大,由于一些老旧的OS不支持2GB以上的文件,故当listener.log文件超过2GB时,会出现无法处理新的连 ...
- Linux/Mac scp命令笔记
scp命令用于Linux之间复制文件和目录. 参数说明: -1: 强制scp命令使用协议ssh1-2: 强制scp命令使用协议ssh2-4: 强制scp命令只使用IPv4寻址-6: 强制scp命令只使 ...