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 ...
随机推荐
- cocos2dx-lua绑定自定义c++类(一)
本文主要介绍mac上,如何将自定义的c++类,绑定到lua. 1.工具先行 找到 你的cocos2d-x/tools/tolua++,里面文件按类型大致分为: (1)*.pkg:用于定义要绑定的c++ ...
- euctb
- 第32讲 UI组件之 时间日期控件DatePicker和TimePicker
第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置, Time ...
- JavaScript 数组的遍历
var a = [1, 2, 3]; // for循环 for(var i = 0; i < a.length; i++) { console.log(a[i]); } // while循环 v ...
- [转]Jquery中AJAX错误信息调试参考
下面是Jquery中AJAX参数详细列表: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET") 请求 ...
- JSON解析---初识
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式 全然独立于语言的文本格式 易于人阅读和编写 易于解析和生成 (网络传输速度快) JSON语法规则 数据在 ...
- Opencv 简单的图片显示
#include <opencv\cv.h> #include <opencv\highgui.h> #include <opencv\cxcore.h> int ...
- ASP.NET 根据现有动态页面生成静态Html
现有动态页面的格式都是类似 pageName.aspx?ID=1的格式,后面由于发布服务器的原因,要求将动态页面转为静态html后上传. 首先根据页面生成的格式,枚举获取页面html: foreach ...
- 巧记--Css选择器
love ------> hate 即: a:link --> a:visited --> a:hover --> a:active a:link ...
- Java 访问控制符
Java提供了3个访问控制符:private.protected和public,分别代表了3个访问控制级别,另外还有一个不加任何访问控制符的访问控制级别,提供了4个访问控制级别.Java的访问控制级别 ...