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

因为那段冒泡排序的代码是每次选取一个最小的数,放在左边的,所以,每个数最多能到达右边的位置应该是起始位置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. 理解Promise

    一.Propmise基本用法 Promise用于发送一个异步完成的结果,是替代回调函数的另一种选择.可以把Promise理解为一种异步函数. 以下函数通过一个Promise来异步地返回一个结果 fun ...

  2. 机器学习:scikit-learn中算法的调用、封装并使用自己所写的算法

    一.scikit-learn库中的kNN算法 scikit-learn库中,所有机器学习算法都是以面向对象的形式进行包装的: 所有scikit-learn库中机器学习算法的使用过程:调用.实例化.fi ...

  3. 【转】 Pro Android学习笔记(五九):Preferences(3):EditText和Ringtone Preference

    目录(?)[-] EditText Preferences xml文件 设备的存贮文件 Ringtone Preferences EditText Preferences xml文件 在res/xml ...

  4. 使用雅虎YUI Compressor压缩JS过程心得记录

    对待发布的项目进行测试时,发现js下载量比较大,从jquery的min版想到了压缩项目中的js文件.很简单的google之(在此,强调一下google的重要性),搜到一个叫做YUI Compresso ...

  5. openssh for windows

  6. webrower + CEF

    理解WebKit和Chromium: Content API和CEF3 标签:               apiAPIAPibrowserchromeChromehtml5HTML5Html5web ...

  7. ansible 基础 简介及 安装

    ansible 运维自动化工具. 没有客户端,底层通信依赖于系统软件,linux下基于openssh,win基于powershell

  8. JavaScript语言精粹知识点总结

    1.NaN是一个数值,它表示一个不能产生正常结果的运算结果.NaN不等于任何值,包括它自己. 2.Infinity表示所有大于1.79769313486231570e+308的值,所以Infinity ...

  9. 每天一道算法题(4)——O(1)时间内删除链表节点

    1.思路 假设链表......---A--B--C--D....,要删除B.一般的做法是遍历链表并记录前驱节点,修改指针,时间为O(n).删除节点的实质为更改后驱指针指向.这里,复制C的内容至B(此时 ...

  10. groupadd添加新组

    一.groupadd命令用于将新组加入系统. 格式groupadd [-g gid] [-o]] [-r] [-f] groupname 主要参数 -g gid:指定组ID号. -o:允许组ID号,不 ...