A - Inversion 归并排序求逆序数
来源:互联网 发布:威海商友网络 编辑:程序博客网 时间:2024/05/21 12:42
A - Inversion
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64uDescription
bobo has a sequence a 1,a 2,…,a n. 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 a i>a j.
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 a i>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).
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.
A single integer denotes the minimum number of inversions.
Sample Input
3 12 2 13 02 2 1
Sample Output
12
#include <stdio.h>#define N 150000int a[N],tmp[N];long long ans;//void Merge(int l,int m,int r){ int i=l; int j=m+1; int k=l; while(i<=m&&j<=r) { if(a[i]>a[j]) { tmp[k++]=a[j++]; ans+=m-i+1; //这里加上相应的数 } else { tmp[k++]=a[i++]; } } while(j<=r) tmp[k++]=a[j++]; while(i<=m) tmp[k++]=a[i++]; for(int i=l;i<=r;i++) a[i]=tmp[i];}void Merge_sort(int l,int r){ if(l<r) { int m=(l+r)>>1; Merge_sort(l,m); Merge_sort(m+1,r); Merge(l,m,r); }}int main(){ int n,k; while(scanf("%d%d",&n,&k)!=EOF) { for(int i=0;i<n;i++) scanf("%d",&a[i]); ans=0; Merge_sort(0,n-1); ans-=k; if(ans<0)ans=0; printf("%I64d\n",ans); } return 0;}
0 0
- A - Inversion 归并排序求逆序数
- HDU 4911 Inversion(归并排序求逆序数)
- HDU1394-Minimum Inversion Number-归并排序求最小逆序数
- HDU 4911 Inversion【归并排序求逆序数】
- 利用归并求逆序数 Inversion
- Inversion(HDU_4911) 归并排序+逆序数对
- 归并排序,求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- ExternalBlockStore
- AC自动机模板
- processing绘制图形的基本属性
- 调用有参构造函数的三种方法
- session的基本用法--留给自己看
- A - Inversion 归并排序求逆序数
- C 语言字符数组的定义与初始化
- 配置php使其支持段标记<? ?>
- 关于ubuntu16.04拨号上网以及无线驱动安装方法
- 跑马灯
- 快速排序
- 浅谈Slick(2)- Slick101:第一个动手尝试的项目
- 多次调用定时器
- 【大学专业第一讲】工科王牌专业——计算机