hdoj Sort it 2689 (归并排序)
来源:互联网 发布:fc2免费共享视频域名 编辑:程序博客网 时间:2024/06/11 21:01
Sort it
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3498 Accepted Submission(s): 2536
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#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[1010];int b[1010];int n,cnt;void merge(int a[],int s,int m,int e){int i=s,j=m+1,k=s;while(i<=m&&j<=e){if(a[i]<=a[j])b[k++]=a[i++];else{cnt+=j-k;b[k++]=a[j++];}}while(i<=m)b[k++]=a[i++];while(j<=e)b[k++]=a[j++];for(i=s;i<=e;i++)a[i]=b[i];}void mergesort(int a[],int s,int e){if(s<e){int m=(s+e)/2;mergesort(a,s,m);mergesort(a,m+1,e);merge(a,s,m,e);}}int main(){int i,j,mm;while(scanf("%d",&n)!=EOF){for(i=1;i<=n;i++)scanf("%d",&a[i]);cnt=0;mergesort(a,1,n);printf("%d\n",cnt);}return 0;}
0 0
- hdoj Sort it 2689 (归并排序)
- HDOJ-2689 Sort it
- HDU 2689 Sort it 归并排序求逆序对
- 归并排序(merge sort)
- 归并排序(merge sort)
- 归并排序(merge sort)
- 归并排序(Merge Sort)
- 归并排序(merge sort)
- 归并排序(merge sort)
- Merge Sort (归并排序)
- 归并排序(Merge sort)
- 归并排序(Merge sort)
- 归并排序(Merge Sort)
- 归并排序(Merge Sort)
- 归并排序(merging sort)
- 归并排序(Merge Sort)
- 归并排序(Merge Sort)
- 归并排序(Merge Sort)
- 进程内组件注册表注册
- 5-7
- Shape Drawable解析
- 5-10
- Dos下怎么返回上一级目录(DOS命令大全)
- hdoj Sort it 2689 (归并排序)
- 5-11
- 浮点数在内存中的表示
- IOS-使用系统默认声音、震动
- 5-12
- 【详解】修改SQL
- 在Android中使用Alarm小结
- Python中数据结构 列表 元组 字典 序列
- mybatis generator 根据table 创建mapping、dao、model