对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置。

初始位置和终点位置容易计算。最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小。

三个位置中的min即为leftpos,max即为rightpos

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int maxn=+;
int n,a[maxn],c[maxn],p[maxn],L[maxn],R[maxn]; int lowbit(int x) { return x&(-x); }
void update(int x) { while(x<=n) c[x]+=,x+=lowbit(x); }
int get(int x) {int res=; while(x) res=res+c[x],x-=lowbit(x); return res;} int main()
{
int T; scanf("%d",&T); int cas=;
while(T--)
{
scanf("%d",&n); memset(c,,sizeof c);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),L[a[i]]=min(i,a[i]),R[a[i]]=max(i,a[i]);
for(int i=;i<=n;i++)
{
int x=get(a[i]-); x=a[i]--x; update(a[i]);
L[a[i]]=min(L[a[i]],i+x), R[a[i]]=max(R[a[i]],i+x);
} printf("Case #%d:",cas++);
for(int i=;i<=n;i++) printf(" %d",abs(L[i]-R[i]));
printf("\n");
}
return ;
}

HDU 5775 Bubble Sort的更多相关文章

  1. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  2. HDU 5775 Bubble Sort (线段树)

    Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  3. hdu 5775 Bubble Sort 树状数组

    Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  4. HDU 5775 Bubble Sort(线段树)(2016 Multi-University Training Contest 4 1012)

    原址地址:http://ibupu.link/?id=31 Problem Description P is a permutation of the integers from 1 to N(ind ...

  5. 【归并排序】【逆序数】HDU 5775 Bubble Sort

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...

  6. HDU 5775:Bubble Sort(树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description   P is a permutation ...

  7. Bubble Sort (5775)

    Bubble Sort Problem Description   P is a permutation of the integers from 1 to N(index starting from ...

  8. Java中的经典算法之冒泡排序(Bubble Sort)

    Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...

  9. Bubble Sort [ASM-MIPS]

    # Program: Bubble sort # Language: MIPS Assembly (32-bit) # Arguments: 5 unordered numbers stored in ...

随机推荐

  1. winform开线程,避免页面假死

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. python 学习 [day6]

    递归阶乘: 函数自己调用自己循环操作的模式称之为递归 def func(num): if num == 1: return 1 return num * func(num - 1) print(fun ...

  3. Python学习之旅--第二周--元组、字符串、运算、字典

    一.元组 另一种有序列表叫元组:tuple.tuple和list非常类似,但是tuple一旦初始化就不能修改,比如同样是列出同学的名字: # Author:Tim Gu tuple = (" ...

  4. C# 委托的三种调用示例(同步调用 异步调用 异步回调)

    首先,通过代码定义一个委托和下面三个示例将要调用的方法: 复制代码 代码如下: public delegate int AddHandler(int a,int b);    public class ...

  5. Windows下Discuz搭建论坛过程

    搭建环境:Win7 + XAMPP5.5 + Discuz3.2 GBK 官方论坛下载安装包,解压,把upload文件夹拷贝到网站文档根目录(例如我的为:D:\IT\XAMPP5.5\htdocs\) ...

  6. unity 隐藏GameObject的方法(转)

    改position,移到视野外,推荐,最节省 gameObject.SetActive (false); //要提前引用,要不你就改不回来了... renderer.enabled = false; ...

  7. js格式转换

    //1.保留整数 function showInteger(value,row,index){ if(value!=null && value!="" && ...

  8. JQery之Ajax

    $.ajax({ url:'/comm/test1.php', type:'POST', //GET async:true, //或false,是否异步 data:{ name:'yang',age: ...

  9. 重载 vs 重写

    http://www.cnblogs.com/lonelyDog/archive/2011/11/16/2251011.html

  10. java中的日期处理

    学习Java日期处理,看见这一篇比较详细,转载之. 转自:http://www.cnblogs.com/hqr9313/archive/2012/04/19/2458221.html   时间日期1) ...