HDU 4911 Inversion (逆序数 归并排序)
Inversion
题目链接:
http://acm.hust.edu.cn/vjudge/contest/121349#problem/A
Description
bobo has a sequence a 1,a 2,…,a n. 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 a i>a j.
Input
The input consists of several tests. For each tests:
The first line contains 2 integers n,k (1≤n≤10 5,0≤k≤10 9). The second line contains n integers a 1,a 2,…,a n (0≤a i≤10 9).
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次操作,每次操作允许交换相邻元素;
求操作后能得到的整个数组的最小逆序数.
##题解:
容易得出每次交换相邻元素最多只能使得逆序数降低1.
而对于一个逆序数不为0的数组,一定存在相邻的两个数满足(i
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
LL num[maxn];
LL _count=0;
void merge_array(int left,int mid,int right)
{
int temp[right-left+1];int k=0;
int i=left;int j=mid+1;
while(i<=mid&&j<=right)
{
if(num[i]<=num[j])
temp[k++]=num[i++];
else
{temp[k++]=num[j++];_count+=mid-i+1;}
}
while(i<=mid)
temp[k++]=num[i++];
while(j<=right)
temp[k++]=num[j++];
for(i=left;i<=right;i++)
num[i]=temp[i-left];
}
void merge_sort(int left,int right)
{
if(left<right)
{
int mid=left+(right-left)/2;
merge_sort(left,mid);
merge_sort(mid+1,right);
merge_array(left,mid,right);
}
}
int main(void)
{
//IN;
int n;
LL k;
while(scanf("%d %I64d",&n,&k) != EOF)
{
for(int i=0; i<n; i++)
scanf("%I64d",&num[i]);
_count=0;
merge_sort(0, n-1);
printf("%I64d\n", max(_count-k,0LL));
}
return 0;
}
HDU 4911 Inversion (逆序数 归并排序)的更多相关文章
- hdu 4911 Inversion (分治 归并排序 求逆序数)
题目链接 题意:给n个数,求交换k次相邻的数之后的最小的逆序数对. 用分治的方法,以前在poj上做过这种题,昨天比赛的时候忘了.... 下面的归并排序还是以前的模板. #include <ios ...
- hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场
Inversion Time Limit: 20 ...
- 2014多校第五场1001 || HDU 4911 Inversion (归并求逆序数)
题目链接 题意 : 给你一个数列,可以随意交换两相邻元素,交换次数不超过k次,让你找出i < j 且ai > aj的(i,j)的对数最小是多少对. 思路 : 一开始想的很多,各种都想了,后 ...
- hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)
题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...
- HDU 4911 Inversion 树状数组求逆序数对
显然每次交换都能降低1 所以求出逆序数对数,然后-=k就好了.. . _(:зゝ∠)_ #include<stdio.h> #include<string.h> #includ ...
- hdu 4911 Inversion(找到的倒数)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 Inversion Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 4911 求逆序对数+树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=4911 给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 实际上每交换一次能且只能 ...
- hdu 4911 Inversion and poj2299 [树状数组+离散化]
题目 题意: 给你一串数字,然后给你最多进行k次交换(只能交换相邻的)问交换后的最小逆序对个数是多少. 给你一个序列,每次只能交换相邻的位置,把他交换成一个递增序列所需要的最少步数 等于 整个序列的 ...
- HDU 4911 Inversion
http://acm.hdu.edu.cn/showproblem.php?pid=4911 归并排序求逆对数. Inversion Time Limit: 2000/1000 MS (Java/ ...
随机推荐
- UserAccountInfo时间倒计时
界面如下: 代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.D ...
- BZOJ 3140 消毒(最小顶点覆盖)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3140 题意:最近在生物实验室工作的小T遇到了大麻烦. 由于实验室最近升级的缘故,他的分格 ...
- bzoj2792
首先想到二分答案是吧,设为lim 这道题难在判定,我们先不管将一个数变为0的条件 先使序列满足相邻差<=lim,这个正着扫一遍反着扫一遍即可 然后我们就要处理将一个数变为0的修改代价 当i变为0 ...
- javascript OOP编辑思想的一个实践参考
<html> <style type="text/css"> .current { background-color: red; } .dv { backg ...
- OOP——UML六种关系
UML定义的关系主要有:泛化.实现.依赖.关联.聚合.组合,这六种关系紧密程度依次加强,分别看一下 泛化 概念:泛化是一种一般与特殊.一般与具体之间关系的描述,具体描述建立在一般描述的基础之上,并对其 ...
- 【C#学习笔记】读access2007
using System; using System.Data.OleDb; namespace ConsoleApplication { class Program { static void Ma ...
- 庖丁解牛-----Live555源码彻底解密(RTP打包)
本文主要讲解live555的服务端RTP打包流程,根据MediaServer讲解RTP的打包流程,所以大家看这篇文章时,先看看下面这个链接的内容; 庖丁解牛-----Live555源码彻底解密(根据M ...
- 批量添加-fno-objc-arc
http://syxiaqj.github.io/2014/02/28/bee-learning-1/ 4.批量添加-fno-objc-arc 因为BeeFramework是一个非RAC的框架,现在X ...
- poj 1236 Network of Schools
题目描述:有一些学校连接到一个计算机网络.这些学校之间达成了一个协议:每个学校维护着一个学校列表,它向学校列表中的学校发布软件.注意,如果学校B在学校A的列表中,则A不一定在B的列表中.任务A:计算为 ...
- Banner 广告设计技巧及经验(转自UI中国)
经常听到banner这个词,但是banner是什么意思呢?引用百度知道的解释:banner可以作为网站页面的横幅广告,也可以作为游行活动时用的旗帜,还可以是报纸杂志上的大标题.Banner主要体现中心 ...