主题链接: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.
 
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).
 
Output
For each tests:



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
 
Author
Xiaoxu Guo (ftiasch)
 
Source

解法:求的所给序列的逆序数。然后减掉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;
}

hdu 4911 Inversion(找到的倒数)的更多相关文章

  1. HDU 4911 Inversion

    http://acm.hdu.edu.cn/showproblem.php?pid=4911   归并排序求逆对数. Inversion Time Limit: 2000/1000 MS (Java/ ...

  2. HDU 4911 Inversion (逆序数 归并排序)

    Inversion 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/A Description bobo has a sequen ...

  3. hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场

    Inversion                                                                             Time Limit: 20 ...

  4. 2014多校第五场1001 || HDU 4911 Inversion (归并求逆序数)

    题目链接 题意 : 给你一个数列,可以随意交换两相邻元素,交换次数不超过k次,让你找出i < j 且ai > aj的(i,j)的对数最小是多少对. 思路 : 一开始想的很多,各种都想了,后 ...

  5. hdu 4911 Inversion (分治 归并排序 求逆序数)

    题目链接 题意:给n个数,求交换k次相邻的数之后的最小的逆序数对. 用分治的方法,以前在poj上做过这种题,昨天比赛的时候忘了.... 下面的归并排序还是以前的模板. #include <ios ...

  6. HDU 4911 Inversion 树状数组求逆序数对

    显然每次交换都能降低1 所以求出逆序数对数,然后-=k就好了.. . _(:зゝ∠)_ #include<stdio.h> #include<string.h> #includ ...

  7. hdu 4911 Inversion and poj2299 [树状数组+离散化]

    题目 题意:  给你一串数字,然后给你最多进行k次交换(只能交换相邻的)问交换后的最小逆序对个数是多少. 给你一个序列,每次只能交换相邻的位置,把他交换成一个递增序列所需要的最少步数 等于 整个序列的 ...

  8. HDU 6098 - Inversion | 2017 Multi-University Training Contest 6

    /* HDU 6098 - Inversion [ 贪心,数论 ] | 2017 Multi-University Training Contest 6 题意: 求出所有B[i] = max(A[j] ...

  9. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

随机推荐

  1. FileStream:The process cannot access the file because it is being used by another process

    先看下面一段代码(先以共享的方式打开文件读写,然后以只读的方式打开相同文件): FileStream fs  = new FileStream(filePath, FileMode.Open, Fil ...

  2. Quartz CronTrigger运用

    CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年]  序号 说明  是否必填  同意填写的值 同意的通配符  1  秒  是  0-59    , - ...

  3. android判断是否含有某权限

    boolean has_permission = (PackageManager.PERMISSION_GRANTED == pkm.checkPermission("android.per ...

  4. DocFX

    微软开源全新的文档生成工具DocFX 微软放弃Sandcastle有些年头了,微软最近开源了全新的文档生成工具DocFX,目前支持C#和VB,类似JSDoc或Sphinx,可以从源代码中提取注释生成文 ...

  5. lua-TestMore(转)

    http://fperrad.github.io/lua-TestMore/ http://www.softpedia.com/get/Programming/Debuggers-Decompiler ...

  6. 得到View Frustum的6飞机

    笔者:i_dovelemon 资源:CSDN 日期:2014 / 9 / 30 主题:View Frustum, Plane, View Matrix, Perspective Projection ...

  7. 上curl java 模拟http请求

    最近,我的项目要求java模拟http请求,获得dns解决 tcp处理过的信息特定的连接. java api提供urlConnection apache提供的httpClient都不能胜任该需求,二次 ...

  8. DOM手术台

    CSS分类 排队: <div id="box" style="width:200px;border:1px solid red color:red;font-siz ...

  9. MSXML2;System.ServiceModel.Configuration;对应dll的添加方法

  10. 数据库 版本号是 661,打不开。此server支持 655 和更早的版本号。不支持降级路径

    "数据库 的版本号为 661,无法打开.此server支持 655 版及更低版本号. 不支持降级路径" 出现这种问题,通常是由于数据库版本号不同造成的. 我们能够用以下的语句查询数 ...