集合帖:排序 ← sort() 函数

排序算法的学习虽然很重要、很必要,但是在算法竞赛中,一般不需要自己写排序的代码,而是直接调用 C++ 的 sort() 函数就可以了。详见:https://blog.csdn.net/hnjzsyjyj/article/details/130524018
https://blog.csdn.net/hnjzsyjyj/article/details/144239572
https://blog.csdn.net/hnjzsyjyj/article/details/144329247
https://blog.csdn.net/hnjzsyjyj/article/details/120184972

一、洛谷 P1862:输油管道问题 ← 排序(从小到大
题目来源:https://www.luogu.com.cn/problem/P1862
算法代码:https://blog.csdn.net/hnjzsyjyj/article/details/145095929

#include 
using namespace std;
 
const int maxn=1e4+5;
int x,y[maxn]; //Ignore the x-coordinate
 
int main() {
    int n;
    cin>>n;
    for(int i=0; i>x>>y[i];
    }
    sort(y,y+n);
    int m=y[n/2]; //median
 
    int ans=0;
    for(int i=0; i

二、lanqiaoOJ 3333:肖恩的排序 ← 双指针+排序(从大到小
题目来源:
https://www.lanqiao.cn/problems/3333/learning/
算法代码:https://blog.csdn.net/hnjzsyjyj/article/details/145159640

#include
using namespace std;
 
typedef long long LL;
const int MOD=1e9+7;
const int maxn=1e5+5;
LL a[maxn],b[maxn];
int n;
 
bool down(int u,int v) {
    return u>v;
}
 
int main() {
    cin>>n;
    for(int i=0; i>a[i];
    for(int i=0; i>b[i];
 
    sort(a,a+n,down);
    sort(b,b+n,down);
 
    LL cnt=0,ans=1;
    for(int i=0,j=0; jb[j]) {
            cnt++,i++;
        }
        ans*=cnt; //Multiplication principle
        cnt--; //Used one, Minus it
        ans%=MOD;
    }
    cout<

三、lanqiaoOJ 2122:数位排序 ← 排序(自定义比较函数
题目来源:
https://www.lanqiao.cn/problems/2122/learning/
算法代码:https://blog.csdn.net/hnjzsyjyj/article/details/145165865

#include
using namespace std;
 
const int maxn=1e6+5;
int a[maxn], b[maxn];
 
int sum(int x) {
    int ans=0;
    while(x) {
        ans+=x%10;
        x/=10;
    }
    return ans;
}
 
bool cmp(int x, int y) {
    if(b[x]==b[y])  return x>n>>m;
    for(int i=1; i<=n; i++) {
        a[i]=i;
        b[i]=sum(i);
    }
    sort(a+1,a+1+n,cmp);
 
    cout<

四、洛谷 P1223:排队接水 ← 贪心算法+排序(结构体排序
题目来源:
https://www.luogu.com.cn/problem/P1223
算法代码:https://blog.csdn.net/hnjzsyjyj/article/details/142996240

#include 
using namespace std;
 
const int maxn=1005;
struct Water {
    int id;
    int time;
} p[maxn];
 
bool up(Water a,Water b) {
    if(a.time!=b.time)
        return a.time>n;
    for(int i=1; i<=n; i++) {
        cin>>p[i].time;
        p[i].id=i;
    }
    sort(p+1,p+n+1,up);
 
    for(int i=1; i<=n; i++) ans+=(n-i)*p[i].time;
    for(int i=1; i<=n; i++) cout<

五、洛谷 P1781:宇宙总统 ← 排序(字符串排序)
题目来源:https://www.luogu.com.cn/problem/P1781
算法代码:https://blog.csdn.net/hnjzsyjyj/article/details/145169301

#include
using namespace std;
 
const int maxn=25;
struct Person {
    string s; //vote used high precision
    int id;
} p[maxn];
 
bool cmp(Person u,Person v) {
    if(u.s.size() == v.s.size()) return u.s>n;
    for(int i=1; i<=n; i++) {
        cin>>p[i].s;
        p[i].id=i;
    }
    sort(p+1,p+1+n,cmp);
    cout<

六、罗勇军OJ 1093:分香蕉 ← 排序(复杂排序)
题目来源:
http://oj.ecustacm.cn/problem.php?id=1093
算法代码:https://blog.csdn.net/hnjzsyjyj/article/details/145166378

#include
using namespace std;
 
const int maxn=1e5+5;
int a[maxn]; //banana
int part[maxn];
 
struct Monkey {
    int id,weight,cnt;
} mk[maxn];
 
bool cmpWT(Monkey u, Monkey v) {
    return u.weight>v.weight;
}
 
bool cmpID(Monkey u, Monkey v) {
    return u.id>n>>m;
    for(int i=1; i<=n; i++) cin>>a[i];
    sort(a+1,a+1+n);
 
    for(int i=1; i<=m; i++) {
        cin>>mk[i].weight;
        mk[i].id=i;
    }
    sort(mk+1,mk+1+m,cmpWT); //Sort by monkey's weight
 
    for(int i=1; i<=n; i++) {
        part[i%m]+=a[n-i+1]; //Divide banana into m parts
    }
 
    for(int i=1; i<=m; i++) {
        mk[i].cnt=part[i%m]; //assign to m monkeys
    }
 
    sort(mk+1,mk+1+m,cmpID); //Sort by monkey's id
    for(int i=1; i<=m; i++) cout<




【参考文献】
https://mp.weixin.qq.com/s/hGgUoujxTrLiDNI9t0U8Kg
https://blog.csdn.net/hnjzsyjyj/article/details/145095929
https://blog.csdn.net/hnjzsyjyj/article/details/145159640
https://blog.csdn.net/hnjzsyjyj/article/details/145165865
https://blog.csdn.net/hnjzsyjyj/article/details/142996240
https://blog.csdn.net/hnjzsyjyj/article/details/145169301
https://blog.csdn.net/hnjzsyjyj/article/details/145166378



 

你可能感兴趣的:(信息学竞赛,#,排序与查找,数据结构,排序算法)