HDU 4911 Inversion【归并排序求逆序数】
来源:互联网 发布:java算法技术手册 编辑:程序博客网 时间:2024/06/05 08:49
Inversion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2725 Accepted Submission(s): 1008
Problem Description
bobo has a sequence a1,a2,…,an. He is allowed to swap twoadjacent 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 12 2 13 02 2 1
Sample Output
12
嗯,题目大意就是说,n和k,n代表数据个数,k代表数据相邻之间交换的次数限制。还是归并排序找到排到从小到大的最小次数然后减去k 就是最小的inversions(因为归并排序是稳定的,而sort不是稳定的)(这里我实验证明用malloc多次申请内存再释放是很费时间的,在主函数里申请一次就ok这里TLE了我几次,然后还是比直接申请一个数组要节省一点点时间的,节省了大概80ms)
#include <iostream>#include<cstdlib>#include<cstdio>#include<cstring>#define maxn 100000+10using namespace std;long long n,cnt,k,a[maxn],p[maxn];void Merge(long long left,long long mid,long long right,long long *p){ long long n=mid,m=right; long long i=left,j=mid+1; long long num=0; while(i<=n&&j<=m) { if(a[i]<=a[j]) p[num++]=a[i++]; else { p[num++]=a[j++]; cnt+=n-i+1; } } while(i<=n) p[num++]=a[i++]; while(j<=m) p[num++]=a[j++]; for(int i=0;i<num;++i) a[left+i]=p[i];}void mergesort(long long left,long long right,long long *p){ if(left<right) { long long mid=(left+right)/2; mergesort(left,mid,p); mergesort(mid+1,right,p); Merge(left,mid,right,p); }}int main(){ while(cin>>n>>k) { for(int i=0;i<n;++i) cin>>a[i]; cnt=0; long long *p=(long long *)malloc(sizeof(long long)*n); mergesort(0,n-1,p); if(cnt<=k) cout<<0<<endl; else cout<<cnt-k<<endl; free(p); } return 0;}
0 0
- HDU 4911 Inversion(归并排序求逆序数)
- HDU 4911 Inversion【归并排序求逆序数】
- A - Inversion 归并排序求逆序数
- HDU 4911 Inversion (归并排序求逆序对)
- Inversion (hdu 4911 树状数组 || 归并排序 求逆序对)
- HDU 4911 Inversion(归并排序求逆序对)
- hdu 4911 归并排序求逆序数
- hdu 4911 归并排序求逆序数
- HDU 4911 Inversion(归并求逆序对)
- hdu 1394 Minimum Inversion Number 求逆序数(树状数组/归并排序/暴力)
- HDU1394-Minimum Inversion Number-归并排序求最小逆序数
- hdu 1394 Minimum Inversion Number 归并求逆序数
- hdu 4911 Inversion(求逆序数)
- HDU 4911 Inversion 求逆序数对
- 归并排序求逆序数——HDU 4911
- hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场
- [hdu]1394 Minimum Inversion Number -- 暴力求逆序、树状数组求逆序、线段树求逆序、归并排序求逆序
- 利用归并求逆序数 Inversion
- 一次对MKMapView的性能优化
- 我的一些网页设计
- 一步一图一代码,一定要让你真正彻底明白红黑树
- Android工程的编译过程
- CSS的position
- HDU 4911 Inversion【归并排序求逆序数】
- nyoj--38(布线问题)
- codeforces 374D
- ucos-ii中os_cpu_a.asm函数解释
- 实战体验几种MySQL Cluster方案
- caffe layer
- Maven3.0.4基础原理分析
- Introduce to algorithm--------pseudo code to C/C++ code (chapter 16)
- gdb tips 总结