HDU 2689 Sort it(树状数组求逆序数)
来源:互联网 发布:淘宝网1 编辑:程序博客网 时间:2024/06/06 20:54
Sort it
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3418 Accepted Submission(s): 2478
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.
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
31 2 34 4 3 2 1
Sample Output
06
Author
WhereIsHeroFrom
Source
ZJFC 2009-3 Programming Contest
题意,用冒泡排序的方法把n个数排成按升序排列需要几步。
点击打开链接
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>using namespace std;const int N = 1001;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 = 0; while(x>0){ 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,0,sizeof(c)); memset(num,0,sizeof(num)); for(int i=1;i<=n;i++){ scanf("%d",&q[i].x); q[i].id = i; } sort(q+1,q+1+n,cmp); for(int i=1;i<=n;i++){ num[q[i].id] = i; } int sum = 0; for(int i=1;i<=n;i++){ add(num[i],1); sum += getsum(n) - getsum(num[i]); } printf("%d\n",sum); } return 0;}
1 0
- HDU 2689 Sort it 求逆序数,树状数组实现
- HDU 2689 Sort it(树状数组求逆序数)
- nyoj- 117 求逆序数 && hdu-sort it----> 树状数组
- hdu 2689 sort it(树状数组 逆序数)
- HDU 2689 Sort it(树状数组,逆序数)
- hdu 2689 sort it (树状数组 逆序数)
- HDU2689 Sort it (树状数组求逆序数)
- HDU2689 Sort it(树状数组求逆序数)
- HDU 5775 Bubble Sort 树状数组求逆序数
- hdu 2689树状数组求逆序数
- HDU2689 Sort it【树状数组】【逆序数】
- HDU 2689 Sort it(树状数组)(类似逆序数,同样不需要离散化)
- hdu 1394 树状数组求逆序数
- hdu 1394(树状数组求逆序数)
- HDU2689-树状数组&逆序数|分治-Sort it
- nyoj 322 sort(树状数组求逆序数)
- 树状数组求逆序数
- 树状数组求逆序数
- 理解HotSpot的CMS GC
- Objective-C中不同方式实现锁(一)
- centos7 lamp环境配置及多域名设置
- 黑马程序员 反射
- CCF 201312-1出现次数最多的数
- HDU 2689 Sort it(树状数组求逆序数)
- js 判断文件是否存在
- Perforce初体验
- 纯CSS分页代码
- mongoDB分片集群常用指令
- Android 问题之找不到资源解决办法
- HDU 1071 The area(数学 抛物线 积分)
- 13. CSS 链接
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)