[HDOJ4911]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): 2744    Accepted Submission(s): 1015

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

  求交换k次以后所获得的数列的最小的逆序数。

  如果逆序数大于0,说明数列中有两个数可以交换,使得逆序数-1。所以所求交换k次所得数列最小逆序数的结果就是排序结束后交换次数-k,这个结果大于等于0。

代码如下:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4
 5 using namespace std;
 6
 7 typedef long long LL;
 8 const int maxn = 100010;
 9 int num[maxn];
10 int Right[maxn], Left[maxn];
11 LL ans;
12
13 void merge(int* num, int p, int q, int r) {
14     int n1, n2, i, j, k;
15     n1 = q - p + 1;
16     n2 = r - q;
17     for(i = 0; i < n1; i++) {
18         Left[i] = num[p+i];
19     }
20     for(i = 0; i < n2; i++) {
21         Right[i] = num[q+i+1];
22     }
23     Left[n1] = Right[n2] = 0x7FFFFFFF;
24     i = 0;
25     j = 0;
26     for(k = p; k <= r; k++) {
27         if(Left[i] <= Right[j]) {
28             num[k] = Left[i];
29             i++;
30         }
31         else {
32             num[k] = Right[j];
33             j++;
34             ans += (n1 - i);
35         }
36     }
37 }
38
39 void mergesort(int* num, int p, int r) {
40     int q;
41     if(p < r) {
42         q = (p + r) / 2;
43         mergesort(num, p, q);
44         mergesort(num, q+1, r);
45         merge(num, p, q, r);
46     }
47 }
48
49 int main() {
50     int n, k;
51     while(~scanf("%d %d", &n, &k)) {
52         ans = 0;
53         for(int i = 0; i < n; i++) {
54             scanf("%d", &num[i]);
55         }
56         mergesort(num, 0, n-1);
57         cout << ((ans-k) > LL(0) ? ans-k : 0) << endl;
58     }
59 }
时间: 2024-10-11 05:00:12

[HDOJ4911]Inversion的相关文章

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

CSUOJ 1555 Inversion Sequence

1555: Inversion Sequence Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 107  Solved: 34 Description For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence which are prior to j and greater to j at the same time. The seq

hdu 1394 Minimum Inversion Number

题目链接:hdu 1394 Minimum Inversion Number 该题是求最小逆序对的扩展.可以使用树状数组来实现.对于$n$个数的序列$A$,其第$i$个数($i\in [0,n)$)的逆序数$r_i$可以表示为它的角标$i$减去在它之前且不大于它的数的个数.例如对序列A = {1,3,5,9,0,8,5,7,4,2}中的数,A[8] = 4.其逆序数$r_8 = 8 - 3 = 5$,第二个3表示三个在它前面且比它小的数:{1,3,0}.从而我们可以得到第$i$个数的逆序数公式:

hdu 1394 Minimum Inversion Number(线段树)

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