hdu 4911 Inversion

Inversion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 197    Accepted Submission(s): 82

Problem Description

bobo has a sequence a1,a2,…,an. He is allowed to swap two adjacent numbers 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.

Input

The 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).

Output

For each tests:

A single integer denotes the minimum number of inversions.

Sample Input

3 1
2 2 1
3 0
2 2 1

Sample Output

1
2

Author

Xiaoxu Guo (ftiasch)

Source

2014 Multi-University Training Contest 5

题解及代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
__int64 ans=0;

void merge(int Array[],int l,int m,int r)
{
    int temp[100110];
    int i=l,j=m+1,k=0;
    while(i<=m&&j<=r)
    {
        if(Array[i]<=Array[j])
        {
            temp[k++]=Array[i];
            i++;
        }
        else
        {
            temp[k++]=Array[j];
            j++;
            ans+=(m-i+1);     //求逆序数的关键
        }
    }
    while(i<=m) {temp[k++]=Array[i];i++;}
    while(j<=r) {temp[k++]=Array[j];j++;}

    for(i=l,j=0;i<=r&&j<k;i++,j++)
    {
        Array[i]=temp[j];
    }
}

void mergesort(int Array[],int l,int r)
{
    if(l>=r) return;
    int m=(l+r)/2;

    mergesort(Array,l,m);
    mergesort(Array,m+1,r);

    merge(Array,l,m,r);
}

int main()
{
    int n,k;
    int Array[100110];
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        ans=0;
        for(int i=0;i<n;i++)
        {
            cin>>Array[i];
        }
        mergesort(Array,0,n-1);
        if(k>=ans)
        {
            printf("0\n");
        }
        else printf("%I64d\n",ans-k);
    }
    return 0;
}
/*
简单题,本题主要是求出当前数列的逆序数。

我们直到每次调序都能将逆序数减少1,我们先求出逆序数,
然后判断,当给出的调序次数小于当前序列的逆序数时,输出ans-k,
否则输出0。
*/

hdu 4911 Inversion,布布扣,bubuko.com

时间: 2024-12-25 23:21:22

hdu 4911 Inversion的相关文章

HDU 4911 Inversion 求逆序数对

点击打开链接 Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1106    Accepted Submission(s): 474 Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two adjacent num

HDU 4911 Inversion(归并求逆序对)

HDU 4911 Inversion 题目链接 题意:给定一个数组,可以相邻交换最多k次,问交换后,逆序对为多少 思路:先利用归并排序求出逆序对,然后再减去k就是答案 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 100005; int n, num[N], save[N], sn; void init() { for

hdu 4911 Inversion(归并)

题目链接:hdu 4911 Inversion 题目大意:给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 解题思路:每交换一次一定可以减少一个逆序对,所以问题转换成如何求逆序对数. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int maxn = 1e5+5; ll k

2014多校第五场1001 || HDU 4911 Inversion (归并求逆序数)

题目链接 题意 : 给你一个数列,可以随意交换两相邻元素,交换次数不超过k次,让你找出i < j 且ai > aj的(i,j)的对数最小是多少对. 思路 : 一开始想的很多,各种都想了,后来终于想出来这根本就是求逆序数嘛,可以用归并排序,也可以用树状数组,不过我们用树状数组做错了,也不知道为什么.求出逆序数来再减掉k次,就可以求出最终结果来了.求逆序数链接1,链接2 1 #include <stdio.h> 2 3 int left[250003], right[250003];

hdu 4911 Inversion(归并排序求逆序对数)

Inversion                                                                             Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two 

hdu 4911 Inversion(求逆序数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 528    Accepted Submission(s): 228 Problem Description bobo has a sequence a1,a2,

HDU 4911 Inversion(归并排序求逆序数)

归并排序求逆序数,然后ans-k与0取一个最大值就可以了. 也可以用树状数组做,比赛的时候可能姿势不对,树状数组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 Description bobo has a seque

HDU 4911 Inversion(基本算法-排序)

Inversion Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two adjacent numbers 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

hdu - 4911 - Inversion(离散化+树状数组)

题意:一个由n个非负整数组成的序列,问进行最多k次相邻交换后最少的逆序对数 (1 ≤ n ≤ 10^5, 0 ≤ k ≤ 10^9, 0 ≤ ai ≤ 10^9).. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 -->>每次只能交换相邻的两个数,每次交换,只改变这两个数的逆序,其他的数对于这两个数的逆序没有改变,所以,求出所有的逆序对,再减去k就是答案. #include <cstdio> #include <cst