HDU4911-Inversion(树状数组)

时间:2022-10-27 14:03:04

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 &lpar; 树状数组求逆序数 &rpar;

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

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

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

  6. &lbrack;hdu1394&rsqb;Minimum Inversion Number&lpar;树状数组&rpar;

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

  7. hdu-5497 Inversion&lpar;滑动窗口&plus;树状数组&rpar;

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

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

    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&lpar;逆序数对&rpar; : 树状数组 O&lpar;nlogn&rpar;

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

随机推荐

  1. php中抽象类与接口的概念以及区别

    php里面的接口类,抽象类到底有什么用呢? 刚接触php的时候,觉得这个东西没什么用,其实这些东西还是有一定的作用的,下面我就简单的说说. 1.php 接口类:interface 其实他们的作用很简单 ...

  2. iOS宏和&lowbar;&lowbar;attribute&lowbar;&lowbar;

    本文目录 iOS宏的经典用法 Apple的习惯 __attribute__ iOS宏的经典用法 1.常量宏.表达式宏 #define kTabBarH (49.0f) #define kScreenH ...

  3. kendo ui 富文本编辑控件 Editor 实现本地上传图片,并显示

    富文本编辑的组件有很多,大名鼎鼎的KENDO UI中自然也有,但是默认功能中,只能包含网络图片, 而如果要实现本地上传图片,KENDO UI也提供了相应的功能,但必须实现KENDO规定的多个接口, 而 ...

  4. vc&plus;&plus;创建文件目录

    #include "stdafx.h" #include <iostream> #include <fstream> #include <string ...

  5. 11th day

    今天MySQL数据库的基本知识就学完了,明天开始做小项目什么的,有点小激动啊... <?php // 定义$sql语句执行函数 function my_query($sql){ $result ...

  6. Eclipse maven hadoop -- java&period;io&period;IOException&colon; No FileSystem for scheme&colon; hdfs

    2019-01-10 概述 今天在Windows系统下新安装了Eclipse和maven的环境,想利用Maven构建一个Hadoop程序的,结果却发现程序运行时一直报 “No FileSystem f ...

  7. Java Web 禁用Cookie对Session的影响

    如果客户端禁用了Cookie,那么服务端就不能得到Session了.因为通过Session ID来确定当前会话对应的服务端Session,而Session ID通过Cookie来传递,所以禁用Cook ...

  8. 基于Github&amp&semi;Hexo的个人博客搭建过程

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  9. Ruby数组的操作

    数组的创建arr = Array.new num #创建num个元素的数组,所有数组元素为nilarr = Array.new num, elem #创建num个元素的数组,所有数组元素为elemar ...

  10. 6 ways to import data into SQL Server

    I’m going to go over some methods to import data from text files into SQL Server today. The particul ...