HDU4911-Inversion

题意:根据题目要求交换相邻的两个元素k次,使得最后剩下的逆序对数最少

思路:如果逆序数大于0,存在0 <= i < n使得交换Ai,Ai+1后逆序数减少1,所求答案就为max(inversion - k, 0);

利用归并排序计算逆序对数。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 1000005;

int arr[MAXN], b[MAXN];
int n, k;
long long cnt;

void merge_sort(int * a, int x, int y, int *b) {
    if (y - x > 1) {
        int m = x + (y - x) / 2;
        int p = x, q = m, i = x;
        merge_sort(a, x, m, b);
        merge_sort(a, m, y, b);
        while (p < m || q < y) {
            if (q >= y || (p < m && a[p] <= a[q]))
                b[i++] = a[p++];
            else {
                b[i++] = a[q++];
                cnt += m - p;
            }
        }
        for (i = x; i < y; i++)
            a[i] = b[i];
    }
}

int main() {
    while (scanf("%d%d", &n, &k) != EOF) {
        for (int i = 0; i < n; i++)
            scanf("%d", &arr[i]);
        cnt = 0;
        merge_sort(arr, 0, n, b);
        if (cnt - k > 0)
            cnt -= k;
        else
            cnt = 0;
        cout << cnt << endl;
    }
    return 0;
}

HDU4911-Inversion,布布扣,bubuko.com

时间: 2024-10-07 07:04:03

HDU4911-Inversion的相关文章

HDU4911 Inversion 解题报告

题意:求逆序对 解题思路:1)树状数组 + 离散化 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月05日 星期二 12时05分09秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<

递归练习1

感觉自己这方面很弱,都是看着题解做的orz.. fzu2038 Another Postman Problem(递归求解) 题意:n个点n-1条边组成无向连通图,求每个点到其他所有点的路径总和的和. 题解:每条边的访问次数为边两端点数乘积的两倍.递归遍历每个点的每条边即可. 1 #include<cstdio> 2 #include<vector> 3 #include<cstring> 4 using namespace std; 5 const int N=1e5+

HDU4911: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 1≤i<j≤n a

HDU4911——归并排序——Inversion

http://acm.hdu.edu.cn/showproblem.php?pid=4911 /*裸题 有重复的不能用树状数组!!!!sort的时候会出错 */ /************************************************ * Author :Powatr * Created Time :2015-8-19 16:24:11 * File Name :A.cpp ************************************************

Minimum Inversion Number 【线段数】

Problem DescriptionThe inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of

hdu 1394 Minimum Inversion Number(这道题改日我要用线段树再做一次哟~)

Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of

HDU1394 Minimum Inversion Number 线段树+数学

Problem Description The inversion number of a given number sequence a1, a2, -, an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, -, an, if we move the first m >= 0 numbers to the end of the

HDOJ 1394 Minimum Inversion Number 求循环串的最小逆序数(暴力&amp;&amp;线段树)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14879    Accepted Submission(s): 9082 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu(1394)——Minimum Inversion Number

Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of

hdu 1394 Minimum Inversion Number 逆序数/树状数组

Minimum Inversion Number Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1394 Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai