首页 技术 正文
技术 2022年11月18日
0 收藏 613 点赞 3,009 浏览 1194 个字

题目

题意:  给你一串数字,然后给你最多进行k次交换(只能交换相邻的)问交换后的最小逆序对个数是多少。

给你一个序列,每次只能交换相邻的位置,把他交换成一个递增序列所需要的最少步数 等于 整个序列的逆序对数。

对于这个题目,我们只要求出个逆序对个数,然后输出逆序数 – k就行了,如果是负数输出0。

之前做的这道题也是和逆序对有关,但是通过这道的代码改编一下,总是Runtime Error (ACCESS_VIOLATION),原因在于这里

The first line contains 2 integers n,k (1≤n≤10^5,0≤k≤10^9). The second line contains n integers a1,a2,…,an (0≤ai≤10^9).

数据范围10^9,所需空间太大,需要离散化

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int Max = 1e5+10;
int n;
LL c[Max];
LL Lowbit(int x)
{
return x&(-x);
}
void update(int i,int x)
{
while(i<=n){
c[i]+=x;
i+=Lowbit(i);
}
}
LL getsum(int i)
{
LL sum = 0;
while(i>0)
{
sum+=c[i];
i-= Lowbit(i);
}
return sum;
}
int main()
{
int k;
int a[Max],tmp[Max];
while(cin>>n>>k)
{
for(int i=1;i<=n;i++){
cin>>a[i];
tmp[i]=a[i];
}
//离散化
sort(tmp+1,tmp+1+n);
int tol = unique(tmp+1,tmp+n+1)-tmp-1;
for(int i=1;i<=n;i++)
a[i]=lower_bound(tmp+1,tmp+tol+1,a[i])-tmp; LL sum = 0;
/*下面几行代码也可以写成这样
for(int i=1;i<=n;i++)
{
update(a[i],1);
sum+=i-getsum(a[i]);
}
*/
for(int i=n;i>0;i--)
{
sum=sum+getsum(a[i]-1);
//getsum()是对 [1,a[i] ]求和,而这里我要求的是比a[i]小的,不包括a[i],即a[i]-1
update(a[i],1);
}
sum -= k;
if(sum<0) sum = 0;
cout<<sum<<endl; memset(c,0,sizeof(c));
memset(a,0,sizeof(a));
memset(tmp,0,sizeof(tmp));
}
return 0;
}

这道题(poj2299)也是一样的,代码修改一丢丢就过了。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,026
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,517
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,364
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,145
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,779
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,856