hdu 1394 Minimum Inversion Number - 树状数组
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
题目大意 求循环排列最小逆序对数。
老是刷cf,不做暑假作业估计会被教练鄙视,所以还是做做暑假作业。
先用各种方法算出原始序列的逆序对数。
显然你可直接算出把开头的一个数挪到序列后面增加的逆序对数。(后面有多少个比它大减去有多少个比它小)
于是这道题就水完了。
Code
/**
* hdu
* Problem#1394
* Accepted
* Time: 62ms
* Memory: 1680k
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const signed long long llf = (signed long long)((1ull << ) - );
const double eps = 1e-;
const int binary_limit = ;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} #define lowbit(x) ((x) & (-x)) typedef class IndexedTree {
public:
int* lis;
int s;
IndexedTree() { }
IndexedTree(int s):s(s) {
lis = new int[(s + )];
memset(lis, , sizeof(int) * (s + ));
} inline void add(int idx, int val) {
for(; idx <= s; idx += lowbit(idx))
lis[idx] += val;
} inline int getSum(int idx) {
int ret = ;
for(; idx; idx -= lowbit(idx))
ret += lis[idx];
return ret;
}
}IndexedTree; int n;
int* arr;
inline boolean init() {
if(!readInteger(n)) return false;
arr = new int[(n + )];
for(int i = ; i <= n; i++)
readInteger(arr[i]), arr[i] += ;
return true;
} IndexedTree it;
int ans, cmp;
inline void solve() {
ans = , cmp = ;
it = IndexedTree(n);
for(int i = ; i <= n; i++) {
it.add(arr[i], );
// cout << 1;
cmp += i - it.getSum(arr[i]);
}
ans = cmp;
for(int i = ; i < n; i++) {
cmp -= arr[i] - , cmp += n - arr[i];
smin(ans, cmp);
}
printf("%d\n", ans);
} inline void clear() {
delete[] arr;
delete[] it.lis;
} int main() {
while(init()) {
solve();
clear();
}
return ;
}
hdu 1394 Minimum Inversion Number - 树状数组的更多相关文章
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
- HDU 1394 Minimum Inversion Number (树状数组 && 规律 && 逆序数)
题意 : 有一个n个数的数列且元素都是0~n-1,问你将数列的其中某一个数及其前面的数全部置到后面这种操作中(比如3 2 1 0中选择第二个数倒置就产生1 0 3 2)能产生的最少的逆序数对是多少? ...
- [hdu1394]Minimum Inversion Number(树状数组)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- 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(逆序数对) : 树状数组 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: 32768 K Description The inve ...
随机推荐
- C#学习入门第一篇
1. using System; using System.Collections.Generic; using System.Ling; using System.Text; using Syste ...
- ssh整合not found class 异常总结
(1)org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.sqls ...
- spring之继承配置
我们有一下两个类,并且Gradate类继承了Student类 public class Student public class Gradate extends Student 在applicatio ...
- tp 例子=登录逻辑
<?php namespace Home\Controller; use Think\Controller; class LoginController extends Controller{ ...
- Unity shader学习之逐像素漫反射光照模型
shader如下: Shader "Custom/Diffuse Fragment-Level" { Properties { _Diffuse (,,,) } SubShader ...
- 事件响应模型(游戏引擎、JAVA中等应用)
事件,我们在生活中时时在产生事件并且做出响应,如早晨出门时,看见外面下雨了,这时候我们需要带把伞等情况! 在现实生活之中事件分为人为事件和自然事件,那么在计算机操作系统中也不例外,存在两种事件 1.人 ...
- <8>Cocos Creator组件开发cc.Component
1.组件简介 组件是Cocos Creator的主要构成,渲染(场景显示内容).逻辑.用户输入反馈.计时器等等几个方面都是由组件完成的.根据Cocos Creator的总体架构,组件和节点配合完成游戏 ...
- git时光机操作
A状态:代码版本A B状态:代码版本B(比A状态时增加了图片.代码) 这时,git add. git commit -m"" .push之前,意识到忘了让git忽略图片的添加,就: ...
- jQuery选择器--:eq(index)、:lt(index)和:gt(index)
:eq(index) 概述 匹配一个给定索引值的元素 参数 index 从 0 开始计数 :gt(index) 概述 匹配所有大于给定索引值的元素 参数 index 从 0 开始计数 ...
- EasyUI添加进度条
EasyUI添加进度条 添加进度条重点只有一个,如何合理安排进度刷新与异步调用逻辑,假如我们在javascript代码中通过ajax或者第三方框架dwr等对远程服务进行异步调用,实现进度条就需要做到以 ...