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. lumen 登陆 注册 demo

    本文将用Lumen来实现一个完整的用户注册.登录及获取用户信息的API. Lumen环境搭建和初始化详细步骤请参考上篇文章<Lumen安装配置使用入门>一文. 一.准备工作 1.Lumen ...

  2. [转]理解android.intent.category.LAUNCHER 具体作用

    转自:http://blog.csdn.net/jackrex/article/details/9189657 android.intent.category.LAUNCHER 具体有什么作用?我做一 ...

  3. [IIS]IIS扫盲(一)

    iis - IIS概念相关 1.IIS(Inter-IC Sound bus)又称I2S,是菲利浦公司提出的串行数字音频总线协议.目前很多音频芯片和MCU都提供了对IIS的支持.IIS总线只处理声音数 ...

  4. [转] 解决HttpServletResponse输出的中文乱码问题

    首先,response返回有两种,一种是字节流outputstream,一种是字符流printwrite. 申明:这里为了方便起见,所有输出都统一用UTF-8编码. 先说字节流,要输出“中国" ...

  5. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

  6. metasploit升级(BT5)

    1.apt-get update 2.apt-get install metasploit 3.修改文件:/opt/metasploit/ruby/lib/ruby/1.9.1/i686-linux/ ...

  7. vc6 编译问题

    Compiling...main.cppLinking...MSVCRT.lib(MSVCRT.dll) : error LNK2005: _malloc already defined in LIB ...

  8. 0525 SCRUM项目7.0

    主题:在下一个SPRINT中做的更好 一,实验回顾总结 当谈到在一个团队里的收获,首当其冲的便是对于团队工作流程的切身体会.亲力亲为.从申报材料.问卷设计.访谈提纲.团队建设.书签制作到实地访谈.问卷 ...

  9. js中RegExp类型

    ECMAScript通过RegExp类型来支持正则表达式. var expression = / pattern / flag ; pattern可以是任意的正则表达式.每个正则都带有标志,用以正则表 ...

  10. Scala伴生类和伴生对象

    单例对象与类同名时,这个单例对象被称为这个类的伴生对象,而这个类被称为这个单例对象的伴生类.伴生类和伴生对象要在同一个源文件中定义,伴生对象和伴生类可以互相访问其私有成员.不与伴生类同名的单例对象称为 ...