http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4046

题意:有一个含有n个元素的数列p,每个元素均不同且为1~n中的一个,求出将数列变为循环递增序列至少需要左右相邻的数交换多少次

题目分析:先看简化版的题目:如果只有1 2 3 4 5是符合要求的,那么交换次数根据冒泡排序可得就是逆序数,直接树状数组求逆序数即可.

由于题目要求只要只要是循环递增数列即可,也就是1 2 3 4 5 和 2 3 4 5 1都是符合要求的.那么就要枚举数字1出现的位置了,如果暴力求解显然会TLE,而可以发现1 2 3 4 5 和2 3 4 5 1所需交换次数的差就是(n - pos[ 1 ] )- (pos[ 1 ] - 1 )(其中pos[ i ]表示数字i的起始位置),因为不管1起始位置在哪,得到1 2 3 4 5的时候都可以先把1移动到第一个位置,然后移动2 3 4 5的相对位置,得到2 3 4 5 1的时候都可以先把1移动到最后一个位置,然后移动2 3 4 5的相对位置,所以移动成1 2 3 4 5与移动成2 3 4 5 1的次数之差就是 (n - pos[ 1 ] )- (pos[ 1 ] - 1 ).正是因为可以分别把数字移动到首位置和末位置,才可以直接根据(n - pos[ i ] )- (pos[ i ] - 1 )计算差值,所以需要分别计算1 2 3 4 5    2 3 4 5 1     3 4 5 1 2       4 5 1 2 3       5 1 2 3 4

 #include <iostream>
#include <iomanip>
#include <algorithm>
#include <map>
# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5+;
int n, id[maxn], sum[maxn];
void add(int x){
for(;x<=n;x+=x&-x) ++sum[x];
}
int query(int x){
int res = ;
for(;x>;x-=x&-x) res += sum[x];
return res;
} int main(){
int T, x;
for(scanf("%d",&T);T;--T){
scanf("%d",&n);
for(int i=; i<=n; ++i){
scanf("%d",&x);
id[x] = i;
sum[i] = ;
}
LL tot = , ans;
for(int i=n; i>; --i){
tot += query(id[i]);
add(id[i]);
}
ans = tot;
for(int i=; i<=n;++i){
tot += n-id[i];
tot -= id[i]-;
ans = min(ans, tot);
}
printf("%lld\n",ans);
}
return ;
}

[zoj4046][树状数组求逆序(强化版)]的更多相关文章

  1. Ultra-QuickSort(树状数组求逆序对数)

    Ultra-QuickSort 题目链接:http://poj.org/problem?id=2299 Time Limit: 7000MS   Memory Limit: 65536K Total ...

  2. 用树状数组求逆序对数(poj2299)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 46995   Accepted: 17168 ...

  3. 【CDOJ931】Car race game(树状数组求逆序)

    题目连接:http://acm.uestc.edu.cn/#/problem/show/931 OJ评判系统有些坑,不支持__int64以及输出的%I64d大家注意.全开long long也会TLE, ...

  4. POJ-2299 Ultra-QuickSort(用树状数组求逆序对数)

    题目链接 ac代码 #include<iostream> #include<cstdio> #include<cstring> #include<algori ...

  5. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

  6. POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)

    树状数组求逆序对   转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...

  7. 【bzoj2789】[Poi2012]Letters 树状数组求逆序对

    题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...

  8. 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)

    2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...

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

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

随机推荐

  1. lodash 学习资料

    lodash.js 是什么不多说,工作时间长了就基本绕不过去他,工作项目中也很好的弥补angular ,jquery 的不足,由中文bootstrap 退出的中文版学习资料 http://lodash ...

  2. 高效方便的IO库: System.IO.Pipelines

    我们在编写网络程序的时候,经常会进行如下操作: 申请一个缓冲区 从数据源中读入数据至缓冲区 解析缓冲区的数据 重复第2步 表面上看来这是一个很常规而简单的操作,但实际使用过程中往往存在如下痛点: 数据 ...

  3. [HDU4585]Shaolin

    Problem 问你一个数的前驱和后继 Solution Treap模板题 Notice 注意输出那个人的编号 Code #include<cmath> #include<cstdi ...

  4. OOP⑹

    1.抽象类 所有由abstract关键字修饰的方法我们称之为 抽象方法! 抽象方法只能存在于 抽象类中! 所有由abstract关键字修饰的类我们称之为 抽象类! 抽象类的特点: 01.由abstra ...

  5. angular4-注入服务

    //配置已创建的服务:import { MemberService } from "./member.service";@NgModule({ // ... providers: ...

  6. LeetCode难度与出现频率

    转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two ...

  7. JSON转化

    相关链接 : http://blog.csdn.net/gchb9527/article/details/8688279 --------------------------------------- ...

  8. java中String的equals()和 ==

    String a=new String("java"); String b=new String("java"); System.out.println(a.e ...

  9. 安装连接mysql8时候遇到的问题以及解决(转)

    官网下载mysql8的安装包: https://dev.mysql.com/downloads/ 下一步安装即可. mysql8增加了传说中的安全性校验 遇到的几个问题: 1.natcat连接不上.参 ...

  10. 用户访一个APP或者网页流程示意图

    用户访问示意图: