HDu 2686 Sort is (树状数组)
来源:互联网 发布:江西理工大学软件学院 编辑:程序博客网 时间:2024/05/21 12:40
Sort it
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4744 Accepted Submission(s): 3301
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他们有的用冒泡排序水过这道题,这道题的正解是用树状数组,然后求逆序数。树状数组入门博客:http://blog.csdn.net/passer__/article/details/76653144题意:通过多少次可以将给的数字变成从小到大的序列。#include<stdio.h>//逆序数 #include<string.h>int a[1050];//刷新数字 int c[1050];//储存有当前这个数字前边有多少比他大的的数字 int lowbit(int x)//树状 { return x&(-x);}void sum1(int x,int y){ int sum=0; while(x>0) { sum=sum+a[x]; x=x-lowbit(x); } c[y]=y-1-sum;//除去自己 用已经放进去的数字减去他前边有多少比他小的数字 就是前边有多少比它大的数字}void sum(int x) { while(x<=1010) { a[x]++; x=x+lowbit(x); }} int main(){ int n,x; while(scanf("%d",&n)!=EOF) { memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) { scanf("%d",&x); sum1(x,i);//查找当前数字前有多少比他小的 sum(x); } int sum=0; for(int i=1;i<=n;i++) sum=sum+c[i]; printf("%d\n",sum); } return 0;}
阅读全文
0 0
- HDu 2686 Sort is (树状数组)
- HDU 2689 Sort it (树状数组)
- hdu 5774 Bubble Sort(树状数组)
- HDU - 5775 Bubble Sort(树状数组)
- HDU 5775 Bubble Sort(树状数组)
- hdu 2689 Sort it(树状数组)
- HDU 5775 Bubble Sort (树状数组或线段树)
- hdu 5775 Bubble Sort 树状数组(多校)
- hdu 2689 sort it (树状数组 逆序数)
- HDU 5775 Bubble Sort 树状数组
- 树状数组-HDU-5775-Bubble Sort
- HDU-5775-Bubble Sort-树状数组
- HDU 2689 Sort it [树状数组]【数据结构】
- HDU 5775 Bubble Sort [树状数组]【数据结构】
- HDU 2689 Sort it 树状数组
- HDU5775Bubble Sort(树状数组)
- HDU5775Bubble Sort(树状数组)
- HDU 5792 World is Exploding (树状数组逆序对)
- mysql区分大小写方法
- The used SELECT statements have a different number of columns 错误解决
- 白话经典算法系列之六 快速排序 快速搞定
- Tensorflow实现一个CNN分类的例子
- 贝佐斯:人生意义,就在于你的选择
- HDu 2686 Sort is (树状数组)
- Spring Aspactj .xmlSpring-jdbc 的实现
- linux中的软连接与硬连接
- adb中常用的命令
- TCP协议之三次握手
- Spring Aspectj xml注解,JDBCSessionTemplate
- 【bzoj2244】[SDOI2011]拦截导弹
- 一个最近挺火的新闻,初入社会的小伙伴们应该要谨慎。。
- HDOJ 2042 不容易系列之二