排序合集
2299 -- Ultra-QuickSort (poj.org)
#include<iostream>//归并排序 #include<cstring> using namespace std; typedef long long ll; const int N=500010; int a[N],tmp[N],ans; void merge_(ll l,ll mid,ll r){ int i=l,j=mid+1,t=0; while(i<=mid&&j<=r){ if(a[i]>a[j]){ tmp[t++]=a[j++]; ans+=mid-i+1; }else tmp[t++]=a[i++]; } while(i<=mid) tmp[t++]=a[i++]; while(j<=r) tmp[t++]=a[j++]; for(int i=0;i<t;i++) a[l+i]=tmp[i]; } void mergesort(ll l,ll r){ if(l<r){ ll mid=(l+r)>>1; mergesort(l,mid); mergesort(mid+1,r); merge_(l,mid,r); } } signed main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int n; while(cin>>n && n!=0){ memset(a,0,sizeof(a)); ans=0; for(int i=0;i<n;i++) cin>>a[i]; mergesort(0,n-1); cout<<ans<<endl; } return 0; }
2388 -- Who's in the Middle (poj.org)
#include<iostream>//找中间数 using namespace std; const int N=10010; int data[N]; #define swap(a,b){int tmp=a;a=b;b=tmp;} int partition(int left,int right){ int i=left; int tmp=data[right]; for(int j=left;j<right;j++){ if(data[j]<tmp){ swap(data[j],data[i]); i++; } } swap(data[i],data[right]); return i; } void quicksort(int left,int right){ if(left<right){ int i=partition(left,right); partition(left,i-1); partition(i+1,right); } } signed main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int n; cin>>n; for(int i=1;i<=n;i++) cin>>data[i]; quicksort(1,n); cout<<data[(n+1)/2]<<endl;; return 0; }
hdu账号莫名用不了,用洛谷的题平替一下,仅用修改一步,即可通过
P1908 逆序对 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include<iostream>//归并排序 #include<cstring> using namespace std; typedef long long ll; const int N=500010; ll a[N],tmp[N],ans; void merge_(ll l,ll mid,ll r){ ll i=l,j=mid+1,t=0; while(i<=mid&&j<=r){ if(a[i]>a[j]){ tmp[t++]=a[j++]; ans+=mid-i+1; }else tmp[t++]=a[i++]; } while(i<=mid) tmp[t++]=a[i++]; while(j<=r) tmp[t++]=a[j++]; for(int i=0;i<t;i++) a[l+i]=tmp[i]; } void mergesort(ll l,ll r){ if(l<r){ ll mid=(l+r)>>1; mergesort(l,mid); mergesort(mid+1,r); merge_(l,mid,r); } } signed main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int n; cin>>n; memset(a,0,sizeof(a)); ans=0; for(int i=0;i<n;i++) cin>>a[i]; mergesort(0,n-1); cout<<ans<<endl; return 0; }
hdu账号早日回归希望!
#include<iostream>//1425解法1 #include<algorithm> using namespace std; const int N=1e6+10; int n,m,a[N]; bool cmp(int a,int b){ return a>b;} signed main(){ //ios::sync_with_stdio(false); //cin.tie(0);cout.tie(0); while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+1+n,cmp); for(int i=1;i<=m;i++) printf("%d ",a[i]); } return 0; }
#include<iostream>//1425解法2 #include<cstring> using namespace std; const int N=1e6+10; int n,m,t,a[N]; signed main(){ while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++){ scanf("%d",&t); a[t+500000]=1; } for(int i=N;m>0;i--){ if(a[i]){ if(m>1) printf("%d ",i-500000); else printf("%d\n",i-500000); m--; } } } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)