HDOJ 5775 (2016多校联合训练 Training Contest 4) Bubble Sort
2016-07-30 13:26
417 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5775
![](http://img.blog.csdn.net/20160730131529172)
题意:让我们求出每一个数在冒泡排序中的排在最左边的位置和最右边的位置的差。
冒泡排序,仔细思考一下其中的原理,每一次我们都是从右边开始在前面前后交换,也就是说每一个数如果左边有几个数比它小,那么在它开始移动的时候,就要向右边移动多少次,所以能够排在最右边的位置就是它的当前位置+右边比它小的数的个数,而能够排在最左边的位置显然就是它的当前位置或者最终状态。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100000+10;
int a[maxn], c[maxn], L[maxn], R[maxn], n;
int Lowbit(int x)
{
return x&(-x);
}
void update(int x, int detal)
{
while(x <= n)
{
c[x] += detal;
x += Lowbit(x);
}
}
int getsum(int x)
{
int sum = 0;
while(x)
{
sum += c[x];
x -= Lowbit(x);
}
return sum;
}
int main()
{
int T, t=1;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
c[i] = 0;
scanf("%d", &a[i]);
L[a[i]] = min(i, a[i]);
}
for(int i=n; i>0; i--)
{
R[a[i]] = i + getsum(a[i]);
update(a[i], 1);
}
printf("Case #%d:", t++);
for(int i=1; i<=n; i++)
{
//printf("%d %d", R[i], L[i]);
printf(" %d", R[i] - L[i]);
}
printf("\n");
}
}
题意:让我们求出每一个数在冒泡排序中的排在最左边的位置和最右边的位置的差。
冒泡排序,仔细思考一下其中的原理,每一次我们都是从右边开始在前面前后交换,也就是说每一个数如果左边有几个数比它小,那么在它开始移动的时候,就要向右边移动多少次,所以能够排在最右边的位置就是它的当前位置+右边比它小的数的个数,而能够排在最左边的位置显然就是它的当前位置或者最终状态。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100000+10;
int a[maxn], c[maxn], L[maxn], R[maxn], n;
int Lowbit(int x)
{
return x&(-x);
}
void update(int x, int detal)
{
while(x <= n)
{
c[x] += detal;
x += Lowbit(x);
}
}
int getsum(int x)
{
int sum = 0;
while(x)
{
sum += c[x];
x -= Lowbit(x);
}
return sum;
}
int main()
{
int T, t=1;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
c[i] = 0;
scanf("%d", &a[i]);
L[a[i]] = min(i, a[i]);
}
for(int i=n; i>0; i--)
{
R[a[i]] = i + getsum(a[i]);
update(a[i], 1);
}
printf("Case #%d:", t++);
for(int i=1; i<=n; i++)
{
//printf("%d %d", R[i], L[i]);
printf(" %d", R[i] - L[i]);
}
printf("\n");
}
}
相关文章推荐
- LeetCode--373. Find Pairs with Smallest Sums
- HDOJ 5763 (2016多校联合训练 Training Contest 4) Another Meaning
- Composer:Failed to execute git clone --mirror ...Permission denied (publickey). fatal: Could not
- HDU5724 Chess 阶梯博弈变形 2016 Multi-University Training Contest 1
- Assign directions to edges so that the directed graph remains acyclic
- HDU 2476 String painter(动态规划)★
- Rails 中 View 层 haml 初步
- Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from http://repo.maven.apache.org/ maven2 was cached in the local repository, resolution will not be reattempted until the update interv
- POJ 1363 Rails(栈,模拟地铁出站)
- HDU 1082 Matrix Chain Multiplication(栈和map的使用)
- wait、notify、notifyAll和Condition
- [iPhone]Xcode process launch failed: Security
- CURL命令报错:dyld: lazy symbol binding failed: Symbol not found: _SSL_load_error_strings解决办法
- foxmail导入outlook部分邮件
- POJ 1218 THE DRUNK JAILER
- Aizu 2677 Breadth-First Search by Foxpower
- [USACO Training] Section 1.1
- Codeforces Round #271 (Div. 2) F 线段树+pair
- SPDA-CNN:Unifying Semantic Part Detection and Abstraction for Fine-grained Recognition
- Rails 中的持久化框架 Active recored