Inversion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 914    Accepted Submission(s): 380

Problem Description
bobo has a sequence a1,a2,…,an. He is allowed to swap two adjacent 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
 
题意:n个数。最多有k次相邻位置的数的交换,问最小的逆序数为多少
思路:保证每次交换逆序数都减小,能够參考冒泡排序的做法。最后仅仅要计算原数列逆序数,然后减掉k就可以。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int maxn = 100000+10;
int sum[maxn];
int n,k;
int num[maxn],tn[maxn];
map<int,int> mma;
bool cmp(int a,int b){
return a > b;
}
int lowbit(int x){
return x&(-x);
}
void add(int x,int d){
while(x < maxn){
sum[x] += d;
x += lowbit(x);
}
}
int getS(int x){
int ret = 0;
while(x > 0){
ret += sum[x];
x -= lowbit(x);
}
return ret;
}
int main(){ while(~scanf("%d%d",&n,&k)){
mma.clear();
memset(sum,0,sizeof sum);
for(int i = 1; i <= n; i++){
scanf("%d",&num[i]);
tn[i] = num[i];
}
sort(tn+1,tn+n+1,cmp);
int i = 1,cnt = 1;
while(i <= n){
mma[tn[i]] = cnt;
int j = i+1;
while(j <= n && tn[i]==tn[j]){
j++;
}
cnt++;
i = j;
}
long long ret = 0;
for(int i = 1; i <= n; i++){
ret += getS(mma[num[i]]-1);
add(mma[num[i]],1);
}
long long ans = ret-k;
if(ans < 0) ans = 0;
cout<<ans<<endl;
}
return 0;
}

HDU4911-Inversion(树状数组)的更多相关文章

  1. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

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

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

  3. hdu4911 简单树状数组

    题意:      给你一串数字,然后给你最多进行k次交换(只能交换相邻的)问交换后的最小逆序数是多少. 思路:      首先要知道的一个就是给你一个序列,每次只能交换相邻的位置,把他交换成一个递增序 ...

  4. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  5. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  6. [hdu1394]Minimum Inversion Number(树状数组)

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

  7. hdu-5497 Inversion(滑动窗口+树状数组)

    题目链接: Inversion Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  8. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

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

  9. HDU 1394 Minimum Inversion Number (树状数组)

    题目链接 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the numb ...

  10. hdu 1394 Minimum Inversion Number(逆序数对) : 树状数组 O(nlogn)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394  //hdu 题目   Problem Description The inversion number ...

随机推荐

  1. 为学Linux,我看了这些书

    为学Linux,我看了这些书   去年开始,抱着学习的态度开始了我的Linux学习,到现在,差不多一年了,收获很多,不敢说精通Linux,但是,还是对得起"略懂"这两个字的.这一年 ...

  2. html:打开新的页面

    在html页面中,打开一个新的页面,有两种方式: 一.利用超链接 <a href="newurl">新页面</a> 上面代码添加了一个新链接,点击链接时会打 ...

  3. asp.net 通过 Handler 导出数据至excel (让用户下载)

    效果图: 代码: Export2Excel.ashx <%@ WebHandler Language="C#" CodeBehind="Export2Excel.a ...

  4. api 跳转规则

    api 配置: <Context docBase="zjzc-web-api" path="/api" reloadable="false&qu ...

  5. 数学之路-python计算实战(14)-机器视觉-图像增强(直方图均衡化)

    我们来看一个灰度图像,让表示灰度出现的次数,这样图像中灰度为 的像素的出现概率是  是图像中全部的灰度数, 是图像中全部的像素数,  实际上是图像的直方图,归一化到 . 把  作为相应于  的累计概率 ...

  6. OCP-1Z0-051-题目解析-第8题

    8. View the Exhibit and examine the structure of the CUSTOMERS table. Which two tasks would require ...

  7. 在前端页面中使用@font-face来显示web自定义字体【转】

    本文转自W3CPLUS 的<CSS @font-face> @font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现, ...

  8. 三家DirectUI的商业公司

    目前正在研究DirectUI技术,分享一点心得给大家.关于DirectUI技术的介绍我在这里就不说了,可以上Google查一下,非常丰富.目前使用DirectUI技术开发的软件产品原来原丰富,比如QQ ...

  9. python编写网络抓包分析脚本

    python编写网络抓包分析脚本 写网络抓包分析脚本,一个称手的sniffer工具是必不可少的,我习惯用Ethereal,简单,易用,基于winpcap的一个开源的软件 Ethereal自带许多协议的 ...

  10. HBase概念学习(七)HBase与Mapreduce集成

    这篇文章是看了HBase权威指南之后,依据上面的解说搬下来的样例,可是略微有些不一样. HBase与mapreduce的集成无非就是mapreduce作业以HBase表作为输入,或者作为输出,也或者作 ...