hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场

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 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

题意:给出n个数,每次能够交换相邻的两个数。最多交换k次,求交换后最小的逆序数是多少。

分析:假设逆序数大于0。则存在1 ≤ i < n,使得交换ai和ai+1后逆序数减1。所以最后的答案就是max((inversion-k), 0)。

利用归并排序求出原序列的逆序对数就能够解决这个问题了。

#include<stdio.h>
#include<string.h>
#define N 100005
__int64 cnt, k;
int a[N],c[N];
//归并排序的合并操作
void merge(int a[], int first, int mid, int last, int c[])
{
    int i = first, j = mid + 1;
    int m = mid, n = last;
    int k = 0;
    while(i <= m || j <= n)
    {
        if(j > n || (i <= m && a[i] <= a[j]))
            c[k++] = a[i++];
        else
        {
            c[k++] = a[j++];
            cnt += (m - i + 1);
        }
    }
    for(i = 0; i < k; i++)
        a[first + i] = c[i];
}
//归并排序的递归分解和合并
void merge_sort(int a[], int first, int last, int c[])
{
    if(first < last)
    {
        int mid = (first + last) / 2;
        merge_sort(a, first, mid, c);
        merge_sort(a, mid+1, last, c);
        merge(a, first, mid, last, c);
    }
}
int main()
{
    int n;
    while(~scanf("%d%I64d",&n,&k))
    {
        memset(c, 0, sizeof(c));
        cnt = 0;
        for(int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        merge_sort(a, 0, n-1, c);
        if(k >= cnt) cnt = 0;
        else cnt -= k;
        printf("%I64d\n",cnt);
    }
}
时间: 2024-10-16 12:42:45

hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场的相关文章

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

Inversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1898    Accepted Submission(s): 743 Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two adjacent numbers fo

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(归并排序求逆序数)

归并排序求逆序数,然后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(求逆序数)

题目链接: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 4970 Killing Monsters(简单题) 2014多校训练第9场

Killing Monsters                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Kingdom Rush is a popular TD game, in which you should b

hdu 4923 Room and Moor(数学题)2014多校训练第6场

Room and Moor                                                                          Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of

hdu 4939 Stupid Tower Defense(DP)2014多校训练第7场

Stupid Tower Defense                                                                         Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description FSF is addicted to a stupid tower defense game. Th

hdu 4960 Another OCD Patient(dp)2014多校训练第9场

Another OCD Patient                                                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) pat

HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少是多少? 因为每次相邻的交换操作最多只能减少一个逆序对,所以最多可以减少k个逆序对,所以我们只要求出原来的序列有多少个逆序对然后减去k再跟0取较大的就可以了. 因为数据范围是10的五次方,所以暴力求肯定会TLE,所以要用n*logn算法求逆序对,n*logn算法有几种可以求逆序对的: 线段树,树状数

2014 HDU多校弟五场A题 【归并排序求逆序对】

这题是2Y,第一次WA贡献给了没有long long 的答案QAQ 题意不难理解,解题方法不难. 先用归并排序求出原串中逆序对的个数然后拿来减去k即可,如果答案小于0,则取0 学习了归并排序求逆序对的方法,可以拿来当模板 TVT 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostream&g