HDU4911-Inversion (tree array)

Source: Internet
Author: User
Inversion Time Limit: 2000/1000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 914 accepted submission (s): 380


Problem descriptionbobo has a sequence A1, A2 ,..., An. He is allowed to swap two 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
Question: N numbers. There can be a maximum of K exchanges of numbers at adjacent locations. The following describes how to minimize the number of reverse orders: to ensure that the number of reverse orders for each exchange is reduced. Refer to the practices of Bubble sorting. Finally, you only need to calculate the reverse order of the original series and then subtract K.
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <string>#include <algorithm>#include <queue>#include <map>using namespace std;const int maxn = 100000+10;int sum[maxn];int n,k;int num[maxn],tn[maxn];map<int,int> mma;bool cmp(int a,int b){    return a > b;}int lowbit(int x){    return x&(-x);}void add(int x,int d){    while(x < maxn){        sum[x] += d;        x += lowbit(x);    }}int getS(int x){    int ret = 0;    while(x > 0){        ret += sum[x];        x -= lowbit(x);    }    return ret;}int main(){    while(~scanf("%d%d",&n,&k)){        mma.clear();        memset(sum,0,sizeof sum);        for(int i = 1; i <= n; i++){            scanf("%d",&num[i]);            tn[i] = num[i];        }        sort(tn+1,tn+n+1,cmp);        int i = 1,cnt = 1;        while(i <= n){            mma[tn[i]] = cnt;            int j = i+1;            while(j <= n && tn[i]==tn[j]){                j++;            }            cnt++;            i = j;        }        long long ret = 0;        for(int i = 1; i <= n; i++){            ret += getS(mma[num[i]]-1);            add(mma[num[i]],1);        }        long long  ans = ret-k;        if(ans < 0) ans = 0;        cout<<ans<<endl;    }    return 0;}


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.