hdu 1394 线段树
来源:互联网 发布:软件流量是什么 编辑:程序博客网 时间:2024/05/18 00:54
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16046 Accepted Submission(s): 9763
Problem Description
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.
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
101 3 6 9 0 8 5 7 4 2
Sample Output
16
/*hdu 1394 求a[i]前面小于a[i]的数的个数给你一个序列,求每个a[i]后面小于a[i]的个数,然后你可以把第一个数放到最后,这样的话sum变化:sum = sum+(n-1-a[i])-a[i];hhh-2016-02-27 15:18:09*/#include <iostream>#include <cstdio>#include <cstring>#include <ctime>#include <algorithm>#include <cmath>#include <queue>#include <map>#include <vector>typedef long long ll;using namespace std;const int maxn = 200000+5;int a[maxn];struct node{ int l,r; int num;} tree[maxn<<2];void push_up(int r){ int lson = r<<1,rson = (r<<1)|1; tree[r].num = (tree[lson].num+tree[rson].num);}void build(int i,int l,int r){ tree[i].l = l,tree[i].r = r; tree[i].num = 0; if(l == r) { return ; } int mid = (l+r)>>1; build(i<<1,l,mid); build(i<<1|1,mid+1,r); push_up(i);}void push_down(int r){}void Insert(int i,int k){ if(tree[i].l == k && tree[i].r == k) { tree[i].num++; return ; } push_down(i); int mid = (tree[i].l + tree[i].r) >>1; if(k <= mid) Insert(i<<1,k); if(k > mid) Insert(i<<1|1,k); push_up(i);}int query(int i,int l,int r){ if(tree[i].l >= l && tree[i].r <= r) { return tree[i].num; } push_down(i); int mid = (tree[i].l+tree[i].r)>>1; int ans = 0; if(l <= mid) ans+=(query(i<<1,l,r)); if(r > mid) ans+=(query(i<<1|1,l,r)); return ans ;}int main(){ int T,n,m,cas = 1; while(scanf("%d",&n)!=EOF) { build(1,0,n-1); int sum = 0; for(int i =1; i <= n; i++) { scanf("%d",&a[i]); Insert(1,a[i]); int t; if(a[i] - 1 < 0) t = 0; else t = query(1,0,a[i]-1); sum += (a[i] - t); } int ans = sum; for(int i = 1;i <= n;i++) { sum = sum+(n-1-a[i])-a[i]; ans = min(ans,sum); } printf("%d\n",ans); } return 0;}
0 0
- HDU 1394(线段树)
- HDU 1394 线段树
- hdu 1394(线段树)
- HDU 1394 线段树
- hdu 1394 线段树
- hdu 1394线段树
- hdu 1394 线段树
- HDU 1394 线段树
- HDU 1394 线段树
- hdu 1394 线段树
- HDU 1394 线段树
- HDU 1394 线段树
- HDU 1394 线段树
- hdu 1394 线段树版~~~@ !@
- hdu 1394(线段树)
- hdu 1394(线段树)
- HDU--1394(线段树)
- hdu 1394(线段树)
- 使用脚本创建客户虚拟机模板 (文档 ID 2089028.1)
- 兼容IE的CSS写法
- 图的遍历
- Flask 简单博客用户文章评论部分
- 应用实例:照片识别 机器学习基础(10)
- hdu 1394 线段树
- 【BZOJ1088】[SCOI2005]扫雷Mine【枚举】【分类讨论】
- OCR/Vote disk 维护操作: (添加/删除/替换/移动) (文档 ID 1674859.1)
- C++语言之静态变量的运用
- kafka入门:简介、使用场景、设计原理、主要配置及集群搭建
- C#之旅 艰难的a+b(不完善版本)
- 解决关闭Hadoop时no namenode to stop异常
- hdu 3016 dp+线段树
- java中的二维数组