HDU 4911 Inversion(歸併排序求逆序數)
歸併排序求逆序數,然後ans-k與0取一個最大值就可以了。
也可以用樹狀陣列做,比賽的時候可能姿勢不對,樹狀陣列wa了、、
Inversion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 578 Accepted Submission(s): 249
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.
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).
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.
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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-12
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
using namespace std;
const int maxn = 10010000;
int num[maxn];
LL ans;
int tmp[maxn];
void Sell(int l, int mid, int r)
{
int i = l;
int j = mid+1;
int k = 0;
while(i <= mid && j <= r)
{
if(num[i] > num[j])
{
tmp[k++] = num[j++];
ans += mid-i+1;
}
else tmp[k++] = num[i++];
}
while(i <= mid)
tmp[k++] = num[i++];
while(j <= r)
tmp[k++] = num[j++];
for(int i = 0; i < k; i++)
num[i+l] = tmp[i];
}
void Sort(int l, int r)
{
if(l < r)
{
int mid = (l+r)/2;
Sort(l, mid);
Sort(mid+1, r);
Sell(l, mid, r);
}
}
int main()
{
int n, k;
while(cin >>n>>k)
{
ans = 0;
for(int i = 0;i < n; i++) scanf("%d",&num[i]);
Sort(0, n-1);
cout<<max(0LL, ans-k)<<endl;
}
return 0;
}
相關文章
- 歸併排序求逆序數排序
- HDU 2689 【歸併排序求逆序對】排序
- HDU 1394 Minimum Inversion Number (樹狀陣列求逆序數)陣列
- 歸併排序的經典-求逆序對排序
- 逆序對的數量(歸併排序模板)排序
- 歸併排序思想應用之----求陣列中的逆序對排序陣列
- 歸併排序-陣列中的逆序對排序陣列
- 進階指南--超快速排序(歸併+逆序對)排序
- 藍橋杯 小朋友排隊 (歸併排序 逆序數 好題)排序
- 歸併排序和基數排序排序
- HDU 3600 Simple Puzzle 歸併排序 N*N數碼問題排序
- HDU2689 Sort it (樹狀陣列求逆序數)陣列
- [排序] 歸併排序排序
- 排序(2)--選擇排序,歸併排序和基數排序排序
- 歸併排序排序
- 快速排序&&歸併排序排序
- HDU 2689 Sort it【樹狀陣列求逆序對】陣列
- 四、歸併排序 && 快速排序排序
- java歸併排序Java排序
- [java]歸併排序Java排序
- 歸併排序模板排序
- 歸併排序--二路排序排序
- 排序演算法__歸併排序排序演算法
- 排序演算法:歸併排序排序演算法
- 歸併排序--排序演算法排序演算法
- 排序演算法 - 歸併排序排序演算法
- 排序演算法——歸併排序排序演算法
- 排序演算法(歸併排序)排序演算法
- 歸併排序 js demo排序JS
- C# 歸併排序C#排序
- 【筆記】歸併排序筆記排序
- 歸併排序例項排序
- 排序演算法之 '歸併排序'排序演算法
- php實現 歸併排序,快速排序PHP排序
- 327. 區間和的個數 (歸併排序)排序
- php插入排序,快速排序,歸併排序,堆排序PHP排序
- go 實現歸併排序Go排序
- 歸併排序——C語言排序C語言