Sort it

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

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
分析:好吧,我智障了,听说有种很快的写法,但好像跑的速度没我快?应该是我代码跑的最快吧,写法也是最复杂的,这题我是用树状数组乱搞的,反正15ms,相当快啊!
下面给出AC代码:
 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
{
write(x/);
}
putchar(x%+'');
}
const int N=;
int n;
int num[N];
int c[N];
struct node
{
int x;
int id;
}q[N] ;
bool cmp(node a,node b)
{
return a.x<b.x;
}
int lowbit(int x)
{
return x&(-x);
}
int getsum(int x)
{
int s=;
while(x>)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
void add(int x,int y)
{
while(x<=n)
{
c[x]+=y;
x+=lowbit(x);
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(c,,sizeof(c));
memset(num,,sizeof(num));
for(int i=;i<=n;i++)
{
scanf("%d",&q[i].x);
q[i].id=i;
}
sort(q+,q++n,cmp);
for(int i=;i<=n;i++)
{
num[q[i].id]=i;
}
int sum = ;
for(int i=;i<=n;i++)
{
add(num[i],);
sum+=getsum(n)-getsum(num[i]);
}
printf("%d\n",sum);
}
return ;
}

HDU 2689 Sort it【树状数组】的更多相关文章

  1. HDU 2689 Sort it (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe ...

  2. HDU 5775:Bubble Sort(树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description   P is a permutation ...

  3. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  4. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  5. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  6. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

  7. HDU 3874 Necklace (树状数组 | 线段树 的离线处理)

    Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  8. HDU 5101 Select --离散化+树状数组

    题意:n 组,每组有一些值,求 在不同的两组中每组选一个使值的和大于k的方法数. 解法:n * Cnt[n] <= 1000*100 = 100000, 即最多10^5个人,所以枚举每个值x,求 ...

  9. HDU 4358 Boring counting 树状数组+思路

    研究了整整一天orz……直接上官方题解神思路 #include <cstdio> #include <cstring> #include <cstdlib> #in ...

  10. HDU 4777 Rabbit Kingdom 树状数组

    分析:找到每一个点的左边离他最近的不互质数,记录下标(L数组),右边一样如此(R数组),预处理 这个过程需要分解质因数O(n*sqrt(n)) 然后离线,按照区间右端点排序 然后扫一遍,对于当前拍好顺 ...

随机推荐

  1. HTML5 进阶系列:拖放 API 实现拖放排序(转载)

    HTML5之拖放API实现拖放排序 前言 HTML5 中提供了直接拖放的 API,极大的方便我们实现拖放效果,不需要去写一大堆的 js,只需要通过监听元素的拖放事件就能实现各种拖放功能. 想要拖放某个 ...

  2. HTML列表的常用属性及其应用

    首先列表分成有序和无序分别是<ol><ul>,无序的比较简单,看个例子: <html> <body> <h4>一个无序列表:</h4& ...

  3. SpringMVC 返回json的两种方式

    前后台数据交互使用json是一种很重要的方式.本文主要探讨SpringMVC框架使用json传输的技术. 请注意,本文所提到的项目使用Spring 版本是4.1.7,其他版本在具体使用上可能有不一样的 ...

  4. StringUtils工具类常用方法

    前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...

  5. html统计

    <!doctype html><html lang="en"> <head>  <meta charset="UTF-8&quo ...

  6. ln 命令详解

    ln 命令 作用:它的功能是为某一个文件在另外一个位置建立一个同步的链接 参数:必要参数:  -b 删除,覆盖以前建立的链接  -d 允许超级用户制作目录的硬链接  -f 强制执行  -i 交互模式, ...

  7. JDK动态代理[4]----ProxyGenerator生成代理类的字节码文件解析

    通过前面几篇的分析,我们知道代理类是通过Proxy类的ProxyClassFactory工厂生成的,这个工厂类会去调用ProxyGenerator类的generateProxyClass()方法来生成 ...

  8. 怎么为WebStorm更换主题 修改字体样式

    这篇文章主要用于帮助大家解决怎么为webstorm换theme. 首先,到选择一个自己喜欢的皮肤,Webstorm皮肤网址: http://phpstorm-themes.com/ 然后,选中你喜欢的 ...

  9. JavaScript的DOM编程--06--两个实验

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  10. Linux入门篇(二)——文件

    这一系列的Linux入门都是本人在<鸟哥的Linux私房菜>的基础上总结的基本内容,主要是记录下自己的学习过程,也方便大家简要的了解 Linux Distribution是Ubuntu而不 ...