HDU - 1394 Minimum Inversion Number (线段树求逆序数)
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
10
1 3 6 9 0 8 5 7 4 2
Sample Output
16
题意:每次能够将第一个放到最后一个,求这个过程中最小的逆序数和
思路:线段树的点更新,首先读入序列的时候就能够算出一个逆序数,找找比它大的数的个数,
之后将第一个a[0]放到最后一个的时候,每次降低的是a[0],添加的是(n-a[0]-1)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define lson(x) ((x) << 1)
#define rson(x) ((x) << 1 | 1)
using namespace std;
const int MAXN = 5005;
const int ROOT = 1;
const int inf = 0x3f3f3f3f;
// single point updated
struct seg{
int w;
}; struct segment_tree {
seg node[MAXN * 4]; void update(int pos) {
node[pos].w = node[lson(pos)].w + node[rson(pos)].w;
} void build(int l, int r, int pos) {
if (l == r) { node[pos].w = 0; return; }
int m = l + r >> 1;
build(l, m, lson(pos));
build(m + 1, r, rson(pos));
update(pos);
} void modify(int l, int r, int pos, int x, int y) {
if (l == r) { node[pos].w = 1; return; }
int m = l + r >> 1;
if (x <= m) modify(l, m, lson(pos), x, y);
else modify(m + 1, r, rson(pos), x, y);
update(pos);
} int query(int l, int r, int pos, int x, int y) {
if (x <= l && r <= y) return node[pos].w;
int m = l + r >> 1;
int res = 0;
if (x <= m) res += query(l, m, lson(pos), x, y);
if (y > m) res += query(m + 1, r, rson(pos), x, y);
return res;
} int remove(int l, int r, int pos, int x) {
if (l == r) { node[pos].w = 0; return l; }
int m = l + r >> 1;
int res;
if (x < node[lson(pos)].w) res = remove(l, m, lson(pos), x);
else res= remove(m + 1, r, rson(pos), x - node[lson(pos)].w);
update(pos);
return res;
}
} tree; int main() {
int n;
int a[MAXN];
while (scanf("%d", &n) != EOF) {
tree.build(1, n, 1);
int cnt = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
cnt += tree.query(1, n, 1, a[i]+1, n);
tree.modify(1, n, 1, a[i]+1, 1);
}
int ans = cnt;
for (int i = 0; i < n-1; i++) {
cnt += (n - 2*a[i] - 1);
ans = min(ans, cnt);
}
printf("%d\n", ans);
}
return 0;
}
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 (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- 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了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...
- 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_线段树求逆序数
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 (树状数组求逆序对)
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- HDU 1394 Minimum Inversion Number (树状数组)
题目链接 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the numb ...
- HDU 1394 Minimum Inversion Number(树状数组/归并排序实现
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- 实现能够直接粘QQ贴截图的bug管理功能
对于一个功能强大的协作平台来说,todo管理和bug管理是不可缺少的功能.Todo和bug往往不是通过一些简单的文字就能实现的,有时候须要配以图片的说名,之前用过的项目管理平台都是以附件的形式上传图片 ...
- Memcahce(MC)系列(两)Linux下一个Memcache安装
Linux下一个memcache安装 memcache是高性能.分布式的内存对象缓存系统,用于在动态应用中降低数据库负载.提升訪问速度.眼下用memcache解决互联网上的大用户读取是很流行的一种使用 ...
- Directx11学习笔记【九】 【转】 3D渲染管线
原文地址:http://blog.csdn.net/bonchoix/article/details/8298116 3D图形学研究的基本内容,即给定场景的描述,包括各个物体的材质.纹理.坐标等,照相 ...
- Directx11学习笔记【一】 最简单的windows程序HelloWin
声明:本系列教程代码有部分来自dx11龙书及dx11游戏编程入门两本书,后面不再说明 首先,在vs2013中创建一个空的解决方案Dx11Demo,以后的工程都会放在这个解决方案下面.然后创建一个win ...
- 文章13称号 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 自己动手写CPU之第八阶段(4)——转移指令实现过程2
将陆续上传本人写的新书<自己动手写CPU>,今天是第36篇,我尽量每周四篇 开展晒书评送书活动,在亚马逊.京东.当当三大图书站点上,发表<自己动手写CPU>书评的前十名读者,均 ...
- 编hadoop-1.X源代码
满足需要在不久的将来windows调试Linux下一个hadoop问题,Linux检查时需要的文件权限.和windows在没有必要,因此,有必要修改hadoop源代码,再次编译,过程例如以下: (1) ...
- SUPPORTDIR引用的文件的加入
上一篇转载了SUPPORTDIR的理论解释,如今截图说明实际操作: 选择依赖的语言,在右面的files框里右键点击,选择Insert Files插入文件,编译.由于安装得时候,压缩包会解压成一个暂时文 ...
- net开源cms系统
.net开源cms系统推荐 内容目录: 提起开源cms,大家第一想到的是php的cms,因为php开源的最早,也最为用户和站长们认可,随着各大cms系统的功能的不断完善和各式各样的开源cms的出现,. ...
- Partitioner分区过程分析
Partition中国人意味着分区,意义的碎片,这个阶段也是整个MapReduce该过程的第三阶段.在Map返回任务,是使key分到通过一定的分区算法.分到固定的区域中.给不同的Reduce做处理,达 ...