百练+归并排序求逆序数+注意最后是按逆序数大小输出原来的序列啊!
来源:互联网 发布:阿里云域名怎么绑定ip 编辑:程序博客网 时间:2024/05/29 15:54
点击打开链接
#include<stdio.h>#include<stdlib.h>#include<iostream>#include<string.h>#include<cstring>#include<string>#include<algorithm>#include<math.h>#include<queue>#include<set>#define LL long long#define inf 0x3f3f3f3f#define mod 1e9+7using namespace std;const int maxn=1e5+5;char temp[maxn];char ss[maxn];int n,m,value;struct Node{ int sum; int num; char s[55];}Q[105];void Merge(char a[],int left,int mid,int right){ int i=left,j=mid+1,n=0,length=right-left;///i开始为左半部分最左边,j为右半部分最左边。temp数组是从下标0开始存数。 while(i<=mid&&j<=right){ if(a[i]>a[j]){///左边比右边大。 temp[n++]=a[j++]; value+=mid-i+1;///从i到mid都是比a[j]大。 } else{ temp[n++]=a[i++]; } } if(i>mid){///这里说明的是左边全部填满了(因为前面的判断条件是i<=mid,那就是天右边了。 while(j<=right){ temp[n++]=a[j++]; } } else{ while(i<=mid){ temp[n++]=a[i++]; } } for(int k=0;k<=length;k++){///最后赋值到原数组必须要有的。 a[left+k]=temp[k]; }}void mergesort(char a[],int left,int right){ if(left<right){ int mid=(left+right)/2; mergesort(a,left,mid); mergesort(a,mid+1,right); Merge(a,left,mid,right); }}bool cmp(Node x,Node y){ if(x.sum==y.sum){ return x.num<y.num; } return x.sum<y.sum;}int main(){ int i=0; memset(Q,0,sizeof(Q)); scanf("%d %d",&n,&m); for(i=0;i<m;i++){ scanf("%s",Q[i].s); } for(i=0;i<m;i++){ strcpy(ss,Q[i].s);///字符串赋值函数有点忘了额。 ///cout<<ss<<endl; value=0; mergesort(ss,0,strlen(Q[i].s)-1); Q[i].num=i+1;///标记编号 Q[i].sum=value; /// printf("%d\n",value); } sort(Q,Q+m,cmp); for(i=0;i<m;i++){ memset(temp,0,sizeof(temp)); cout<<Q[i].s<<endl; } return 0;}
0 0
- 百练+归并排序求逆序数+注意最后是按逆序数大小输出原来的序列啊!
- 利用归并排序求有限序列的逆序数
- 归并排序,求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 归并排序求逆序数
- 求逆序数(归并排序)
- 归并排序求逆序数
- 归并排序&求逆序数
- 归并排序求逆序数
- Chrome和Firefox常用插件
- java中的IO整理
- 使用Docker registry镜像创建私有仓库
- 全局编录服务器(GC)
- neural artistic style艺术风格图片生成测试
- 百练+归并排序求逆序数+注意最后是按逆序数大小输出原来的序列啊!
- 【DP】最大子串和
- DLNA 笔记
- STL栈 - 火车进出栈
- nyoj305
- fzoj Problem 2190 非提的救赎 【单调栈】
- 【leetcode】226. Invert Binary Tree
- C++ 结构体指针的定义
- 深入了解viewport和px