HDU1394(线段树||树状数组)
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17870 Accepted Submission(s): 10851
Problem Description
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
Output
Sample Input
Sample Output
逆序数的概念: 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个 逆序 。一个排列中逆序的总数就称为这个排列的 逆序数 。逆序数为 偶数 的排列称为 偶排列 ;逆序数为奇数的排列称为 奇排列 。如2431中,21,43,41,31是逆序,逆序数是4,为偶排列。
逆序数计算方法是:在逐个元素读取原始数列时,每次读取都要查询当前已读取元素中大于当前元素的个数,加到sum里,最后得到的sum即为原始数列的逆序数。
接下来找数列平移后得到的最小逆序数,假设当前序列逆序数是sum,那么将a[0]移到尾部后逆序数的改变是之前比a[0]大的数全部与尾部a[0]组合成逆序数,假设数量为x,则x=n-1-a[0],而之前比a[0]小的数(也就是之前能和a[0]组合为逆序数的元素)不再与a[0]组合成逆序数,假设数量为y,则y=n-x-1,这样,新序列的逆序数就是sum+x-y=sum-2*a[0]+n-1;
接下来说明下线段树的作用,线段区间表示当前已读取的元素个数,比如[m,n]表示在数字m到n之间有多少个数已经读入,build时所有树节点全部为0就是因为尚未读数,ins函数是将新读入的数字更新到线段树里,点更新,query函数是查询当前数字区间已存在的数字个数。
除去逆序数方面的内容,这基本就是一道线段树的模板题
//2016.8.10
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 5005
#define lson (id<<1)
#define rson ((id<<1)|1)
#define mid ((l+r)>>1) using namespace std; struct node
{
int sum;
}tree[N*]; int arr[N]; void build_tree(int id, int l, int r)
{
tree[id].sum = ;
if(l == r){
return ;
}
build_tree(lson, l, mid);
build_tree(rson, mid+, r);
return;
} void ins(int id, int l, int r, int pos)
{
if(l == r){
tree[id].sum = ;return ;
}
if(pos <= mid)
ins(lson, l, mid, pos);
else
ins(rson, mid+, r, pos);
tree[id].sum = tree[lson].sum + tree[rson].sum;
return ;
} int query(int id, int l, int r, int ql, int qr)
{
if(ql<=l&&r<=qr){
return tree[id].sum;
}
int cnt = ;
if(ql<=mid)cnt += query(lson, l, mid, ql, qr);
if(mid+<=qr)cnt += query(rson, mid+, r, ql, qr); return cnt;
} int main()
{
int n;
while(cin>>n)
{
int sum = ;
build_tree(, , n-);
for(int i = ; i < n; i++){
scanf("%d", &arr[i]);
sum+=query(, , n-, arr[i], n-);
ins(, , n-, arr[i]);
}
//得到初始序列的逆序数
int ans = sum;
for(int i = ; i < n; i++)//移动数列找最小逆序数
{
sum = sum-*arr[i]+n-;
if(ans > sum)ans = sum;
}
cout<<ans<<endl;
} return ;
}
树状数组
//2016.9.13
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 5005 using namespace std; int a[N], arr[N], n; int lowbit(int x){return x&(-x);} void add(int pos, int tt)
{
for(int i = pos; i <= n; i+=lowbit(i))
arr[i] += tt;
} int query(int pos)
{
int sum = ;
for(int i = pos; i > ; i-=lowbit(i))
sum += arr[i];
return sum;
} int main()
{
int sum, ans, tmp;
while(scanf("%d", &n)!=EOF)
{
memset(arr, , sizeof(arr));
for(int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
a[i];
}
sum = ;
for(int i = ; i <= n; i++)
{
sum += query(n-a[i]);//n-a[i]表示a[i]为第n-a[i]大
add(n-a[i], );
}
ans = sum;
for(int i = ; i < n; i++)
{
add(n-a[i], -);
sum = sum+query(n-a[i])-a[i];
add(n-a[i], );
if(ans > sum) ans = sum;
}
printf("%d\n", ans);
} return ;
}
HDU1394(线段树||树状数组)的更多相关文章
- CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)
The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...
- hdu1394(枚举/树状数组/线段树单点更新&区间求和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...
- HDU-1394 Minimum Inversion Number (逆序数,线段树或树状数组)
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树
题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...
- [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- HDU 1556 线段树或树状数组,插段求点
1.HDU 1556 Color the ball 区间更新,单点查询 2.题意:n个气球,每次给(a,b)区间的气球涂一次色,问最后每个气球各涂了几次. (1)树状数组 总结:树状数组是一个查 ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings
谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...
- HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description The inve ...
随机推荐
- 一个简单但详细的解释Windows文件映射读取数据文件的例子
#include <windows.h>#include <string.h>#include <string>#include <iostream>u ...
- codis 新版本 CodisLabs 编译安装
codis 3.0 版本编译安装 # 首先安装 go 语言 wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz ...
- LPC1768的USB-相关结构体定义
#ifndef __USB_H__ #define __USB_H__ //usb传输数据的宏定义描述 #include "sys.h" typedef __packed unio ...
- 使用DLL进行不同语言之间的调用(转)
源:使用DLL进行不同语言之间的调用 __declspec(dllexport) 是告诉编译器用来导出函数的,在代码中不另作说明了. extern "C" 的意思就是用C的方式来导 ...
- python遍历字典元素
a={'a':{'b':{'c':{'d':'e'}},'f':'g'},'h':'i'} def show(myMap): for str in myMap.keys(): secondDict=m ...
- vs生成解决方案错误无法将文件“xx.*”复制到xx.*”。对路径“bin\xx.*”的访问被拒绝
使用vs2008生成解决方案时出现的问题: 无法将文件“obj\xx.*”复制到“bin\xx.*”.对路径“bin\xx.*”的访问被拒绝 解决方法: 将*.dll的只读属性去掉(在windows对 ...
- 开启分布式事物DTC
1.web服务器开启分布式事物配置后,数据库服务器的host文件要设置 “IP web服务器主机名” 的映射,否则会 出现 “与基础事务管理器的通信失败” #跨网段使用TransactionSco ...
- 更改Windows Live Writer默认日志与草稿保存路径
目的:把保存Windows Live Writer的日志与草稿文件夹My Weblog Posts移动到E:\Blog\路径下 用mklink命令,创建E:\Blog\路径下的My Weblog Po ...
- python 爬取的数据要如何展现(可视化)?
我是把数据放在 mongodb ,然后单独一个脚本作分析,导出 json ,用 c3.js 画图,然后随便写个很简单的页面就好了. 展示在这里: http://107.170.207.236/job_ ...
- Leetcode 181. Employees Earning More Than Their Managers
The Employee table holds all employees including their managers. Every employee has an Id, and there ...