《树状数组求逆序对数》
来源:互联网 发布:淘宝美工外包 编辑:程序博客网 时间:2024/05/16 16:21
#include<stdio.h>#include<string.h>#define maxn 1005int tree[maxn];int lowbit(int x){ return x&(-x);}void update(int pos,int num){ while(pos<=maxn) { tree[pos]+=num; pos+=lowbit(pos); }}int sum(int pos){ int ans=0; while(pos>0) { ans+=tree[pos]; pos-=lowbit(pos); } return ans;}int main(){ int t,i,num,ans; while(~scanf("%d",&t)) { memset(tree,0,sizeof(tree)); ans=0; for(i=1;i<=t;i++) { scanf("%d",&num); update(num,1); ans+=i-sum(num); } printf("%d\n",ans); } return 0;}
http://acm.hdu.edu.cn/showproblem.php?pid=2689
Problem DescriptionYou 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.InputThe 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.OutputFor each case, output the minimum times need to sort it in ascending order on a single line.Sample Input31 2 34 4 3 2 1Sample Output06AuthorWhereIsHeroFrom
0 0
- 树状数组求逆序对数
- 《树状数组求逆序对数》
- 离散化+树状数组 求逆序对数
- hdu 4911 求逆序对数+树状数组
- 树状数组求逆序对数板子
- 求数组逆序对数
- SGU 180-Inversions(树状数组离散化求逆序对数)
- POJ 2299 利用树状数组求逆序对数
- POJ-2299 Ultra-QuickSort (树状数组 离散 求逆序对数)
- 树状数组与逆序对数
- poj2299 Ultra-QuickSort&&NYOJ117 求逆序数 (树状数组求逆序对数+离散化)+(归并排序)
- 求数组的逆序对数
- 树状数组求Thair对数
- Ultra-QuickSort 求最小交换次数即求逆序对数 树状数组+离散化
- POJ 2299 Ultra-QuickSort 【归并排序 || 树状数组求逆序对数】
- POJ 2299 Ultra-QuickSort 求逆序对数(归并排序,树状数组)
- 求逆序对数的NLogN解法:归并排序、树状数组和线段树
- 蓝桥杯 历届试题 小朋友排队(归并,树状数组求逆序对数)
- yum安装lnmp
- java内存管理
- 通过InitBinder注解,做到全局的格式化转换
- kendo grid序号显示
- Entity Framework Extended Library (EF扩展类库,支持批量更新、删除、合并多个查询等)
- 《树状数组求逆序对数》
- windows 平台下生成 hash 值的几款软件,支持的算法有 md5 、sha1 等,可以校验下载文件的完整性
- php中heredoc的使用方法
- 系统目录介绍
- Java NIO框架Netty教程(一) – Hello Netty
- spring 定时器 详细配置
- Unity3D 串口通信 遇到的问题记录
- tableViewCell自适应高度
- Reverse Nodes in k-Group