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. [入门]bower安装和使用

    bower安装和使用 字数745 阅读10127 评论2 喜欢3 bower的安装 1,首先在我的系统 安装 nodejs.因为我的系统是windows,还需要安装msysgit,注意图二中的选项 m ...

  2. MSSQL 判断一个时间段是否在另一个时间段内!

    MSSQL 判断一个时间段是否在另一个时间段内! 1 CREATE TABLE #B ( MeetingRoom int, BeginTime datetime, EndTime datetime ) ...

  3. OpenGL 学习

    一.红宝书学习资料汇集 第八版的图书源码 源代码: http://opengl-redbook.com/Code/oglpg-8th-edition.zip 第九版的图书源码 http://www.o ...

  4. NeHe OpenGL教程 第四十八课:轨迹球

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  5. su和su-命令的本质区别

    su命令和su -命令最大的本质区别就是:前者只是切换了root身份,但Shell环境仍然是普通用户的Shell: 而后者连用户和Shell环境一起切换成root身份了.只有切换了Shell环境才不会 ...

  6. 【转载】Securing Kibana + Elasticsearch

    from: http://tom.meinlschmidt.org/2014/05/19/securing-kibana-elasticsearch/ After some successful se ...

  7. Linux批量更改文件后缀名

    一.rename解决 1.  Ubuntu系统下 rename 's/\.c/\.h/'  ./* 把当前目录下的后缀名为.c的文件更改为.h的文件 2.  CentOS5.5系统下 rename . ...

  8. (转载)C#中使用GUID

    原文地址:http://www.cnblogs.com/dongqi/archive/2008/10/13/1310128.html GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同 ...

  9. pageContext对象的用法

    (1) pageContext对象 这个对象代表页面上下文,该对象主要用于访问JSP之间的共享数据. pageContext是PageContext类的实例,使用pageContext可以访问page ...

  10. CSS基础篇

    写的不错,收藏 http://www.cnblogs.com/suoning/p/5625582.html