HDU 5775 L - Bubble Sort 树状数组
给定一段冒泡排序的代码,要求输出每个数字能到达的最右边的位置和最左边的位置的差
因为那段冒泡排序的代码是每次选取一个最小的数,放在左边的,所以,每个数最多能到达右边的位置应该是起始位置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 树状数组的更多相关文章
- hdu 5775 Bubble Sort 树状数组
Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)
Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 5775:Bubble Sort(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation ...
- HDU 5877 Weak Pair(树状数组)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...
- HDU 5517 【二维树状数组///三维偏序问题】
题目链接:[http://acm.split.hdu.edu.cn/showproblem.php?pid=5517] 题意:定义multi_set A<a , d>,B<c , d ...
- HDU 5862 Counting Intersections 扫描线+树状数组
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Counting Intersections Time Limit: 12000/ ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
随机推荐
- paramiko使用1
- CentOS 7 配置 http 服务器
一.http单域名访问 1.安装软件: yum -y install httpd 2.启动服务:systemctl start httpd 3.设置开机启动: systemctl enable ht ...
- #425[div2]
A 签到 #include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll n,k; ci ...
- python 开发微信 自定义菜单
1.再次重复说明,下面代码只是为了简单说明接口获取方式.实际中并不推荐,尤其是业务繁重的公众号,更需要中控服务器,统一的获取accessToken. vim basic.py #name: basic ...
- mongodb教程国外
https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/
- javascript的DI
学习AngularJS的原因之一就是觉得他的DI很牛叉,为了更好的学习,在研究源码之前,想自己按照自己的思路先实现个DI,希望这个思路能够对学习源码有帮助. (function(){ var conf ...
- 24B F1 Champions
传送门 题目 Formula One championship consists of series of races called Grand Prix. After every race driv ...
- 使用form 组件写一个用户注册,并用 bootstrap渲染
需求:使用form组件,写一个用户注册系统,包含用户名, 密码, 确认密码,手机号,性别,爱好,注册.并用bootsrap渲染,成果如下: 首先创建一个django 项目.然后在连接pymysql数据 ...
- 业务、数据记录——ThreadPool.QueueUserWorkItem及Redis的实现
业务描述 当用户执行完业务操作,或者数据操作后,讲业务记录/数据追踪插入到Redis中.ThreadPool.QueueUserWorkItem定时检查队列并将上述数据插入到数据库中持久化. 实现流程 ...
- SpringBoot应用篇(一):自定义starter
一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...