HDU1394 Minimum Inversion Number(线段树OR归并排序)
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15113 Accepted Submission(s): 9230
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 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.
1 3 6 9 0 8 5 7 4 2
再来看这道题,求得初始数列的逆序数后,再求其他排列的逆序数有一个规律,就是
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 5500
int tre[*maxn]; void update(int num, int le, int ri, int x)
{
if(le == ri)
{
tre[num] = ;
return;
}
int mid = (le+ri)/;
if(x<=mid)
update(num*,le,mid,x);
else
update(num*+,mid+,ri,x);
tre[num] = tre[num*] + tre[num*+];
}
int query(int num, int le, int ri, int x, int y)
{
if(x<=le&&y>=ri)
{
return tre[num];
}
int mid = (le+ri)/;
int ans = ;
if(x<=mid)
ans += query(num*,le,mid,x,y);
if(y>mid)
ans += query(num*+,mid+,ri,x,y);
return ans;
}
int a[maxn];
int main()
{
int n;
while(~scanf("%d", &n))
{
memset(tre, , sizeof tre);
int sum = ; for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
update(,,n-,a[i]); sum += query(,,n-,a[i]+,n-);
} int ans = sum;
for(int i = ; i < n; i++)
{
sum = sum - a[i] + (n--a[i]);
ans = min(ans, sum);
}
printf("%d\n", ans);
}
return ;
}
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 5500 int cnt;
int t[maxn],a[maxn],b[maxn];
void merge_sort(int x,int y)//归并排序模板
{
if(y-x>)
{
int m=x+(y-x)/;
int p=x,q=m,i=x;
merge_sort(x,m);
merge_sort(m,y);
while(p<m||q<y)
{
if(q>=y||(p<m&&a[p]<=a[q]))
t[i++]=a[p++];
else
{
t[i++]=a[q++];
cnt+=m-p;
}
}
for(i=x;i<y;i++)
a[i]=t[i];
}
}
int main()
{
int n;
while(~scanf("%d", &n))
{
for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
b[i] = a[i];
}
cnt = ;
merge_sort(,n);
int ans = cnt;
for(int i = ; i < n; i++)
{
cnt = cnt - b[i] + (n--b[i]);
ans = min(ans, cnt);
}
printf("%d\n", ans);
}
return ;
}
HDU1394 Minimum Inversion Number(线段树OR归并排序)的更多相关文章
- hdu1394(Minimum Inversion Number)线段树
明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...
- HDU-1394 Minimum Inversion Number 线段树+逆序对
仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...
- 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 13394 Minimum Inversion Number 线段树
题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...
- HDU 1394 Minimum Inversion Number(线段树 或 树状数组)
题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...
- hdu - 1394 Minimum Inversion Number(线段树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...
- HDU 1394 Minimum Inversion Number 线段树
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...
- 2018.07.08 hdu1394 Minimum Inversion Number(线段树)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
随机推荐
- Makefile学习(二)条件判断和内嵌函数
第七章:Makefile的条件执行 条件语句可是是两个不同的变量.或者变量和常量值的比较: 7.1例子: 对变量“CC”进行判断,其值如果是“gcc ”那么在程序连接时使用库“libgnu.so”或者 ...
- Hexo博客搭建图文教程
准备 你需要准备好以下软件: Node.js环境 Git Windows 配置Node.js环境 下载Node.js安装文件: Windows Installer 32-bit Windows Ins ...
- Chapter 4: Spring and AOP:Spring's AOP Framework -- draft
Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...
- 使用android-resource-remover删除项目中无用的资源,减少包的大小
写这篇文章的原因是,一个CSDN的资源链接,Android程序员必备精品资源,在该链接的实用工具集锦中有一个工具吸引了我的注意,那就是android-resource-remover,它的解释是:一个 ...
- ios--图片处理(修改、保存)UIGraphicsBeginImageContext
http://www.th7.cn/Program/IOS/201407/250904.shtml
- NSString字符串类型-学习总结
1.字符串的创建 (1)创建常量字符串 NSString *str = @"This is a String"; //str是变量名 (2)创建空的字符串,给字符串赋值 NSStr ...
- OD调试9—实例:深入分析代码完成软件破解
OD调试9—实例:深入分析代码完成软件破解 爆破,是最初级的解决方案,不到万不得已,我们不直接修改JNZ通关.因为这样子的话,我们就享受不到破解.逆向的真正乐趣了. 了解程序背后按照剧情发展经常会出 ...
- w530 在ubuntu 12.04 _x64 背光调节方法
So to get the screen brightness keys working with your Nvidia graphics card, create a file in the xo ...
- 关于jquery-easyUI中主键属性data-options的了解
data-options是jQuery Easyui 最近两个版本才加上的一个特殊属性.通过这个属性,我们可以对easyui组件的实例化可以完全写入到html中,例如: <div class=& ...
- Java 执行终端命令实现,调用执行另外一个Java文件
Test.java package com.journaldev.files; public class Test { public static void main(String[] args) { ...