给定一段冒泡排序的代码,要求输出每个数字能到达的最右边的位置和最左边的位置的差

因为那段冒泡排序的代码是每次选取一个最小的数,放在左边的,所以,每个数最多能到达右边的位置应该是起始位置i+右边有多少个数比它大。

它能到达的最左的位置,可以这样考虑

1、它本来应该是排去起始位置的左边的,就是它本来是一个小的数字,起始位置在末尾这种情况的话。最左边的值就是a[i]

2、它是去右边的,那么最左边的值就是起始位置,

两种取max就去可以了

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
typedef unsigned long long int ULL;
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = +;
int a[maxn];
int c[maxn];
int n;
int dpR_min[maxn];
int ans[maxn];
int pos[maxn];
int f;
int lowbit (int x)//得到x二进制末尾0的个数的2次方 2^num
{
return x&(-x);
}
void add (int pos,int val)//在第pos位加上val这个值
{
while (pos<=n) //n是元素的个数
{
c[pos] += val; pos += lowbit(pos);
}
return ;
}
int get_sum (int pos) //求解:1--pos的总和
{
int ans = ;
while (pos)
{
ans += c[pos]; pos -= lowbit(pos);
}
return ans;
} void work ()
{
scanf("%d",&n);
for (int i=; i<=n; ++i)
{
scanf("%d",&a[i]);
pos[a[i]]=i;
}
memset(c,,sizeof c);
for (int i=n; i>=; --i)
{
dpR_min[i] = get_sum(a[i]-);
add(a[i],);
}
for (int i=;i<=n;++i)
{
int end = pos[a[i]] + dpR_min[pos[a[i]]];//看看它最右能去哪里
//如果这个数字是要去右边
int tR = end - pos[a[i]];
//去左边
int tL = end - a[i];
ans[a[i]] = max(tR,tL);
}
printf ("Case #%d:",++f);
for (int i=;i<=n;++i)
{
printf (" %d",ans[i]);
}
printf ("\n");
} int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d",&t);
while (t--) work();
return ;
}

题目要看懂。。题目看得我头疼

HDU 5775 L - Bubble Sort 树状数组的更多相关文章

  1. hdu 5775 Bubble Sort 树状数组

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

  2. 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)

    Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...

  3. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  4. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

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

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

  6. HDU 5877 Weak Pair(树状数组)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...

  7. HDU 5517 【二维树状数组///三维偏序问题】

    题目链接:[http://acm.split.hdu.edu.cn/showproblem.php?pid=5517] 题意:定义multi_set A<a , d>,B<c , d ...

  8. HDU 5862 Counting Intersections 扫描线+树状数组

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Counting Intersections Time Limit: 12000/ ...

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

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

随机推荐

  1. C#创建DataTable的语法

    namespace EazyCMS.Web.admin { public partial class ceshi1 : System.Web.UI.Page { protected void Page ...

  2. 一 ThreadLocal

    (1)  Threadlocal定义: 当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它 ...

  3. QTP连接数据库

    '注意:其中DSN=数据源名:UID=用户名:PWD=用户密码 Dim Conn Set Conn=CreateObject("ADODB.Connection") Const C ...

  4. 集合对象与自定义javabean对象接收数据库查询的数据 (基础知识扫盲)

    一.集合对象(List,Map,数组)等对象接收数据库查询的记录,如果没有一条记录,就得到的内容为空的集合,不是null: 例如:List查不到记录得到的就是size=0的list 二.自定义的jav ...

  5. 创建Ajax

    Ajax的全称是Asynchronous javascript and XML = 异步传输 + JS + XML     不需要刷新页面就可以获取新的数据 创建步骤:    (1)创建XML对象也就 ...

  6. [51nod1212]最小生成树模板

    解题关键:注意下标 解法一:prim算法 #include<bits/stdc++.h> #define maxv 1002 #define maxm 50002 #define INF ...

  7. HDFS追加文件

    配置:hdfs-site.xml <property> <name>dfs.support.append</name> <value>true</ ...

  8. 1、perl学习

    1.字符串函数 print chomp chop length uc lc index ord #转符号为ASCII的数字 chr #转数字为ASCII的字母 substr($string,offse ...

  9. Centos6.8下yum安装python2.7

    下载 ius-release.rpm包 wget https://centos6.iuscommunity.org/ius-release.rpm 安装ius-release.rpm包 rpm -Uv ...

  10. 《精通Spring4.X企业应用开发实战》读后感第六章(引用Bean的属性值)