题目链接

题意:给n个数,求交换k次相邻的数之后的最小的逆序数对。

用分治的方法,以前在poj上做过这种题,昨天比赛的时候忘了。。。。

下面的归并排序还是以前的模板。

 #include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL __int64
const int maxn = +;
using namespace std;
int n;
LL sum, a[maxn], b[maxn]; void merge_sort(LL *A, int x, int y, LL *T)
{
if(y-x>)
{
int m=x+(y-x)/;
int p=x, q=m, i=x;
merge_sort(A,x,m,T);
merge_sort(A,m,y,T);
while(p<m || q<y)
{
if(q>=y ||(p<m && A[p] <= A[q]))
T[i++] = A[p++];
else
{
T[i++] = A[q++];
sum+=m-p;
} }
for(i=x; i<y; i++)
A[i] = T[i];
}
};
int main()
{
int k, i;
while(~scanf("%d%d", &n, &k))
{
sum = ;
memset(b, , sizeof(b));
for(i = ; i < n; i++)
scanf("%I64d", &a[i]);
merge_sort(a, , n, b);
if(sum-k<) sum = k;
printf("%I64d\n", sum-k);
}
return ;
}

hdu 4911 Inversion (分治 归并排序 求逆序数)的更多相关文章

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

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

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

    Inversion                                                                             Time Limit: 20 ...

  3. HDU 3743 Frosh Week(归并排序求逆序数)

    归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...

  4. poj 2299 Ultra-QuickSort 归并排序求逆序数对

    题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...

  5. poj 2299 Ultra-QuickSort :归并排序求逆序数

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34676   Accepted ...

  6. [CF 351B]Jeff and Furik[归并排序求逆序数]

    题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...

  7. POJ2299 Ultra-QuickSort(归并排序求逆序数)

    归并排序求逆序数   Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  8. hiho一下 第三十九周 归并排序求逆序数

    题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...

  9. POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39279   Accepted: 14163 ...

随机推荐

  1. google api , the problem of null refresh token

    http://stackoverflow.com/questions/10827920/google-oauth-refresh-token-is-not-being-received The ref ...

  2. openstack安装、卸载与启动

    一.安装: 更新: sudo apt-get update sudo apt-get upgrade 安装图形化界面: sudo apt-get install ubuntu-desktop 安装gc ...

  3. sharepoint 列表的column验证----------SharePoint 2010 List Validation Formula

    首先,依次打开-站点->列表名称->列表设置->验证设置: 我们设置一个时间的列不能小于当前时间,并且在编辑的时候不需要验证. =OR([,Created<TODAY())

  4. Zabbix实现告警分级

    Zabbix中trigger的severity的值定义了trigger的不同严重程度,其中severity默认的6个值为 Not classified, Information, Warning, A ...

  5. 一个有趣的 SQL 查询(查询7天连续登陆)

    一个有趣的 SQL 查询 一个朋友有这样一个SQL查询需求: 有一个登录表(tmp_test),包含用户ID(uid)和登录时间(login_time).表结构如下: . row ********** ...

  6. 导入 github 步骤

    https://github.com/dotnet/corefx       如果出现未能找到解决方案的情况,则找项目文件打开,如:  

  7. NData BUG 记录

    一.collection 如果设计如下页面 页面模型如下 using UnityEngine; using System.Collections; using System.Collections.G ...

  8. yum源万能

    sed -i ‘s|^#baseurl|baseurl| ; s|^mirrorlist|#mirrorlist|’ /etc/yum.repos.d/*

  9. mysql 常用操作

    添加用户并设置权限: grant all on *.* to root@‘%’ identified by ‘123456’with grant option; all:所有权限 select,ins ...

  10. C#的cs文件中Response.Clear();Response.ClearHeaders()的作用

    在学习一个CS文件,如下:public partial class GetPic : System.Web.UI.Page{    protected void Page_Load(object se ...