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. Linux电源管理-Linux regulator framework概述

    前言 1.  什么是regulator?      regulator翻译为"调节器",分为voltage regulator(电压调节器)和current(电流调节器).一般电源 ...

  2. Uva 12124 Uva Live 3971 - Assemble 二分, 判断器, g++不用map.size() 难度:0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  3. Java反射《四》获取方法

    package com.study.reflect; import java.lang.reflect.InvocationTargetException; import java.lang.refl ...

  4. SignalR 前期简单配置

    一.随便你在哪个命名空间下新建一个Startup类,并在在该类中注册SignalR. using Owin; using System; using System.Collections.Generi ...

  5. Edit Distance II

    Given two strings S and T, determine if they are both one edit distance apart. Example Given s = &qu ...

  6. Cracking The Coding Interview 9.7

    //原文: // // A circus is designing a tower routine consisting of people standing atop one another's s ...

  7. transiton,transform,animation,border-image

    animation,transition,transform三者联系与区别: https://www.jianshu.com/p/0e0e1903b80d transform: 使用小技巧: tran ...

  8. OpenStack之queens版本创建负载均衡器时报错问题!

    采用kolla-ansible部署完毕后,创建负载均衡器时会提示如下的报错 解决办法: 修改网络节点的neutron-lbaas-agent容器 进入lbaas容器里 [root@openstack0 ...

  9. spoon 更新数据

    一个小需求,被要求使用spoon 来同步数据 主要流程为: 1.查询A库未同步数据 id 2.根据步骤1查到的id,作为条件更新B库数据 3.更新B库数据成功则更改库A中的数据状态为已同步. 4.添加 ...

  10. 百杂讲堂之为什么32位系统只能操作4g内存

    百杂讲堂之为什么32位系统只能操作4g内存 计算机内存中很多的单元,每一个单元就是一个字节,一个字节有8位.每一个单元有两种状态:0和1. 所以 两个单元就有4个组合: 3个单元就有8个组合: 依次类 ...