Inversion

题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=52584

题意:

先输入两个数n,k,n表示这组数的个数,k表示最多可以交换的次数,再输入一组数,交换相邻的数使它是从小到大排序,交换次数不能大于k,问,你交换到最后,还会有多少个倒置数。

案例:

input

3 1

2 2 1

3 0

2 2 1

output

1

2

思路分析:

在第一个案例中,最多交换一次,我要尽可能的使它从小到大排序,那么只能交换相邻的1,2,这样得到的是1,简单来说,就是在这些数中,在它前面却比它大的数的总和减去可以交换的数k就是所得答案,这让我想到归并排序,归并排序可以求出在它前面却比它大的数的总和。这样就可以得出答案。

但是要注意一种情况,当需要交换的次数小于k呢?在这种情况输出0就行。

注意:数的范围过大,要用long long型。

源代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #define MAX 100005
 4 using namespace std;
 5 int n;
 6 long long a[MAX],t[MAX],s,k;
 7 void merge_sort(long long *A,long long x,long long y,long long *T)
 8 {
 9     if(y-x>1)
10     {
11         long long m=x+(y-x)/2;
12         long long p=x,q=m,i=x;
13         merge_sort(A,x,m,T);
14         merge_sort(A,m,y,T);
15         while(p<m||q<y)
16         {
17             if(q>=y||(p<m&&A[p]<=A[q])) T[i++]=A[p++];
18             else {T[i++]=A[q++];s+=m-p;}     //需要交换次数
19         }
20         for(i=x;i<y;i++)A[i]=T[i];
21     }
22 }
23 int main()
24 {
25     while(scanf("%d%d",&n,&k)!=EOF)
26     {
27         s=0;
28         for(int i=0;i<n;i++)
29             scanf("%d",&a[i]);
30         merge_sort(a,0,n,t);
31         if(s>=k)
32             cout<<s-k<<endl;
33         else
34             cout<<"0"<<endl;
35     }
36     return 0;
37 }
时间: 2024-10-07 13:34:20

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

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