提交时间:2024-08-19 21:50:57
运行 ID: 168019
#include <bits/stdc++.h> using namespace std; int n,a[100001],b[100001],c[100001],d[100001],e[100001]; void quick_sort(int l,int r){ if (l>=r) return; int mid=rand()%(r-l+1)+l;//This value is random. int tb=0,tc=0,td=0;//b is smaller than a[mid],c is bigger than it,d is the same of it. for (int i=l;i<=r;i++){ if (a[i]<a[mid]) b[++tb]=a[i]; else if (a[i]>a[mid]) c[++tc]=a[i]; else d[++td]=a[i]; } for (int i=1;i<=tb;i++) a[l+i-1]=b[i]; for (int i=1;i<=td;i++) a[l+i-1+tb]=d[i]; for (int i=1;i<=tc;i++) a[l+i-1+tb+td]=c[i]; quick_sort(l,l+tb-1); quick_sort(l+tb+td,r); } void quick_sort1(int l,int r){ if (l>=r) return; int mid=rand()%(r-l+1)+l;//This value is random. int tb=0,tc=0,td=0;//b is smaller than a[mid],c is bigger than it,d is the same of it. for (int i=l;i<=r;i++){ if (e[i]<e[mid]) b[++tb]=e[i]; else if (e[i]>e[mid]) c[++tc]=e[i]; else d[++td]=e[i]; } for (int i=1;i<=tb;i++) e[l+i-1]=b[i]; for (int i=1;i<=td;i++) e[l+i-1+tb]=d[i]; for (int i=1;i<=tc;i++) e[l+i-1+tb+td]=c[i]; quick_sort1(l,l+tb-1); quick_sort1(l+tb+td,r); } int main(){ srand((int)time(0)); cin>>n; for (int i=1;i<=n;i++) cin>>a[i]>>e[i]; quick_sort(1,n); quick_sort1(1,n); for (int i=1;i<=n;i++) cout<<a[i]<<' '<<e[i]<<endl; return 0; }