标签:ret col clu out tor algo mes ace std
题意:给你一些字符串,字符串的权值是逆序对个数,对这个权值排序字符串
对每个字符串归并排序求逆序对即可(我归并排序写跪了就很捞)
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; const int N=202; string b[N],s; int c[N],p[N],cnt; int n,m; struct name { int p; string a; friend inline bool operator < (const name &x,const name &y) { return x.p<y.p; } }t[N]; void merge_sort(int l,int r) { int mid,i,j,tmp; if(l==r) return ; mid=l+r>>1; merge_sort(l,mid); merge_sort(mid+1,r); tmp=l; for(i=l,j=mid+1;i<=mid&&j<=r;) { if(s[i]>s[j]) c[tmp++]=s[j++],cnt+=mid-i+1; else c[tmp++]=s[i++]; } if(j<=r) for(;j<=r;j++) c[tmp++]=s[j]; else for(;i<=mid;i++) c[tmp++]=s[i]; for(i=l;i<=r;i++) s[i]=c[i]; } int calc(int x) { s=t[x].a; cnt=0; merge_sort(0,n-1); t[x].p=cnt; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) cin>>t[i].a,calc(i); sort(t+1,t+m+1); for(int i=1;i<=m;i++) cout<<t[i].a<<endl; return 0; }
标签:ret col clu out tor algo mes ace std
原文地址:https://www.cnblogs.com/pigba/p/8981903.html