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 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,a2,…,a
n (0≤a
i≤10
9).output
for each tests:
a single integer denotes the minimum number of inversions.
sample input
3 12 2 1
3 02 2 1
sample output
12
author
xiaoxu guo (ftiasch)
source
2014 multi-university training contest 5
題意:給出n個數,每次可以交換相鄰的兩個數,最多交換k次,求交換後最小的逆序數是多少。
思路:如果逆序數大於0,則存在1 ≤ i < n,使得交換ai和ai+1後逆序數減1。
歸併排序時穩定的排序,能較快求出逆序數;
先貼別人寫得規範點的**:
#include#include#define n 100005
__int64 cnt, k;
int a[n],c[n];
//歸併排序的合併操作
void merge(int a, int first, int mid, int last, int c)
} for(i = 0; i < k; i++)
a[first + i] = c[i];
} //歸併排序的遞迴分解和合併
void merge_sort(int a, int first, int last, int c)
} int main()
}
自己**:
#include#include#includeusing namespace std;
const int n=100005;
__int64 a[n],c[n];
__int64 cnt;
void _merge(__int64 l,__int64 mid,__int64 r)
}for(int i = 0; i < k; i++)
a[first + i] = c[i];
}void merge_sort(__int64 l,__int64 r)
{ if(l
歸併排序hdu4911
寫乙個歸併排序的模板,歸併排序也是非常實用的一種演算法吧,而且時間複雜度為o nlgn 並且可以求出逆序數等等問題。話不多說請看 orz void merge sort int a,int x,int y,int t else for i x i 如poj2299,hud4911 就拿hdu4911...
HDU 4911 Inversion(求逆序對)
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 swap...
HDU 4911 樹狀陣列求逆序數 離散化
題意 最多可以交換k次,就最小逆序對數。思路 逆序數定理,當逆序對數大於0時,若ai要知道樹狀陣列的長度是資料範圍,由於資料比較大,所以要離散化。注意開long long。include using namespace std define mem a,b memset a,b,sizeof a d...