hdu 4911 Inversion(求逆序数)
来源:互联网 发布:境外网络电视直播.apk 编辑:程序博客网 时间:2024/06/04 04:54
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911
Inversion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 528 Accepted Submission(s): 228
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
Author
Xiaoxu Guo (ftiasch)
Source
2014 Multi-University Training Contest 5
解法:求的所给序列的逆序数,然后减掉k,如果小于零就去零!
代码如下:(归并排序法)
#include<stdio.h>int is1[112345],is2[112345];// is1为原数组,is2为临时数组,n为个人定义的长度__int64 merge(int low,int mid,int high){int i=low,j=mid+1,k=low;__int64 count=0;while(i<=mid&&j<=high)if(is1[i]<=is1[j])// 此处为稳定排序的关键,不能用小于is2[k++]=is1[i++];else{is2[k++]=is1[j++];count+=j-k;// 每当后段的数组元素提前时,记录提前的距离}while(i<=mid)is2[k++]=is1[i++];while(j<=high)is2[k++]=is1[j++];for(i=low;i<=high;i++)// 写回原数组is1[i]=is2[i];return count;}__int64 mergeSort(int a,int b)// 下标,例如数组int is[5],全部排序的调用为mergeSort(0,4){if(a<b){int mid=(a+b)/2;__int64 count=0;count+=mergeSort(a,mid);count+=mergeSort(mid+1,b);count+=merge(a,mid,b);return count;}return 0;}int main(){int n, x;__int64 k;__int64 sum;while(scanf("%d%I64d",&n,&k)!=EOF){for(int i=0;i<n;i++){scanf("%d",&x);is1[i] = x;}__int64 ans=mergeSort(0,n-1);sum=0;printf("%I64d\n",ans-k>0?ans-k:0); }return 0;}
1 2
- hdu 4911 Inversion(求逆序数)
- HDU 4911 Inversion 求逆序数对
- HDU 4911 Inversion(求逆序对)
- HDU 4911 Inversion 树状数组求逆序数对
- hdu 4911 Inversion 树状数组求逆序数对
- hdu 4911 Inversion 多校第五场 求逆序数
- HDU 4911 Inversion(归并排序求逆序数)
- HDU 4911 Inversion【归并排序求逆序数】
- HDU 1394 Minimum Inversion Number (求逆序数)
- HDU 1394 Minimum Inversion Number(求最小逆序数)
- hdu 4911 Inversion (多校第5场,求逆序数对,离散化)
- HDU 4911 Inversion (归并排序求逆序对)
- HDU 4911 Inversion(归并排序求逆序对)
- HDU 4911 Inversion(归并求逆序对)
- HDU 4911 Inversion 解题报告(逆序数)
- hdu 4911 Inversion (逆序数变换)
- HDU 1394 Minimum Inversion Number 线段树求逆序数
- hdu 1394 Minimum Inversion Number 线段树求逆序数
- java解析xml--DOM模型
- HDU 1166 敌兵布阵 线段树
- hdu 4539 郑厂长系列故事——排兵布阵 状态压缩+dp;
- Android 软件测试日志文件抓取
- windows server 2003配置CA服务器通过SCEP颁发证书
- hdu 4911 Inversion(求逆序数)
- 连接池的优势
- adapter中加点击事件的一种写法
- u盘自动挂载-udev (转载加修改)
- 寻找最小的k个数
- 心态
- 计算机使用的一些技巧
- SSH 和 EasyUI结合的 combobox默认值问题解决方法
- java生成PFX证书