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
题目大意:给你一个数n ,然后有1 ~ n 的一个排列,让你找出这个排列的逆序数。
解题思路:此题可以用树状数组来解,树状数组的三个用途:1.单点更新,区间求和 2、区间更新,单点求和
3、求逆序数。求逆序数想法较简单,请看代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std ;
const int MAXN = 1e5 + 7 ;
int C[MAXN] ;
int n ;
int lowbit(int x)
{
return x & -x ;
}
int sum(int x)
{
int sumt = 0 ;
while (x > 0)
{
sumt += C[x] ;
x -= lowbit(x) ;
}
return sumt ;
}
void add(int x , int d)
{
while (x <= n)
{
C[x] += d ;
x += lowbit(x) ;
}
}
int main()
{
while (scanf("%d" , &n) != EOF)
{
int i ;
int ans = 0 ;
memset(C , 0 ,sizeof(C)) ;
for(i = 1 ; i <= n ; i ++)
{
int a ;
scanf("%d" , &a) ;
add(a , 1) ; // 此处是整个程序的精华部分,请好好理解
ans += i - sum(a) ; // 统计逆序数
}
printf("%d\n" , ans) ;
}
return 0 ;
}

HDU 2689 sort it - from lanshui_Yang的更多相关文章

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

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

  2. hdu 2689 Sort it

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 题目分析:求至少交换多少次可排好序,可转换为逆序对问题. 用冒泡排序较为简单,复杂度较大~~ 也 ...

  3. HDU 2689 Sort it【树状数组】

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  4. HDU 2689.Sort it-冒泡排序

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  5. HDU - 2689 Sort it与2016蓝桥杯B 交换瓶子 排序(相邻交换与任意交换)

    Sort it You want to processe a sequence of n distinct integers by swapping two adjacent sequence ele ...

  6. hdu 2689

    hdu 2689 超级大水题....两种代码都过了,开始以为n^2会tle,后来竟然过了...汗 注意下cin写在while里面,就可以了 #include <iostream> usin ...

  7. hdu 1425 sort 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 常规的方法是对输入的数从大到小进行排序(可以用sort或qsort),然后输出前m大的数. 不过 ...

  8. HDU 5884 Sort (二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...

  9. HDU 5884 Sort(二分+优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=5884 题意:有个屌丝设计了一个程序,每次可以将k个数组进行合并,代价为这k个数组总的长度之和.现在另外一个屌丝要 ...

随机推荐

  1. DevExpress GridView.CustomSummaryCalculate 实现自定义Group Summary

    --首发于博客园, 转载请保留链接  博客原文 DevExpress Documentation官方地址:GridView.CustomSummaryCalculate Event 1. 概要 界面上 ...

  2. USB2.0速度识别

    我们知道USB2.0向下兼容USB1.x,即高速2.0的hub能支持所有的速度类型的设备,而USB1.x的hub不能支持高速设备(High Speed Device).因此,如果高速设备挂到USB1. ...

  3. linux服务器内存占用太高-释放内存

    修改/proc/sys/vm/drop_caches,释放Slab占用的cache内存空间(参考drop_caches的官方文档): Writing to this will cause the ke ...

  4. Linux系统调用system_call

    2016-03-25 张超的<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 我的虚拟环境和代码在http ...

  5. jQuery Alert Dialogs (Alert, Confirm, & Prompt代替方案)

    基本范例--原文:http://keleyi.com/keleyi/phtml/jqplug/ Alert jAlert('自定义对话框', 'Alert对话框'); Confirm jConfirm ...

  6. 新版本的strcpy_s

    char a[32] = "1234"; char b[32] ="123"; strcpy_s(b,sizeof(b), a + 2);//可以用strlen ...

  7. UVALive - 5116

    dfs n以内所有素数的乘积map或set删多余的,有点思维在里面,就写写

  8. css应用四

    1.Float属性详解 float属性定义元素在哪个方向浮动. Left 左浮动: Right 右浮动: None 默认,不浮动: Inherit 继承父元素float属性. 示例一: <div ...

  9. ecshop代码详解之init.php

    在includes/init.php目录下 因为工作原因,需要对ecshop二次开发,顺便记录一下对ecshop源代码的一些分析: 首先是init.php文件,这个文件在ecshop每个页面都会 调用 ...

  10. SelectedNode与e.node的区别

    SelectedNode与e.node的区别 待补.......