HDU 4911 Inversion(求逆序對)

2021-08-07 15:42:38 字數 1371 閱讀 2576

bobo has a sequence a 

1,a 

2,…,a 

n. he is allowed to swap twoadjacentnumbers 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≤ii>a

j.input

the input consists of several tests. for each tests: 

the first line contains 2 integers n,k (1≤n≤10 

5,0≤k≤10 

9). the second line contains n integers a 

1,a 

2,…,a 

n (0≤a 

i≤10 

9).output

for each tests: 

a single integer denotes the minimum number of inversions.

sample input

3 1

2 2 1

3 02 2 1

sample output

1

2

題解:題意:

給你n個數,允許相鄰的交換k次,問你能得到的最小逆序對數

思路:一開始想用樹狀陣列來求逆序對。。後來發現1e9就放棄了。。老老實實歸併排序吧,max(逆序對數減去交換次數,0)就是答案,注意要long long

**:#include#include#include#include#include#include#include#include#include#include#include#includeusing namespace std;

#define inf 100861111

#define ll long long

#define eps 1e-15

int a[100005];

int t[100005];

ll ans;

void merge(int l,int r,int mid)

else

k++;

}while(i<=mid)

while(j<=r)

for(i=l;i<=r;i++)

a[i]=t[i];

}void guibin(int l,int r)

{ if(l

hdu 4911 Inversion 歸併排序

problem description bobo has a sequence a 1,a2,a n.he is allowed to swap twoadjacentnumbers for no more than k times.find the minimum number of invers...

HDU 4911 樹狀陣列求逆序數 離散化

題意 最多可以交換k次,就最小逆序對數。思路 逆序數定理,當逆序對數大於0時,若ai要知道樹狀陣列的長度是資料範圍,由於資料比較大,所以要離散化。注意開long long。include using namespace std define mem a,b memset a,b,sizeof a d...

hdu 1394 求逆序數

首先建立空樹,將 a i 逐個插入 計算乙個序列n排列的最小逆序數 首先用線段樹算出出事序列的逆序數,然後找規律推出排列的最小逆序數。include include include include include include include include include include inc...