Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
168066 | B班 马淑龄 | 双关键字排序 | C++ | 通过 | 100 | 461 MS | 2044 KB | 959 | 2024-08-19 22:10:22 |
#include <bits/stdc++.h> using namespace std; struct S{ int num1; int num2; }; int n; S a[100001],b[100001],c[100001],d[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].num1<a[mid].num1 || (a[i].num1==a[mid].num1 && a[i].num2<a[mid].num2)) b[++tb]=a[i]; else if (a[i].num1>a[mid].num1 || (a[i].num1==a[mid].num1 && a[i].num2>a[mid].num2)) 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); } int main(){ srand((int)time(0)); cin>>n; for (int i=1;i<=n;i++) cin>>a[i].num1>>a[i].num2; quick_sort(1,n); for (int i=1;i<=n;i++) cout<<a[i].num1<<' '<<a[i].num2<<endl; return 0; }