HDU 1394 Minimum Inversion Number (线段树:单点增减求和)
2016-09-12 21:12
501 查看
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18375 Accepted Submission(s): 11156
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each case, output the minimum inversion number on a single line.
[align=left]Sample Input[/align]
10
1 3 6 9 0 8 5 7 4 2
[align=left]Sample Output[/align]
16
[align=left]Author[/align]
CHEN, Gaoli
[align=left]Source[/align]
ZOJ Monthly, January 2003
[align=left]Recommend[/align]
Ignatius.L | We have carefully selected several similar problems for you: 1698 1540 1542 1255 2795
题解:问你不断地将开头的数放到结尾。求其中最小的逆序数。
线段树求出最初的逆序数。复杂度O(nlogn),然后O(n)查询其他解。
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000") //#include<bits/stdc++.h> #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <map> #include <cmath> #include <queue> #include <set> #include <bitset> #include <iomanip> #include <list> #include <stack> #include <utility> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef vector<int> vi; const double eps = 1e-8; const int INF = 1e9+7; const ll inf =(1LL<<62) ; const int MOD = 1e9 + 7; const ll mod = (1LL<<32); const int N =1e6+6; const int M=100010; const int maxn=55555; #define mst(a) memset(a, 0, sizeof(a)) #define M_P(x,y) make_pair(x,y) #define in freopen("in.txt","r",stdin) #define rep(i,j,k) for (int i = j; i <= k; i++) #define per(i,j,k) for (int i = j; i >= k; i--) #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1 | 1 const int lowbit(int x) { return x&-x; } //const int lowbit(int x) { return ((x)&((x)^((x)-1))); } int read(){ int v = 0, f = 1;char c =getchar(); while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();} while(48 <= c && c <= 57) v = v*10+c-48, c = getchar(); return v*f;} int sum[maxn<<2]; int x[maxn]; void pushup(int rt) //把当前结点的信息更新到父结点 { //线段树是用数组来模拟树形结构 //对于每一个节点rt,左子节点为 2*rt (一般写作rt<<1)右子节点为 2*rt+1(一般写作rt<<1|1) sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } void build(int l,int r,int rt) { sum[rt] = 0; if(l==r) return ; int mid=(l+r)>>1; build(lson);//递归构造左子树 build(rson);//递归构造右子树 pushup(rt); //更新求和 } void update(int p, int l, int r, int rt)//单点增 { if(l==r){ sum[rt] ++; return ; } int mid=(l+r)>>1; if(p<=mid) update(p,lson); else update(p,rson); pushup(rt); } int query(int L,int R,int l,int r,int rt) //区间求逆序数 { if(L <= l && r <= R){ return sum[rt]; } int mid = (l+r)>>1; int ans = 0; if(L <= mid) ans+=query(L,R,lson); if(R > mid) ans+=query(L,R,rson); return ans; } int main() { int n; while(~scanf("%d",&n)) { build(0,n-1,1); int sum = 0; for(int i=0; i<n; i++) { scanf("%d",&x[i]); sum+=query(x[i],n-1,0,n-1,1); update(x[i],0,n-1,1); } int ans = sum; for(int i=0;i<n;i++) { sum=sum-x[i]; //减少了的逆序数 sum+=n-x[i]-1; //增加了的逆序数 ans=min(ans,sum); } cout<<ans<<endl; } return 0; }
相关文章推荐
- 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(线段树 单点更新 区间查询)
- hdu1394 Minimum Inversion Number 线段树,单点更新
- HDU1394 Minimum Inversion Number(线段树单点更新,暴力,逆序数)
- HDU 1394 Minimum Inversion Number (线段树 单点更新 求逆序数)
- HDOJ 1394 Minimum Inversion Number 线段树 : 单点更新 成段求和
- HDU1394-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(线段树 单点更新)