HDU 5775 Bubble Sort (线段树)
Bubble Sort
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5775
Description
P is a permutation of the integers from 1 to N(index starting from 1).
Here is the code of Bubble Sort in C++.
for(int i=1;i<=N;++i)
for(int j=N,t;j>i;—j)
if(P[j-1] > P[j])
t=P[j],P[j]=P[j-1],P[j-1]=t;
After the sort, the array is in increasing order. ?? wants to know the absolute values of difference of rightmost place and leftmost place for every number it reached.
Input
The first line of the input gives the number of test cases T; T test cases follow.
Each consists of one line with one integer N, followed by another line with a permutation of the integers from 1 to N, inclusive.
limits
T <= 20
1 <= N <= 100000
N is larger than 10000 in only one case.
Output
For each test case output “Case #x: y1 y2 … yN” (without quotes), where x is the test case number (starting from 1), and yi is the difference of rightmost place and leftmost place of number i.
Sample Input
2
3
3 1 2
3
1 2 3
Sample Output
Case #1: 1 1 2
Case #2: 0 0 0
Hint
In first case, (3, 1, 2) -> (3, 1, 2) -> (1, 3, 2) -> (1, 2, 3)
the leftmost place and rightmost place of 1 is 1 and 2, 2 is 2 and 3, 3 is 1 and 3
In second case, the array has already in increasing order. So the answer of every number is 0.
Source
2016 Multi-University Training Contest 4
##题意:
对于N的一个全排列做一遍冒泡排序,求每个元素所到达的最右端和最左端的差.
##题解:
一开始想当然以为每个元素在冒泡的过程中只会往单一方向移动,所以原始位置和最终位置之差即为所求.
不过很快看到大部分队伍都挂掉了, 意识到上述算法有问题.
对于数据:1 5 3 4 2
按以上思路:0 3 0 0 3
而实际模拟一遍:0 3 1 1 3
考虑元素i,它的右边有多少个比i小的元素,就会右移多少次; 左边有多少个比i大的元素,就会左移多少次.
所以分别记录每个元素往两边的逆序数,即为左移和右移的次数.
又根据冒泡的过程,对于i,一定要把i右边比i小的数移到左边后,才会考虑i的左移:这说明所有的右移操作都先于左移操作.
综上可以求得每个元素达到的最右端和最左端.
记录逆序数这里用线段树实现,也可用更简洁的树状数组.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL int
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n;
struct Tree
{
int left,right;
LL sum; /sum为区间和,可改为最大值最小值等/
}tree[maxn<<2]; /四倍大小/
/递归建树/
void build(int i,int left,int right)
{
tree[i].left=left;
tree[i].right=right;
if(left==right){
tree[i].sum=0;
return ;
}
int mid=mid(left,right);
build(i<<1,left,mid);
build(i<<1|1,mid+1,right);
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
}
/单点修改,d为改变量,两函数可共存/
void update(int i,int x,LL d)
{
if(tree[i].left==tree[i].right){
tree[i].sum = d;
return;
}
int mid=mid(tree[i].left,tree[i].right);
if(x<=mid) update(i<<1,x,d);
else update(i<<1|1,x,d);
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
}
/区间结果查询/
LL query(int i,int left,int right)
{
if(tree[i].leftleft&&tree[i].rightright)
return tree[i].sum;
int mid=mid(tree[i].left,tree[i].right);
if(right<=mid) return query(i<<1,left,right);
else if(left>mid) return query(i<<1|1,left,right);
else return query(i<<1,left,mid)+query(i<<1|1,mid+1,right);
}
int pos[maxn];
int num[maxn];
int _left[maxn];
int _right[maxn];
int main(int argc, char const *argv[])
{
//IN;
int t; cin >> t; int ca = 1;
while(t--)
{
cin >> n;
for(int i=1; i<=n; i++) {
int x; scanf("%d", &x);
num[i] = x;
pos[x] = i;
}
build(1,1,n);
for(int i=1; i<=n; i++) {
int x = num[i];
_left[x] = 0;
_left[x] = query(1, x, n);
update(1, x, 1);
}
build(1,1,n);
for(int i=n; i>=1; i--) {
int x = num[i];
_right[x] = 0;
_right[x] = query(1, 1, x);
update(1, x, 1);
}
printf("Case #%d: ", ca++);
for(int i=1; i<=n; i++) {
int ma = max(pos[i], pos[i]+_right[i]);
int mi = min(pos[i], pos[i]+_right[i]-_left[i]);
printf("%d%c", abs(ma-mi), i==n?'\n':' ');
}
}
return 0;
}
HDU 5775 Bubble Sort (线段树)的更多相关文章
- 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 ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 5775 Bubble Sort 树状数组
Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- 多校hdu-5775 Bubble sort(线段树)
题意根据题目中给的冒泡排序写出每个元素交换过程中该元素位置左右最大差距: 分析:因为题目中冒泡程序从后向前遍历的,假设第i个元素左边有k个比i小的数,那么i必定会向右移动k位,我们用k1记住i+k,用 ...
- 【归并排序】【逆序数】HDU 5775 Bubble Sort
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...
- HDU 5775 Bubble Sort
对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置. 初始位置和终点位置容易计算.最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小. 三个位置中的min即为leftpos, ...
- HDU 5775:Bubble Sort(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation ...
- hdu 5700区间交(线段树)
区间交 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
随机推荐
- 【POJ】1084 Square Destroyer
1. 题目描述由$n \times n, n \in [1, 5]$的正方形由$2 \times n \times (n+1)$根木棍组成,可能已经有些木棍被破坏,求至少还需破坏多少木根,可以使得不存 ...
- 演练:Office 编程(C# 和 Visual Basic)
https://msdn.microsoft.com/zh-cn/library/ee342218(v=vs.110).aspx PIA的全称是 primary interop assembly 主 ...
- poj 1151 Atlantis (离散化 + 扫描线 + 线段树 矩形面积并)
题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 映射到y轴,并且记录下每个的y坐标,并对y坐标进行离散. 然后按照x从左向右扫描. #include <io ...
- Android加速度传感器实现“摇一摇”,带手机振动
由于代码有点多,所以就分开写了,注释还算详细,方便学习 Activity package com.lmw.android.test; import android.app.Activity; im ...
- Asp.Net微信登录-电脑版扫描二维码登录
像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 一.创建网站应用 在微信开放平台创建一个网站应用 https:// ...
- Android ArrayAdapter 详解
本文主要讲解ArrayAdapter的创建方法,我把ArrayAdapter分为三种:简单的.样式丰富的但内容简单的.内容丰富的. 默认的,ArrayAdapter期望接受的样式文件里只含有一个tex ...
- 省常中模拟 Test1 Day1
临洮巨人 排序 题意:在字符串中找出 A.B.C 三个字母出现次数相同的区间个数. 初步的解法是前缀和,用 a(i), b(i), c(i) 表示在位置 i 之前(包括 i)各有 字母 A.B.C 多 ...
- liunx上运行mybase
mybase 是个人知识管理软件,国内用他的人很多,尤其是程序员.我也是mybase的忠实使用者,有大量的积累. 以前一直用Windows,mybase使用完全没有问题,后来转投ubuntu阵营 ...
- 【DFS,双向】NYOJ-20-吝啬的国度
[题目链接:NYOJ-20] 很巧妙,要好好想想 #include <iostream> #include <stdio.h> #include <vector> ...
- 【转载】两个Web.config中连接字符串中特殊字符解决方案
userid = test password = aps'"; 那么连接字符串的写法为: Provider=SQLOLEDB.1;Password="aps'"&quo ...