HDU 4911 inversion (merge sort to calculate the number of reverse orders)

Source: Internet
Author: User

Merge and sort to obtain the number of reverse orders, and then obtain the maximum value between ANS-K and 0.

You can also use a tree array. During the game, the posture may be incorrect. The tree array wa ,,

Inversion Time Limit: 2000/1000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 578 accepted submission (s): 249


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
 
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-12///#define M 1000100#define LL __int64///#define LL long long///#define INF 0x7ffffff#define INF 0x3f3f3f3f#define PI 3.1415926535898#define zero(x) ((fabs(x)<eps)?0:x)using namespace std;const int maxn = 10010000;int num[maxn];LL ans;int tmp[maxn];void Sell(int l, int mid, int r){    int i = l;    int j = mid+1;    int k = 0;    while(i <= mid && j <= r)    {        if(num[i] > num[j])        {            tmp[k++] = num[j++];            ans += mid-i+1;        }        else tmp[k++] = num[i++];    }    while(i <= mid)        tmp[k++] = num[i++];    while(j <= r)        tmp[k++] = num[j++];    for(int i = 0; i < k; i++)        num[i+l] = tmp[i];}void Sort(int l, int r){    if(l < r)    {        int mid = (l+r)/2;        Sort(l, mid);        Sort(mid+1, r);        Sell(l, mid, r);    }}int main(){    int n, k;    while(cin >>n>>k)    {        ans = 0;        for(int  i = 0;i < n; i++) scanf("%d",&num[i]);        Sort(0, n-1);        cout<<max(0LL, ans-k)<<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.