title: 261023周五晚 - 排序 author: wu_sir date: 2026-10-23 organization: 河源高级中学 header: title————{title} ———— {author}(organization)footer:起始于:{organization}) footer: 起始于:{date} - - - - - - 第 pageNo页/共{pageNo} 页 / 共 {pageCount} 页 - - - - - - 终止于:${today} keywords:

  • 排序

简单排序

四大基本排序:选择排序、冒泡排序、选择排序、计数排序。

#C1108. 排序【模板】

思路
  • 直接通过 c++ 库函数 sort 完成排序
    • 其内部由:快速排序 + 归并排序完成(混合排序)
  • <algorithm>
  • 其时间复杂度:O(n×log⁡n)O(n\times \log n)
    • log⁡103≈10\log 10^3 \approx 10
    • log⁡106=log⁡(103)2≈10×2=20\log 10^6 = \log (10^3)^2 \approx 10\times 2 = 20
  • 对于本题来说大概 2e7 的计算量,可以被接受

#C1120. 区间排序 [GESP202409 四级]【改】

思路
  • 对于一个区间 [l, r],如果是从小到大排序通过:
    • sort(a + l, a + r + 1, less<int>())
  • 如何从大到小排序一个区间 [l, r]
    • sort(a + l, a + r + 1, greater<int>())

#C1112. 最长连续段 [GESP202509 四级]

思路
  • 对所有元素从小到大排序后,再去重,然后数出最长连续段的长度即可
code
	int n;	cin >> n;
	for(int i = 1; i <= n; i ++) cin >> a[i];
	sort(a + 1, a + n + 1);	// 排序
//	int m = 1;	// 去重后数组的元素个数
//	for(int i = 2; i <= n; i ++) {
//		if(a[i] == a[m]) continue;
////		m ++;
////		a[m] = a[i];
//		a[++ m] = a[i];
//	}
//	n = m;
	n = unique(a + 1, a + n + 1) - a - 1;
	int ans = 1, len = 1;
	for(int i = 2; i <= n; i ++) {
		if(a[i] == a[i - 1] + 1) len ++;
		else len = 1;
		ans = max(ans, len);
	}
	cout << ans;

多关键字排序

场景:正常社会中,不是只看某一个方面的。看很多个方面,这样排序的场景也会变得更复杂,每个方面都可以被抽象为一个数值(关键字),那么可以综合多个角度依次进行排序。

#C1113. 整数奇偶排序

思路
  • 想法一
    • 分为奇偶两个数组,分别排序,分别输出
  • 想法二
    • 直接从小到大排序所有元素
    • 第一遍从右往左扫描所有元素,输出奇数
    • 第二遍从左往右扫描所有元素,输出偶数
  • 想法三
    • 重新定义大小关系,按照新的大小关系进行排序
    • 重新定义两个数 x 和 y,谁应该放前面
code
bool cmp(int x, int y) {	// x 是否一定放在 y 前方
	if((x % 2 == 0) != (y % 2 == 0)) return x % 2 != 0;
	if(x % 2 == 0) return x < y;
	return x > y;
}
void solved() {
	int n;	cin >> n;
	for(int i = 1; i <= n; i ++) cin >> a[i];
//	for(int i = 1; i <= n; i ++) {
//		for(int j = i - 1; j >= 1; j --) {
//			// 如果后面的元素应该放前面来,那就交换它们 
//			if(cmp(a[j + 1], a[j]) == true) swap(a[j], a[j + 1]);
//			else break;
//		}
//	}
	sort(a + 1, a + n + 1, cmp);	// 自定义比较函数 
	for(int i = 1; i <= n; i ++) cout << a[i] << ' ';
	cout << endl;
}

#C1115. 排队合影

思路
  • 结构体排序
  • 利用结构体存储复杂的数据,考虑两个结构体之间谁放前面
code
struct node {
	string xb;
	double sg;
} a[N];

bool cmp(node a, node b) {
	if(a.xb != b.xb) return a.xb == "male";
	if(a.xb == "male") return a.sg < b.sg;
	return a.sg > b.sg;
}
void solved() {
	int n;	cin >> n;
	for(int i = 1; i <= n; i ++) cin >> a[i].xb >> a[i].sg;
	sort(a + 1, a + n + 1, cmp);
	for(int i = 1; i <= n; i ++) cout << a[i].sg << ' ';
	cout << endl;
}
int main() {
//	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(2);
	int t;	cin >> t;	while(t --)
	solved();
	
	return 0;
}

#C1116. 病人排队看病

思路
  • 这里的【先后顺序】其实就是说要保证排序的稳定性
  • 而 sort 使用快排实现,注定了其会丢失稳定性
  • 常用的做法就是增加一个时间参数,当所有数值都相等时按照时间排序
    • 这样就可以保证排序该有的稳定性仍然存在
code
struct node {
	string id;
	int age, time;
} a[N];

bool cmp(node x, node y) {
	if((x.age >= 60) != (y.age >= 60)) return x.age >= 60;
	if(x.age < 60) return x.time < y.time;
	if(x.age == y.age) return x.time < y.time;
	return x.age > y.age;
}
void solved() {
	int n;	cin >> n;
	for(int i = 1; i <= n; i ++) cin >> a[i].id >> a[i].age, a[i].time = i;
	sort(a + 1, a + n + 1, cmp);
	for(int i = 1; i <= n; i ++) cout << a[i].id << endl;
}

#C1118. 同一天生日的人

code
struct node {
	string name;
	int m, d, id;
} a[N];

bool cmp(node a, node b) {
	if(a.m != b.m) return a.m < b.m;
	if(a.d != b.d) return a.d < b.d;
	return a.id < b.id;
}
void solved() {
	int n;	cin >> n;
	for(int i = 1; i <= n; i ++) {
		cin >> a[i].name >> a[i].m >> a[i].d;
		a[i].id = i;
	}
	sort(a + 1, a + n + 1, cmp);
	for(int i = 1; i <= n; i ++) {
		if(i == 1 || a[i].m != a[i - 1].m || a[i].d != a[i - 1].d) {
			if(i != 1) cout << endl;
			cout << a[i].m << ' ' << a[i].d << ' ' << a[i].name;
		} else {
			cout << ' ' << a[i].name;
		}
	}
	cout << endl;
}

0 条评论

目前还没有评论...