Inversion (hdu 4911 tree-like array | | Merge sort to reverse order)

Source: Internet
Author: User
Tags acos cmath

InversionTime limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 2003 Accepted Submission (s): 787


Problem Descriptionbobo has a sequence a1,a2,...,. He is allowed to swap adjacentNumbers for no more than k times.

Find the minimum number of inversions after his swaps.

Note:the number of inversions is the number of pair (I,J) where 1≤i<j≤n and Ai>aj.
Inputthe input consists of several tests. For each tests:

The first line contains 2 integers n,k (1≤n≤105,0≤k≤109). The second line contains n integers a1,a2,..., an (0≤ai≤109).
Outputfor each tests:

A single integer denotes the minimum number of inversions.
Sample Input
3 12 2 13 02 2 1

Sample Output
12

Authorxiaoxu Guo (Ftiasch)
Source2014 multi-university Training Contest 5
Recommendwe carefully selected several similar problems for you:5197 5196 5193 5192 5189

Test instructions: In order to find the number of n reverse logarithm, you can exchange K adjacent, so the original sequence of reverse order minus K.

Idea: There are two ways to reverse the order, merging the sort and the tree array. Several methods to find the right of reverse order

Code:

Tree-like array # include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include < set> #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 100005#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid# Define Rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b; i++) #define FREE (i,a,b) for (i = A; I >= B; i --) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset (( T), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (A, B,C) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long LONG LL ; using namespace Std;int n,k,len;int bit[maxn];int a[maXn],num[maxn];map<int,int>pos;int cmp (int a,int b) {return a>b;} int lowbit (int i) {return i&-i;}    ll sum (int i) {ll s=0;        while (i>0) {s+=bit[i];    I-=lowbit (i); } return s;}        void Add (int i,int x) {while (I&LT;MAXN) {bit[i]+=x;    I+=lowbit (i);    }}ll solve () {int i,j;    ll Ans=0;        FRE (i,1,n) {ans+=sum (pos[num[i]]-1);    Add (pos[num[i]],1);    } ans-=k;    if (ans<0) ans=0; return ans;}    int main () {int i,j;        while (~SFF (n,k)) {pos.clear ();        MEM (bit,0);            FRE (i,1,n) {SF (a[i]);        Num[i]=a[i];        } sort (a+1,a+n+1,cmp);        Len=1;        Pos[a[1]]=len;        FRE (i,2,n)//map de-weight, discretized if (a[i]!=a[i-1]) Pos[a[i]]=++len;        PF ("len=%d\n", Len);    PF ("%i64d\n", Solve ()); } return 0;}

Merge sort # include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include < set> #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 100005#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid# Define Rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b; i++) #define FREE (i,a,b) for (i = A; I >= B; i --) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset (( T), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (A, B,C) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long LONG LL ; using namespace Std;int a[maxn];int N,k;__int64 ans;voID Merge (int a[],int l,int mid,int r) {int i,j,k=l;    int *b=new int[r+1];    FRE (I,l,r) b[i]=a[i];    i=l,j=mid+1;        while (i<=mid&&j<=r) {if (B[i]<=b[j]) a[k++]=b[i++];            else{a[k++]=b[j++];        ans+= (mid-i+1);    }} while (I<=mid) a[k++]=b[i++];    while (J&LT;=R) a[k++]=b[j++]; delete []b;}    void Merge_sort (int a[],int l,int R) {if (l==r) return;    int mid= (L+R) >>1;    Merge_sort (A,l,mid);    Merge_sort (A,MID+1,R); Merge (a,l,mid,r);}    int main () {int i,j;        while (~SFF (n,k)) {FRE (i,1,n) SF (A[i]);        ans=0;        Merge_sort (A,1,n);        Ans-=k;        if (ans<0) ans=0;    PF ("%i64d\n", ans); } return 0;}



Inversion (hdu 4911 tree-like array | | Merge sort to reverse order)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.