hdu 4911 Inversion(找到的倒数)
2015-06-11 16:07
344 查看
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911
Total Submission(s): 528 Accepted Submission(s): 228
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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).
[align=left]Output[/align]
For each tests:
A single integer denotes the minimum number of inversions.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
[align=left]Author[/align]
Xiaoxu Guo (ftiasch)
[align=left]Source[/align]
2014 Multi-University Training Contest 5
解法:求的所给序列的逆序数。然后减掉k,假设小于零就去零!
代码例如以下:(归并排序法)
Inversion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 528 Accepted Submission(s): 228
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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).
[align=left]Output[/align]
For each tests:
A single integer denotes the minimum number of inversions.
[align=left]Sample Input[/align]
3 1 2 2 1 3 0 2 2 1
[align=left]Sample Output[/align]
1 2
[align=left]Author[/align]
Xiaoxu Guo (ftiasch)
[align=left]Source[/align]
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; }
相关文章推荐
- 易宝支付原理
- DEV GridControl小结。。转载的
- Contains Duplicate
- 缓存
- A Few Words about my ProRes Encoder
- JavaScript语法中的九个陷阱
- c#使用unit在mac环境下测试
- C# 获得当前目录的一些方法(转)
- Android drawable-nodpi 的作用
- 多线程之线程池任务管理通用模板
- 动画库tween.js--常用的运动算法
- [整理]Android在线源码
- Linux使用 tar命令-g参数进行增量+差异备份、还原文件
- MDT 2013 从入门到精通之客户端映像捕获
- MDT 2013 从入门到精通之客户端映像捕获
- [POJ_1068]Parencodings
- 8种移动APP导航设计模式
- 微信支付开发h5调用
- Leetcode 3 Longest Substring Without Repeating Characters
- [python]python三元表达式另类实现方式