Minimum Inversion Number HDU
来源:互联网 发布:淘宝违反价格法 编辑:程序博客网 时间:2024/05/21 09:33
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.
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
10
1 3 6 9 0 8 5 7 4 2
Sample Output
16
题意:给你一串由0-n-1组成的序列,每次将头元素移动至序列尾部,对每个变换求一次逆序对,问在这些变换的序列中序列逆序对最小是多少。
思路:首先求逆序对,线段树区间 (l,r)的和 ,这个和表示序列到目前为止l,r中出现数字的次数,然后当前位置数x的逆序对就是(x+1,n)的结果,由于根据题意序列是0-n-1,元素不重复,所以避免越界可以query(x,n)。。。我建树从1建到n而且会有越界风险感觉很low2333来神的线段树好像就,神TM高端2333
然后把第一个初始序列的逆序对处理出来之后,由于这个题意这个操作啊,就很神奇 ,毛估估是有规律的对不对。看 序列 2 4 1 3 我们操作要移动2。这个2在队首对这个序列提供的逆序对贡献是比这个数小的所有数的个数,2移到队尾对逆序对的贡献是所有比2大的数字个数之和。那么我们对上一次的总数减去比当前数小的个数 加上比当前数大的个数 取一个最小就可以了
恕我直言 大半夜脑子不大好使。。我printf里面一开始写了个tot在那里狂debug。。。一度绝望。。
#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<vector>#include<cmath>#include<cstdio>#include<cstring>#include<string>#include<stack>#include<map>using namespace std;//thanks to pyf ...#define INF 0x3f3f3f3f#define CLR(x,y) memset(x,y,sizeof(x))#define mp(x,y) make_pair(x,y)typedef pair<int,int> PII;typedef long long ll;const int N = 1e5+5;struct Tree{ int l,r,sum;}t[N*4];int a[N];void push_up(int step){ t[step].sum = t[step*2].sum + t[step*2+1].sum;}void build(int l,int r,int step){ t[step].l = l; t[step].r = r; t[step].sum = 0; if(l==r) { t[step].sum = 0; return ; } int mid = (l+r)/2; build(l,mid,step*2); build(mid+1,r,step*2+1);}void update(int x,int step){ if(t[step].l==t[step].r) { t[step].sum ++; return; } int mid = (t[step].l + t[step].r)/2; if(x<=mid) update(x,step*2); else update(x,step*2+1); push_up(step);}int query(int l,int r,int step){// cout << l << " " << r << " " << t[step].l << " " << t[step].r << endl; if(t[step].l == l&&t[step].r == r) return t[step].sum; int mid = (t[step].l + t[step].r)/2; if(r<=mid) return query(l,r,step*2); else if(l>mid) return query(l,r,step*2+1); else return query(l,mid,step*2) + query(mid+1,r,step*2+1);}int main(){ int n; while(scanf("%d",&n)==1) { for(int i=0;i<n;i++) { int x; scanf("%d",&x); a[i] = x+1; } build(1,n,1); int tot = 0; for(int i=0;i<n;i++) {// cout << a[i] << endl; tot += query(a[i],n,1); update(a[i],1); } int ans = tot; for(int i=0;i<n;i++) { tot += n-a[i]; tot -= a[i] -1;// cout << ans << " "<< tot << endl; ans = min(ans,tot); } printf("%d\n",ans); }}
- Minimum Inversion Number HDU
- HDU Minimum Inversion Number
- HDU Minimum Inversion Number
- Minimum Inversion Number HDU
- Minimum Inversion Number HDU
- hdu 1394 Minimum Inversion Number
- hdu 1394 Minimum Inversion Number
- hdu 1394 Minimum Inversion Number
- HDU 1394 Minimum Inversion Number
- HDU-1394-Minimum Inversion Number
- HDU 1394 - Minimum Inversion Number
- HDU 1394 Minimum Inversion Number
- Hdu 1394 Minimum Inversion Number
- hdu 1394 Minimum Inversion Number
- hdu 1394 Minimum Inversion Number
- hdu 1394 Minimum Inversion Number
- hdu 1394 Minimum Inversion Number
- Hdu 1394 Minimum Inversion Number
- centos iptable端口转发 端口映射
- [Azure]ARM模式下没有公网IP地址的虚拟机如何与外部通信
- 操作系统实验六之多线程矩阵乘法
- Shell脚本编程的常识
- jQuery过滤选择器详解
- Minimum Inversion Number HDU
- LeetCode 15. 3Sum
- springmvc常用注解标签详解
- Solr集群搭建,zookeeper集群搭建,Solr分片管理,Solr集群下的DataImport,分词配置。
- PyCharm用法的相关介绍
- BNU 49098 神奇的身高
- BNU 49097 Araleii & Bill的冠名权争夺战之登顶校赛
- BNU 49102
- caffe测试训练好的caffemodel和记录每层的运行时间