Sort it

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4110    Accepted Submission(s): 2920

Problem Description
You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need.
For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.
 
Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n.
 
Output
For each case, output the minimum times need to sort it in ascending order on a single line.
 
Sample Input
3
1 2 3
4
4 3 2 1
 
Sample Output
0
6
 
Author
WhereIsHeroFrom
 
Source
 
Recommend
yifenfei   |   We have carefully selected several similar problems for you:  1892 2688 3584 2492 2227 
 
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2689

题意:交换相邻的两个数,使得序列是上升的。问需要交换多少次。
思路:求出每个数ai前面有多少个数比ai大,或者每个数ai后面有多少个数比ai小。第一种方法只需要交换树状数组更新和求和的函数。第二种方法就是求逆向对的数量和只需要逆向输入a,直接标准的树状数组。
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int c[MAXN];
inline int Lowbit(int x)
{
return x&(-x);
}
void update(int i,int val)
{
for(i; i>; i-=Lowbit(i))
c[i]+=val;
}
int sum(int i)
{
int temp=;
for(i; i<=MAXN; i+=Lowbit(i))
temp+=c[i];
return temp;
}
int main()
{
int i,n;
int a;
while(~scanf("%d",&n))
{
int ans=;
memset(c,,sizeof(c));
for(i=; i<=n; i++)
{
scanf("%d",&a);
ans+=sum(a);
update(a,);
}
cout<<ans<<endl;
}
return ;
}

求左边大于等于 a[i]的数的个 数

HDU 2689Sort it 树状数组 逆序对的更多相关文章

  1. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  2. [树状数组+逆序对][NOIP2013]火柴排队

    火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...

  3. hdu 2838 Cow Sorting (树状数组+逆序对)

    题目 题意:给你N个排列不规则的数,任务是把它从小到大排好,每次只能交换相邻两个数,交换一次的代价为两数之和,求最小代价 拿到这道题,我根本看不出这道题和树状数组有半毛钱关系,博客之,全说用树状数组做 ...

  4. Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对

    题目:http://codevs.cn/problem/3286/ 3286 火柴排队  2013年NOIP全国联赛提高组  时间限制: 1 s   空间限制: 128000 KB   题目等级 : ...

  5. Bzoj 2789: [Poi2012]Letters 树状数组,逆序对

    2789: [Poi2012]Letters Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 278  Solved: 185[Submit][Stat ...

  6. Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2886  Solved: 924[Submit][Stat ...

  7. Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1539  Solved: 665[Submit][Status][Di ...

  8. Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 52306   Accepted: 19194 ...

  9. 【树状数组逆序对】USACO.2011JAN-Above the median

    [题意] 给出一串数字,问中位数大于等于X的连续子串有几个.(这里如果有偶数个数,定义为偏大的那一个而非中间取平均) [思路] 下面的数据规模也小于原题,所以要改成__int64才行.没找到测试数据, ...

随机推荐

  1. JavaScript中 Promise的学习以及使用

    今天一个哥们发过来一段js代码,没看懂,就顺便学习了一下,代码如下  Promise.resolve('zhangkai').then(value => {console.log(value)} ...

  2. 黄聪:MySQL 按指定字段自定义列表排序

    问题描述 大家都知道, MySQL 中按某字段升序排列的 SQL 为 (以 id 为例, 下同): SELECT * FROM `MyTable` , , , ) ORDER BY `id` ASC ...

  3. 对char类型的理解以及对补码的理解分析

    今天遇到这样一个小程序,觉得当中有些问题很容易让人忽略的! 这个程序代码如下: 程序的结果为: 我想很多像我一样的小白可能才开始是想不明白为什么最后的结果是255吧!首先,我们得知道 strlen() ...

  4. jquery 文本域光标操作(选、添、删、取)

    一.JQuery扩展 ; (function ($) { /* * 文本域光标操作(选.添.删.取)的jQuery扩展 http://www.cnblogs.com/phpyangbo/p/55286 ...

  5. MyEclipse中查询

    在当前文件中查询: Ctrl + F 在整个项目中查询:   Ctrl + H(选File Search)

  6. (Array)27. Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. D ...

  7. 洛谷P3367 【模板】并查集

    P3367 [模板]并查集 293通过 551提交 题目提供者HansBug 标签 难度普及- 提交  讨论  题解 最新讨论 不知道哪错了 为啥通不过最后三个节点 题解 不懂为什么MLE 最后一个数 ...

  8. js编译器的一些简单原理

    有没有发现在写代码的时候,往往会遇到一些莫名其妙的错误,然后时间紧急不得不去网上查阅一些代码.虽然要实现的功能解决了,但是看被拷贝的代码好多真心看不懂,以后遇到诸如此类的问题,如果查阅不到这些代码的话 ...

  9. gps转百度地图

    HttpResponse res=WS.url(mapUrl+"/ag/coord/convert?from=0&to=4&x="+longitude+" ...

  10. cookie sessionStorage localStorage 区别

    sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...