题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911

解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少是多少?

因为每次相邻的交换操作最多只能减少一个逆序对,所以最多可以减少k个逆序对,所以我们只要求出原来的序列有多少个逆序对然后减去k再跟0取较大的就可以了。

因为数据范围是10的五次方,所以暴力求肯定会TLE,所以要用n*logn算法求逆序对,n*logn算法有几种可以求逆序对的:

线段树,树状数组,归并排序。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef __int64 INT;
const int maxn = +; struct node
{
int d,cixu;
}A[maxn];
struct Node
{
int l,r;
INT n;
}tree[*maxn];
bool cmp1(node a,node b)
{
if(a.d == b.d) return a.cixu < b.cixu;
return a.d < b.d;
}
bool cmp2(node a,node b)
{
return a.cixu < b.cixu;
}
void build(int p)
{
if(tree[p].l == tree[p].r) return ;
int mid = (tree[p].l + tree[p].r) / ;
tree[*p].l = tree[p].l;
tree[*p].r = mid;
tree[*p].n = tree[*p+].n = ;
tree[*p+].l = mid + ;
tree[*p+].r = tree[p].r;
build(*p);
build(*p+);
}
void insert(int p,int l)
{
tree[p].n++;
if(tree[p].l == tree[p].r) return ;
int mid = (tree[p].l + tree[p].r) / ;
if(l <= mid) insert(*p,l);
else insert(*p+,l);
}
INT query(int p,int l,int r)
{
if(l > r) return ;
if(tree[p].l == l && tree[p].r == r)
return tree[p].n;
int mid = (tree[p].l + tree[p].r) / ;
if(r <= mid) query(*p,l,r);
else if(l <= mid && r > mid)
return query(*p,l,mid) + query(*p+,mid+,r);
else return query(*p+,l,r);
} int main()
{
int n;
INT k;
while(scanf("%d%I64d",&n,&k)!=EOF)
{
tree[].l = ;
tree[].r = n;
tree[].n = ;
build();
for(int i = ;i <= n;++i)
{
scanf("%d",&A[i].d);
A[i].cixu = i;
}
sort(A+,A+n+,cmp1);
int f = ,hehe = 0x7fffffff;
for(int i = ;i <= n;++i)
{
if(A[i].d != hehe)
{
hehe = A[i].d;
f++;
}
A[i].d = f;
}
sort(A+,A+n+,cmp2);
INT ans = ;
for(int i = ;i <= n;++i)
{
ans += query(,A[i].d+,n);
insert(,A[i].d);
}
ans = ans-k > ? (ans-k) : ;
printf("%I64d\n",ans);
}
return ;
}

HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)的更多相关文章

  1. HDU 1394 Minimum Inversion Number(线段树求逆序对)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1394 解题报告:给出一个序列,求出这个序列的逆序数,然后依次将第一个数移动到最后一位,求在这个过程中 ...

  2. HDU 1394 线段树求逆序对

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  3. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  4. HDU - 1394 Minimum Inversion Number (线段树求逆序数)

    Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs ( ...

  5. hdu 1394 (线段树求逆序数)

    <题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...

  6. hdu 1394 Minimum Inversion Number 【线段树求逆序数】

    之前写过树状数组的,再用线段树写一下--- #include<cstdio> #include<cstring> #include<iostream> #inclu ...

  7. hdu 6318 Swaps and Inversions (线段树求逆序对数)

    Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. HDU - 1394 Minimum Inversion Number(线段树求逆序数---点修改)

    题意:给定一个序列,求分别将前m个数移到序列最后所得到的序列中,最小的逆序数. 分析:m范围为1~n,可得n个序列,求n个序列中最小的逆序数. 1.将序列从头到尾扫一遍,用query求每个数字之前有多 ...

  9. hdu 4911 求逆序对数+树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=4911 给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 实际上每交换一次能且只能 ...

随机推荐

  1. 编写高质量代码--改善python程序的建议(六)

    原文发表在我的博客主页,转载请注明出处! 建议二十八:区别对待可变对象和不可变对象 python中一切皆对象,每一个对象都有一个唯一的标识符(id()).类型(type())以及值,对象根据其值能否修 ...

  2. Node基础:资源压缩之zlib

    概览 做过web性能优化的同学,对性能优化大杀器gzip应该不陌生.浏览器向服务器发起资源请求,比如下载一个js文件,服务器先对资源进行压缩,再返回给浏览器,以此节省流量,加快访问速度. 浏览器通过H ...

  3. 分页pagination实现及其应用

    1.分页jquery.page.js //分页插件 /** 2014-08-05 ch **/ (function ($) { var ms = { init: function (obj, args ...

  4. blur效果,模糊效果css

    .blur { -webkit-filter: blur(5px);   -moz-filter: blur(5px);   -o-filter: blur(5px);   -ms-filter: b ...

  5. [BZOJ 3143][HNOI2013]游走(数学期望)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3143 分析: 易得如果知道了每条边经过的数学期望,那就可以贪心着按每条边的期望的大小赋 ...

  6. PHP乱码问题,UTF-8(乱码)

    一.HTML页面转UTF-8编码问题 1.在head后,title前加入一行: <meta http-equiv='Content-Type' content='text/html; chars ...

  7. nginx root && alias 文件路径配置

    文章摘自:http://www.ttlsa.com/nginx/nginx-root_alias-file-path-configuration/ nginx指定文件路径有两种方式root和alias ...

  8. nslog

    今天有人问我怎么更好的使用nslog,打包的时候老注释 pch里加下面的代码就好了平时debug的时候打印,release后就不打印了 #ifdef DEBUG#define NSLog(...) N ...

  9. 用php生成数据字典

    <?php header("Content-type: text/html; charset=utf-8"); $dbserver = "localhost&quo ...

  10. Fence 设备

    RHCS中必须有Fence设备,在设备为知故障发生时,Fence负责让占有浮动资源的设备与集群断开. REDHAT的fence device有两种, 内部fence设备: IBM RSAII卡,HP的 ...